Files
ss-tools/backend/alembic/versions/b0c1d2e3f4a5_cascade_ondelete_translate_fks.py
busya 4205618ee6 chore: eliminate all deprecation warnings from tests and linter
Warnings fixed:
- datetime.utcnow() → datetime.now(UTC) across 48+ files (src/ + tests/)
- datetime.utcnow (callback ref) → lambda: datetime.now(UTC) in model fields (18 files)
- Pydantic class Config → model_config = ConfigDict(...) (16 files)
- Pydantic .dict() → .model_dump() (8 files)
- ConfigDict(allow_population_by_field_name=True) → validate_by_name=True
- SQLAlchemy declarative_base() import path updated
- FastAPI on_event → lifespan context manager (app.py)
- Import sorting (ruff I001) auto-fixed across all files
- Fixed broken re-export chains that ruff F401 cleanup broke:
  _validate_bcp47: service.py now imports from dictionary_validation directly
  job_to_response: _job_routes.py and test imports from service_utils directly
  fetch_datasource_metadata: restored re-export in service.py
- Added missing TranslateJobService import in _job_routes.py (was deleted by F401)
- Added ConfigDict(protected_namespaces=()) for DashboardDatasetItem schema field
- pytest.ini: replaced deprecated importmode with asyncio_mode

All 440 tests pass with zero deprecation warnings.
2026-05-26 19:18:28 +03:00

83 lines
3.8 KiB
Python

"""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'],
)