baodan/api/insurance/db/migrate_004.py

75 lines
2.2 KiB
Python
Raw Normal View History

"""迁移脚本 004创建方案模板表
使用方式
python -m insurance.db.migrate_004
"""
from sqlalchemy import text
from insurance.db.compat import db
def migrate():
"""执行迁移:创建方案模板表。"""
print("[migrate_004] 开始执行迁移...")
try:
# 检查表是否已存在
result = db.session.execute(text("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'insurance_proposal_templates'
)
"""))
if result.scalar():
print("[migrate_004] 表已存在,跳过")
return True
# 创建方案模板表
print("[migrate_004] 创建 insurance_proposal_templates 表")
db.session.execute(text("""
CREATE TABLE insurance_proposal_templates (
id SERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
description VARCHAR(256) DEFAULT '',
insurance_type VARCHAR(64) NOT NULL,
file_path VARCHAR(512) DEFAULT '',
file_type VARCHAR(32) DEFAULT 'pdf',
placeholders TEXT DEFAULT '[]',
is_default BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 添加注释
db.session.execute(text("""
COMMENT ON TABLE insurance_proposal_templates IS '方案模板表'
"""))
db.session.execute(text("""
COMMENT ON COLUMN insurance_proposal_templates.insurance_type IS '险种类型'
"""))
db.session.execute(text("""
COMMENT ON COLUMN insurance_proposal_templates.placeholders IS '占位符配置JSON'
"""))
db.session.commit()
print("[migrate_004] 迁移完成!")
return True
except Exception as e:
db.session.rollback()
print(f"[migrate_004] 迁移失败: {e}")
return False
if __name__ == "__main__":
from flask import Flask
from configs.app_config import AppConfig
app = Flask(__name__)
app.config.from_object(AppConfig)
with app.app_context():
migrate()