"""Security Additions to Tenants CORS Part 2

Revision ID: b79748d3aaae
Revises: 29c9d981a10b
Create Date: 2024-05-15 14:33:22.321287

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'b79748d3aaae'
down_revision = '29c9d981a10b'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('tenant_domain',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('tenant_id', sa.Integer(), nullable=False),
    sa.Column('domain', sa.String(length=255), nullable=False),
    sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('domain'),
    schema='public'
    )
    with op.batch_alter_table('roles_users', schema=None) as batch_op:
        batch_op.drop_constraint('roles_users_user_id_fkey', type_='foreignkey')
        batch_op.drop_constraint('roles_users_role_id_fkey', type_='foreignkey')
        batch_op.create_foreign_key(None, 'role', ['role_id'], ['id'], referent_schema='public', ondelete='CASCADE')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'], referent_schema='public', ondelete='CASCADE')

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_constraint('user_tenant_id_fkey', type_='foreignkey')
        batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key('user_tenant_id_fkey', 'tenant', ['tenant_id'], ['id'])

    with op.batch_alter_table('roles_users', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.create_foreign_key('roles_users_role_id_fkey', 'role', ['role_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key('roles_users_user_id_fkey', 'user', ['user_id'], ['id'], ondelete='CASCADE')

    op.drop_table('tenant_domain', schema='public')
    # ### end Alembic commands ###
