baodan/api/insurance/db/migrate_003.py

84 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""迁移脚本 003创建Prompt模板表
使用方式:
python -m insurance.db.migrate_003
"""
from sqlalchemy import text
from insurance.db.compat import db
def migrate():
"""执行迁移创建Prompt模板表。"""
print("[migrate_003] 开始执行迁移...")
try:
# 检查表是否已存在
result = db.session.execute(text("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'insurance_prompt_templates'
)
"""))
if result.scalar():
print("[migrate_003] 表已存在,跳过")
return True
# 创建Prompt模板表
print("[migrate_003] 创建 insurance_prompt_templates 表")
db.session.execute(text("""
CREATE TABLE insurance_prompt_templates (
id SERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
description VARCHAR(256) DEFAULT '',
content TEXT DEFAULT '',
variables TEXT DEFAULT '[]',
category VARCHAR(64) DEFAULT 'general',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 创建Prompt版本历史表
print("[migrate_003] 创建 insurance_prompt_versions 表")
db.session.execute(text("""
CREATE TABLE insurance_prompt_versions (
id SERIAL PRIMARY KEY,
prompt_id INTEGER NOT NULL,
version INTEGER NOT NULL,
content TEXT DEFAULT '',
variables TEXT DEFAULT '[]',
change_note VARCHAR(256) DEFAULT '',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 添加注释
db.session.execute(text("""
COMMENT ON TABLE insurance_prompt_templates IS 'Prompt模板表'
"""))
db.session.execute(text("""
COMMENT ON TABLE insurance_prompt_versions IS 'Prompt版本历史表'
"""))
db.session.commit()
print("[migrate_003] 迁移完成!")
return True
except Exception as e:
db.session.rollback()
print(f"[migrate_003] 迁移失败: {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()