"""cascade ondelete for all translate FK references (translation_jobs/runs/batches chain) Revision ID: b0c1d2e3f4a5 Revises: f0e9d8c7b6a5 Create Date: 2026-05-21 19:05:00.000000 """ from collections.abc import Sequence from alembic import op # revision identifiers, used by Alembic. revision: str = 'b0c1d2e3f4a5' down_revision: str | Sequence[str] | None = 'f0e9d8c7b6a5' branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None # Each entry: (table, constraint_name, column, ref_table, ondelete_rule) FK_FIXES = [ # Direct FKs to translation_jobs.id ('translation_runs', 'translation_runs_job_id_fkey', 'job_id', 'CASCADE'), ('translation_events', 'translation_events_job_id_fkey', 'job_id', 'CASCADE'), ('translation_preview_sessions', 'translation_preview_sessions_job_id_fkey', 'job_id', 'CASCADE'), ('translation_schedules', 'translation_schedules_job_id_fkey', 'job_id', 'CASCADE'), ('translation_job_dictionaries', 'translation_job_dictionaries_job_id_fkey', 'job_id', 'CASCADE'), ('translation_metric_snapshots', 'translation_metric_snapshots_job_id_fkey', 'job_id', 'CASCADE'), # FKs to translation_runs.id ('translation_batches', 'translation_batches_run_id_fkey', 'run_id', 'CASCADE'), ('translation_records', 'translation_records_run_id_fkey', 'run_id', 'CASCADE'), ('translation_events', 'translation_events_run_id_fkey', 'run_id', 'SET NULL'), ('translation_preview_sessions', 'translation_preview_sessions_run_id_fkey', 'run_id', 'SET NULL'), ('translation_metric_snapshots', 'translation_metric_snapshots_run_id_fkey', 'run_id', 'SET NULL'), ('translation_run_language_stats', 'translation_run_language_stats_run_id_fkey', 'run_id', 'CASCADE'), # FKs to translation_batches.id ('translation_records', 'translation_records_batch_id_fkey', 'batch_id', 'CASCADE'), # FKs to translation_records.id ('translation_languages', 'translation_languages_record_id_fkey', 'record_id', 'CASCADE'), # FKs to translation_preview_sessions.id ('translation_preview_records', 'translation_preview_records_session_id_fkey', 'session_id', 'CASCADE'), # FKs to translation_preview_records.id ('translation_preview_languages', 'translation_preview_languages_preview_record_id_fkey', 'preview_record_id', 'CASCADE'), ] def upgrade() -> None: """Upgrade schema.""" for table, constraint, column, ondelete in FK_FIXES: op.drop_constraint(constraint, table, type_='foreignkey') op.create_foreign_key( constraint, table, 'translation_jobs' if 'job_id' in column else ( 'translation_runs' if 'run_id' in column else ( 'translation_batches' if 'batch_id' in column else ( 'translation_records' if 'record_id' in column and 'preview' not in constraint else ( 'translation_preview_sessions' if 'session_id' in column else ( 'translation_preview_records' if 'preview_record_id' in column else 'unknown' ) ) ) ) ), [column], ['id'], ondelete=ondelete, ) def downgrade() -> None: """Downgrade schema.""" ref_map = { 'job_id': 'translation_jobs', 'run_id': 'translation_runs', 'batch_id': 'translation_batches', 'record_id': 'translation_records', 'session_id': 'translation_preview_sessions', 'preview_record_id': 'translation_preview_records', } for table, constraint, column, _ondelete in reversed(FK_FIXES): ref_table = ref_map[column] op.drop_constraint(constraint, table, type_='foreignkey') op.create_foreign_key( constraint, table, ref_table, [column], ['id'], )