"""cascade ondelete for all remaining environments FK references

Revision ID: f0e9d8c7b6a5
Revises: a5b6c7d8e9f0, e5f4d3c2b1a
Create Date: 2026-05-21 19:00:00.000000

"""
from collections.abc import Sequence

from alembic import op

# revision identifiers, used by Alembic.
revision: str = 'f0e9d8c7b6a5'
down_revision: str | Sequence[str] | None = ('a5b6c7d8e9f0', 'e5f4d3c2b1a')
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None

FK_DEFS = [
    ('resource_mappings', 'resource_mappings_environment_id_fkey', 'environment_id'),
    ('database_mappings', 'database_mappings_source_env_id_fkey', 'source_env_id'),
    ('database_mappings', 'database_mappings_target_env_id_fkey', 'target_env_id'),
    ('migration_jobs', 'migration_jobs_source_env_id_fkey', 'source_env_id'),
    ('migration_jobs', 'migration_jobs_target_env_id_fkey', 'target_env_id'),
    ('dataset_review_sessions', 'dataset_review_sessions_environment_id_fkey', 'environment_id'),
]


def upgrade() -> None:
    """Upgrade schema."""
    for table, constraint_name, column in FK_DEFS:
        op.drop_constraint(constraint_name, table, type_='foreignkey')
        op.create_foreign_key(
            constraint_name,
            table,
            'environments',
            [column],
            ['id'],
            ondelete='CASCADE',
        )


def downgrade() -> None:
    """Downgrade schema."""
    for table, constraint_name, column in reversed(FK_DEFS):
        op.drop_constraint(constraint_name, table, type_='foreignkey')
        op.create_foreign_key(
            constraint_name,
            table,
            'environments',
            [column],
            ['id'],
        )
