45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
|
|
"""迁移 037:PlanData、证据与不可变确认快照。"""
|
|||
|
|
import logging
|
|||
|
|
|
|||
|
|
from sqlalchemy import inspect, text
|
|||
|
|
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _add_columns_if_missing(table_name: str, columns: dict[str, str]) -> None:
|
|||
|
|
"""一次读取现有列后完成本表变更,避免未提交 DDL 与二次反射互锁。"""
|
|||
|
|
from insurance.db.compat import db
|
|||
|
|
|
|||
|
|
existing = {item["name"] for item in inspect(db.engine).get_columns(table_name)}
|
|||
|
|
for column_name, ddl in columns.items():
|
|||
|
|
if column_name not in existing:
|
|||
|
|
db.session.execute(text(
|
|||
|
|
f"ALTER TABLE {table_name} ADD COLUMN {column_name} {ddl}"
|
|||
|
|
))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def migrate():
|
|||
|
|
from insurance.db.compat import db
|
|||
|
|
from insurance.models.plan_snapshot import (
|
|||
|
|
InsuranceFieldEvidence,
|
|||
|
|
InsurancePlanOverride,
|
|||
|
|
InsurancePlanSnapshot,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
for model in (InsurancePlanSnapshot, InsuranceFieldEvidence, InsurancePlanOverride):
|
|||
|
|
model.__table__.create(bind=db.engine, checkfirst=True)
|
|||
|
|
|
|||
|
|
inspector = inspect(db.engine)
|
|||
|
|
tables = set(inspector.get_table_names())
|
|||
|
|
if "poster_case_uploads" in tables:
|
|||
|
|
_add_columns_if_missing("poster_case_uploads", {
|
|||
|
|
"document_id": "BIGINT",
|
|||
|
|
"snapshot_id": "BIGINT",
|
|||
|
|
})
|
|||
|
|
if "insurance_ppt_sessions" in tables:
|
|||
|
|
_add_columns_if_missing("insurance_ppt_sessions", {
|
|||
|
|
"snapshot_ids_json": "TEXT",
|
|||
|
|
})
|
|||
|
|
db.session.commit()
|
|||
|
|
logger.info("[migrate_037] PlanData 快照、证据与人工覆盖迁移完成")
|