37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
"""迁移 018: 为 PPT 解析任务增加进度字段。"""
|
|
import logging
|
|
from sqlalchemy import text
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def migrate():
|
|
"""执行迁移。"""
|
|
from insurance.db.compat import db
|
|
|
|
columns = {
|
|
"parse_progress": "INTEGER DEFAULT 0 NOT NULL",
|
|
"parse_message": "TEXT",
|
|
"parse_error": "TEXT",
|
|
"parse_started_at": "TIMESTAMP",
|
|
"parse_finished_at": "TIMESTAMP",
|
|
}
|
|
|
|
for column_name, column_type in columns.items():
|
|
if _column_exists(db, "insurance_ppt_sessions", column_name):
|
|
logger.info(f"[migrate_018] {column_name} 已存在,跳过")
|
|
continue
|
|
db.session.execute(text(
|
|
f"ALTER TABLE insurance_ppt_sessions ADD COLUMN {column_name} {column_type}"
|
|
))
|
|
db.session.commit()
|
|
logger.info(f"[migrate_018] 已添加 {column_name} 列")
|
|
|
|
|
|
def _column_exists(db, table_name: str, column_name: str) -> bool:
|
|
result = db.session.execute(text(
|
|
"SELECT COUNT(*) FROM information_schema.columns "
|
|
"WHERE table_name = :table_name AND column_name = :column_name"
|
|
), {"table_name": table_name, "column_name": column_name})
|
|
return result.scalar() > 0
|