- flask_app.py 1166行→33行纯入口
- 新建 db.py(配置+连接+SQL工具)
- 新建 helpers.py(attach_common/monthly_finance/add_file_index)
- 新建 routes.py(全路由 Blueprint + 装饰器 + TABLES)
- 新建 migrations/seed_data.py(seed_db 搬迁)
- migrations/{tables,columns,data_fixes,seed}.py 改 import 为 from db
- 删除死代码 init_db(228行)+ latest_followup(10行)
- 反向依赖消除:migrations 不再 import flask_app
- 前端零改动,URL 不变
74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
"""migrations/data_fixes.py — 数据修正迁移(修复脏数据、变更枚举值)"""
|
|
|
|
|
|
def migrate_fix_task_status():
|
|
"""修正 project_tasks 中非法的 status 值"""
|
|
from db import db, _exec, mysql, logger
|
|
|
|
conn = db()
|
|
try:
|
|
fixes = [
|
|
"UPDATE project_tasks SET status='未开始' WHERE status='' OR status IS NULL",
|
|
"UPDATE project_tasks SET status='已结束' WHERE status='done'",
|
|
"UPDATE project_tasks SET status='进行中' WHERE status='验收中'",
|
|
]
|
|
for sql in fixes:
|
|
try:
|
|
cur = _exec(conn, sql)
|
|
affected = cur.rowcount
|
|
cur.close()
|
|
if affected:
|
|
print(f"[migrate] 修正 {affected} 条任务状态")
|
|
except mysql.connector.Error as e:
|
|
logger.warning(f"task status fix skipped: {e}")
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def migrate_rename_tenant():
|
|
"""工作台重命名:无界·无界 → 学会·无界"""
|
|
from db import db, _exec, mysql
|
|
|
|
conn = db()
|
|
try:
|
|
tables = ["user_tenants", "sales_leads", "follow_up_records", "business_proposals",
|
|
"operation_projects", "product_versions", "finance_records", "project_tasks",
|
|
"project_finances"]
|
|
for table in tables:
|
|
try:
|
|
cur = _exec(conn, f"UPDATE {table} SET tenant='学会·无界' WHERE tenant='无界·无界'")
|
|
affected = cur.rowcount
|
|
cur.close()
|
|
if affected:
|
|
print(f"[migrate] {table}: {affected} 条记录 tenant 改为 '学会·无界'")
|
|
except mysql.connector.Error:
|
|
pass
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def migrate_drop_product_fields():
|
|
"""删除 product_versions 表的 owner / platform / feature_list 字段"""
|
|
from db import db, mysql
|
|
|
|
conn = db()
|
|
try:
|
|
for col in ["owner", "platform", "feature_list"]:
|
|
cur = conn.cursor(dictionary=True)
|
|
cur.execute("SHOW COLUMNS FROM product_versions LIKE %s", (col,))
|
|
exists = cur.fetchone()
|
|
cur.close()
|
|
if exists:
|
|
try:
|
|
cur = conn.cursor()
|
|
cur.execute(f"ALTER TABLE product_versions DROP COLUMN {col}")
|
|
cur.close()
|
|
conn.commit()
|
|
print(f"[migrate] product_versions.{col} 列已删除")
|
|
except mysql.connector.Error as e:
|
|
print(f"[migrate] 删除 {col} 失败: {e}")
|
|
finally:
|
|
conn.close()
|