"""Fix foreign key CASCADE constraints in user_organization

Revision ID: c6724d75676a
Revises: c61f1307be00
Create Date: 2025-06-10 20:30:26.874633

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c6724d75676a'
down_revision = 'c61f1307be00'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f('api_key_organization_id_fkey'), 'api_key', type_='foreignkey')
    op.create_foreign_key(None, 'api_key', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('collection_organization_id_fkey'), 'collection', type_='foreignkey')
    op.create_foreign_key(None, 'collection', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('dag_edge_organization_id_fkey'), 'dag_edge', type_='foreignkey')
    op.create_foreign_key(None, 'dag_edge', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('dag_node_organization_id_fkey'), 'dag_node', type_='foreignkey')
    op.create_foreign_key(None, 'dag_node', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('entity_organization_id_fkey'), 'entity', type_='foreignkey')
    op.create_foreign_key(None, 'entity', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('integration_credential_organization_id_fkey'), 'integration_credential', type_='foreignkey')
    op.create_foreign_key(None, 'integration_credential', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('source_connection_organization_id_fkey'), 'source_connection', type_='foreignkey')
    op.create_foreign_key(None, 'source_connection', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('sync_organization_id_fkey'), 'sync', type_='foreignkey')
    op.create_foreign_key(None, 'sync', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('sync_dag_organization_id_fkey'), 'sync_dag', type_='foreignkey')
    op.create_foreign_key(None, 'sync_dag', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('sync_job_organization_id_fkey'), 'sync_job', type_='foreignkey')
    op.create_foreign_key(None, 'sync_job', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('user_organization_user_id_fkey'), 'user_organization', type_='foreignkey')
    op.drop_constraint(op.f('user_organization_organization_id_fkey'), 'user_organization', type_='foreignkey')
    op.create_foreign_key(None, 'user_organization', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'user_organization', 'user', ['user_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(op.f('white_label_organization_id_fkey'), 'white_label', type_='foreignkey')
    op.create_foreign_key(None, 'white_label', 'organization', ['organization_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'white_label', type_='foreignkey')
    op.create_foreign_key(op.f('white_label_organization_id_fkey'), 'white_label', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'user_organization', type_='foreignkey')
    op.drop_constraint(None, 'user_organization', type_='foreignkey')
    op.create_foreign_key(op.f('user_organization_organization_id_fkey'), 'user_organization', 'organization', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('user_organization_user_id_fkey'), 'user_organization', 'user', ['user_id'], ['id'])
    op.drop_constraint(None, 'sync_job', type_='foreignkey')
    op.create_foreign_key(op.f('sync_job_organization_id_fkey'), 'sync_job', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'sync_dag', type_='foreignkey')
    op.create_foreign_key(op.f('sync_dag_organization_id_fkey'), 'sync_dag', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'sync', type_='foreignkey')
    op.create_foreign_key(op.f('sync_organization_id_fkey'), 'sync', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'source_connection', type_='foreignkey')
    op.create_foreign_key(op.f('source_connection_organization_id_fkey'), 'source_connection', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'integration_credential', type_='foreignkey')
    op.create_foreign_key(op.f('integration_credential_organization_id_fkey'), 'integration_credential', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'entity', type_='foreignkey')
    op.create_foreign_key(op.f('entity_organization_id_fkey'), 'entity', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'dag_node', type_='foreignkey')
    op.create_foreign_key(op.f('dag_node_organization_id_fkey'), 'dag_node', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'dag_edge', type_='foreignkey')
    op.create_foreign_key(op.f('dag_edge_organization_id_fkey'), 'dag_edge', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'collection', type_='foreignkey')
    op.create_foreign_key(op.f('collection_organization_id_fkey'), 'collection', 'organization', ['organization_id'], ['id'])
    op.drop_constraint(None, 'api_key', type_='foreignkey')
    op.create_foreign_key(op.f('api_key_organization_id_fkey'), 'api_key', 'organization', ['organization_id'], ['id'])
    # ### end Alembic commands ###
