46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from sqlalchemy import text
|
||
|
|
|
||
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
|
|
|
||
|
|
from app.db.session import SessionLocal
|
||
|
|
from scripts.init_demo_db import init_database
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> None:
|
||
|
|
"""新表迁移:创建并补齐 case_base、traditional_case、teaching_case、case_exam_item、training_* 和 training_record。"""
|
||
|
|
init_database()
|
||
|
|
with SessionLocal() as db:
|
||
|
|
_apply_table_comments(db)
|
||
|
|
db.commit()
|
||
|
|
print("new schema migration completed")
|
||
|
|
|
||
|
|
|
||
|
|
def _apply_table_comments(db) -> None:
|
||
|
|
"""表注释补齐:为当前业务表写入中文说明,便于数据库工具查看。"""
|
||
|
|
comments = {
|
||
|
|
"case_base": "病例主表",
|
||
|
|
"traditional_case": "传统病例扩展表",
|
||
|
|
"teaching_case": "教学互动病例扩展表",
|
||
|
|
"scoring_rule": "评分规则表",
|
||
|
|
"case_exam_item": "病例检查检验项目表",
|
||
|
|
"training_session": "训练会话表",
|
||
|
|
"training_order": "训练检查申请表",
|
||
|
|
"training_submission": "训练诊断治疗提交表",
|
||
|
|
"training_record": "训练记录表",
|
||
|
|
}
|
||
|
|
dialect = db.bind.dialect.name if db.bind else ""
|
||
|
|
if dialect != "mysql":
|
||
|
|
return
|
||
|
|
for table_name, comment in comments.items():
|
||
|
|
safe_comment = comment.replace("'", "''")
|
||
|
|
db.execute(text(f"ALTER TABLE `{table_name}` COMMENT='{safe_comment}'"))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|