"""add delete cascades to source connection

Revision ID: 9a528c5b6fe4
Revises: a8b2c3d4e5f6
Create Date: 2025-05-06 22:57:59.782753

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '9a528c5b6fe4'
down_revision = 'a8b2c3d4e5f6'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('source_connection_dag_id_fkey', 'source_connection', type_='foreignkey')
    op.drop_constraint('source_connection_sync_id_fkey', 'source_connection', type_='foreignkey')
    op.drop_constraint('source_connection_readable_collection_id_fkey', 'source_connection', type_='foreignkey')
    op.drop_constraint('fk_source_connection_connection_id', 'source_connection', type_='foreignkey')
    op.drop_constraint('source_connection_integration_credential_id_fkey', 'source_connection', type_='foreignkey')
    op.create_foreign_key(None, 'source_connection', 'connection', ['connection_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'source_connection', 'collection', ['readable_collection_id'], ['readable_id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'source_connection', 'sync', ['sync_id'], ['id'], ondelete='CASCADE')
    op.drop_column('source_connection', 'integration_credential_id')
    op.drop_column('source_connection', 'dag_id')
    op.drop_column('source_connection', 'cron_schedule')
    op.drop_constraint('sync_white_label_id_fkey', 'sync', type_='foreignkey')
    op.create_foreign_key(None, 'sync', 'white_label', ['white_label_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'sync', type_='foreignkey')
    op.create_foreign_key('sync_white_label_id_fkey', 'sync', 'white_label', ['white_label_id'], ['id'])
    op.add_column('source_connection', sa.Column('cron_schedule', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.add_column('source_connection', sa.Column('dag_id', sa.UUID(), autoincrement=False, nullable=True))
    op.add_column('source_connection', sa.Column('integration_credential_id', sa.UUID(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'source_connection', type_='foreignkey')
    op.drop_constraint(None, 'source_connection', type_='foreignkey')
    op.drop_constraint(None, 'source_connection', type_='foreignkey')
    op.create_foreign_key('source_connection_integration_credential_id_fkey', 'source_connection', 'integration_credential', ['integration_credential_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key('fk_source_connection_connection_id', 'source_connection', 'connection', ['connection_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key('source_connection_readable_collection_id_fkey', 'source_connection', 'collection', ['readable_collection_id'], ['readable_id'], ondelete='SET NULL')
    op.create_foreign_key('source_connection_sync_id_fkey', 'source_connection', 'sync', ['sync_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key('source_connection_dag_id_fkey', 'source_connection', 'sync_dag', ['dag_id'], ['id'], ondelete='SET NULL')
    # ### end Alembic commands ###
