"""Correct Not Null constraints on TenantConsent

Revision ID: 2228f7cafada
Revises: a6ee51d72bb4
Create Date: 2025-10-15 07:44:13.243434

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '2228f7cafada'
down_revision = 'a6ee51d72bb4'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('tenant_consent', schema=None) as batch_op:
        batch_op.alter_column('partner_id',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('partner_service_id',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.drop_constraint('tenant_consent_created_by_fkey', type_='foreignkey')
        batch_op.drop_constraint('tenant_consent_user_id_fkey', type_='foreignkey')
        batch_op.drop_constraint('tenant_consent_partner_service_id_fkey', type_='foreignkey')
        batch_op.drop_constraint('tenant_consent_updated_by_fkey', type_='foreignkey')
        batch_op.drop_constraint('tenant_consent_partner_id_fkey', type_='foreignkey')
        batch_op.drop_constraint('tenant_consent_tenant_id_fkey', type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'], referent_schema='public')
        batch_op.create_foreign_key(None, 'user', ['updated_by'], ['id'], referent_schema='public')
        batch_op.create_foreign_key(None, 'partner_service', ['partner_service_id'], ['id'], referent_schema='public')
        batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public')
        batch_op.create_foreign_key(None, 'partner', ['partner_id'], ['id'], referent_schema='public')
        batch_op.create_foreign_key(None, 'user', ['created_by'], ['id'], referent_schema='public')


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('tenant_consent', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key('tenant_consent_tenant_id_fkey', 'tenant', ['tenant_id'], ['id'])
        batch_op.create_foreign_key('tenant_consent_partner_id_fkey', 'partner', ['partner_id'], ['id'])
        batch_op.create_foreign_key('tenant_consent_updated_by_fkey', 'user', ['updated_by'], ['id'])
        batch_op.create_foreign_key('tenant_consent_partner_service_id_fkey', 'partner_service', ['partner_service_id'], ['id'])
        batch_op.create_foreign_key('tenant_consent_user_id_fkey', 'user', ['user_id'], ['id'])
        batch_op.create_foreign_key('tenant_consent_created_by_fkey', 'user', ['created_by'], ['id'])
        batch_op.alter_column('partner_service_id',
               existing_type=sa.INTEGER(),
               nullable=False)
        batch_op.alter_column('partner_id',
               existing_type=sa.INTEGER(),
               nullable=False)

    # ### end Alembic commands ###
