"""drop upstream action audit foreign keys

Revision ID: 040_upstream_action_no_fks
Revises: 039_upstream_actions
Create Date: 2026-06-17
"""
import sqlalchemy as sa
from alembic import op


revision = "040_upstream_action_no_fks"
down_revision = "039_upstream_actions"
branch_labels = None
depends_on = None


def upgrade() -> None:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    tables = set(inspector.get_table_names())
    if "upstream_action_logs" not in tables:
        return

    for fk in inspector.get_foreign_keys("upstream_action_logs"):
        name = fk.get("name")
        if name:
            op.drop_constraint(name, "upstream_action_logs", type_="foreignkey")


def downgrade() -> None:
    # Audit rows are intentionally weakly linked by snapshot IDs. Recreating
    # constraints would risk failing on historical rows whose parent records
    # have since been removed, so downgrade is a no-op.
    return
