"""remove all database foreign keys

Revision ID: 20260813_14
Revises: 20260812_13
Create Date: 2026-08-13
"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "20260813_14"
down_revision: str | None = "20260812_13"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Drop every FK present in the current database, regardless of generated name."""
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    for table_name in inspector.get_table_names():
        for constraint in inspector.get_foreign_keys(table_name):
            name = constraint.get("name")
            if not name:
                raise RuntimeError(f"无法删除未命名外键: table={table_name}")
            op.drop_constraint(name, table_name, type_="foreignkey")


def downgrade() -> None:
    # Forward-only policy: this project must never recreate database FKs.
    pass
