"""删除 sys_audit_log.action_type 的 CHECK 白名单约束。 Revision ID: 0035_drop_audit_action_check Revises: 0034_role_anchor_self Create Date: 2026-09-14 变更要点: 1. 审计动作是持续扩展的枚举(0009~0031 已重建该约束 8 次),DB CHECK 导致 新动作写入失败并连带业务操作 500(如库白名单落后于代码时审批单上传失败); 审计日志作为事实记录不应因枚举扩展而丢失,合法性收敛到应用层字典; 2. target_type / operation_result 的 CHECK 保留(稳定小枚举,与本次无关); 3. 模型层同步删除该 CheckConstraint(dms/models/audit_log.py,新装库不再创建)。 """ from __future__ import annotations from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "0035_drop_audit_action_check" down_revision: str | None = "0034_role_anchor_self" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None # 与 0026 一致:op.drop_constraint 按 naming convention 前缀化约束名, # 传原始名渲染最终名;线性链上 0026 已把约束统一为渲染名。 _ACTION_CONSTRAINT = "audit_action_type" def _existing_check_names() -> set[str]: return { item["name"].lower() for item in sa.inspect(op.get_bind()).get_check_constraints("sys_audit_log") } def upgrade() -> None: if f"ck_sys_audit_log_{_ACTION_CONSTRAINT}" in _existing_check_names(): op.drop_constraint(_ACTION_CONSTRAINT, "sys_audit_log", type_="check") def downgrade() -> None: raise RuntimeError( "0035降级需按当时动作全集重建 CHECK 白名单,且库中不得存在白名单外" "action_type 记录;如确需恢复约束请人工评估后执行" )