73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
|
|
"""迁移脚本 006:创建部门表
|
|||
|
|
|
|||
|
|
使用方式:
|
|||
|
|
python -m insurance.db.migrate_006
|
|||
|
|
"""
|
|||
|
|
from sqlalchemy import text
|
|||
|
|
from insurance.db.compat import db
|
|||
|
|
|
|||
|
|
|
|||
|
|
def migrate():
|
|||
|
|
"""执行迁移:创建部门表。"""
|
|||
|
|
print("[migrate_006] 开始执行迁移...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 检查表是否已存在
|
|||
|
|
result = db.session.execute(text("""
|
|||
|
|
SELECT EXISTS (
|
|||
|
|
SELECT FROM information_schema.tables
|
|||
|
|
WHERE table_name = 'insurance_departments'
|
|||
|
|
)
|
|||
|
|
"""))
|
|||
|
|
|
|||
|
|
if result.scalar():
|
|||
|
|
print("[migrate_006] 表已存在,跳过")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
# 创建部门表
|
|||
|
|
print("[migrate_006] 创建 insurance_departments 表")
|
|||
|
|
db.session.execute(text("""
|
|||
|
|
CREATE TABLE insurance_departments (
|
|||
|
|
id SERIAL PRIMARY KEY,
|
|||
|
|
name VARCHAR(128) NOT NULL,
|
|||
|
|
code VARCHAR(64) UNIQUE NOT NULL,
|
|||
|
|
parent_id INTEGER DEFAULT 0,
|
|||
|
|
description VARCHAR(256) DEFAULT '',
|
|||
|
|
sort_order INTEGER DEFAULT 0,
|
|||
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|||
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|||
|
|
)
|
|||
|
|
"""))
|
|||
|
|
|
|||
|
|
# 添加注释
|
|||
|
|
db.session.execute(text("""
|
|||
|
|
COMMENT ON TABLE insurance_departments IS '部门/分组表'
|
|||
|
|
"""))
|
|||
|
|
db.session.execute(text("""
|
|||
|
|
COMMENT ON COLUMN insurance_departments.code IS '部门编码'
|
|||
|
|
"""))
|
|||
|
|
db.session.execute(text("""
|
|||
|
|
COMMENT ON COLUMN insurance_departments.parent_id IS '上级部门ID,0表示顶级'
|
|||
|
|
"""))
|
|||
|
|
|
|||
|
|
db.session.commit()
|
|||
|
|
|
|||
|
|
print("[migrate_006] 迁移完成!")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
db.session.rollback()
|
|||
|
|
print(f"[migrate_006] 迁移失败: {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()
|