baodan/api/insurance/db/migrate_005.py

85 lines
2.6 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.

"""迁移脚本 005创建通知告警配置表
使用方式:
python -m insurance.db.migrate_005
"""
from sqlalchemy import text
from insurance.db.compat import db
def migrate():
"""执行迁移:创建通知告警配置表。"""
print("[migrate_005] 开始执行迁移...")
try:
# 检查表是否已存在
result = db.session.execute(text("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'insurance_notification_channels'
)
"""))
if result.scalar():
print("[migrate_005] 表已存在,跳过")
return True
# 创建通知渠道表
print("[migrate_005] 创建 insurance_notification_channels 表")
db.session.execute(text("""
CREATE TABLE insurance_notification_channels (
id SERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
type VARCHAR(32) NOT NULL,
config TEXT DEFAULT '{}',
enabled BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 创建告警规则表
print("[migrate_005] 创建 insurance_alert_rules 表")
db.session.execute(text("""
CREATE TABLE insurance_alert_rules (
id SERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
description VARCHAR(256) DEFAULT '',
condition_type VARCHAR(64) NOT NULL,
condition_config TEXT DEFAULT '{}',
channel_id INTEGER,
enabled BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# 添加注释
db.session.execute(text("""
COMMENT ON TABLE insurance_notification_channels IS '通知渠道配置表'
"""))
db.session.execute(text("""
COMMENT ON TABLE insurance_alert_rules IS '告警规则配置表'
"""))
db.session.commit()
print("[migrate_005] 迁移完成!")
return True
except Exception as e:
db.session.rollback()
print(f"[migrate_005] 迁移失败: {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()