49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""迁移 021: 为保司和产品表新增脱敏展示名字段。"""
|
|
import logging
|
|
from sqlalchemy import text
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _column_exists(db, table: str, column: str) -> bool:
|
|
"""检查列是否存在。"""
|
|
try:
|
|
result = db.session.execute(text(
|
|
"SELECT COUNT(*) FROM information_schema.COLUMNS "
|
|
"WHERE TABLE_NAME = :table AND COLUMN_NAME = :column"
|
|
), {"table": table, "column": column})
|
|
return result.scalar() > 0
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def migrate():
|
|
"""执行迁移。"""
|
|
from insurance.db.compat import db
|
|
|
|
if not _column_exists(db, "insurance_ppt_companies", "masked_display_name"):
|
|
db.session.execute(text(
|
|
"ALTER TABLE insurance_ppt_companies ADD COLUMN masked_display_name VARCHAR(100)"
|
|
))
|
|
logger.info("[migrate_021] 已为 insurance_ppt_companies 添加 masked_display_name 列")
|
|
else:
|
|
logger.info("[migrate_021] insurance_ppt_companies.masked_display_name 已存在,跳过")
|
|
|
|
if not _column_exists(db, "insurance_ppt_products", "masked_display_name"):
|
|
db.session.execute(text(
|
|
"ALTER TABLE insurance_ppt_products ADD COLUMN masked_display_name VARCHAR(100)"
|
|
))
|
|
logger.info("[migrate_021] 已为 insurance_ppt_products 添加 masked_display_name 列")
|
|
else:
|
|
logger.info("[migrate_021] insurance_ppt_products.masked_display_name 已存在,跳过")
|
|
|
|
if not _column_exists(db, "poster_records", "extra_data"):
|
|
db.session.execute(text(
|
|
"ALTER TABLE poster_records ADD COLUMN extra_data TEXT"
|
|
))
|
|
logger.info("[migrate_021] 已为 poster_records 添加 extra_data 列")
|
|
else:
|
|
logger.info("[migrate_021] poster_records.extra_data 已存在,跳过")
|
|
|
|
db.session.commit()
|