diff --git a/deploy/profile-scripts/schema_migrate_20260817.py b/deploy/profile-scripts/schema_migrate_20260817.py new file mode 100644 index 00000000..a125063a --- /dev/null +++ b/deploy/profile-scripts/schema_migrate_20260817.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""schema_migrate_20260817.py — 2026-08-17 数据库迁移 +新增列(幂等,已存在则跳过): + 1. candidates.source TEXT — 资金流突变候选来源(fund_flow_alert 写) + 2. candidates.source_strategy TEXT — 选股来源策略(4 scanner 写,老莫要求) + 3. holding_strategies.strategy_name TEXT — 策略名(promote 继承候选) + 4. strategy_history.strategy_name TEXT — 重评快照策略名 +""" +import sqlite3 + +DB = "/home/hmo/MoFin/data/mofin.db" + +MIGRATIONS = [ + ("candidates", "source", "TEXT"), + ("candidates", "source_strategy", "TEXT"), + ("holding_strategies", "strategy_name", "TEXT"), + ("strategy_history", "strategy_name", "TEXT"), +] + +def main(): + conn = sqlite3.connect(DB, timeout=30) + conn.execute("PRAGMA busy_timeout=30000") + done = 0 + for table, col, ctype in MIGRATIONS: + cols = [c[1] for c in conn.execute(f"PRAGMA table_info({table})").fetchall()] + if col not in cols: + conn.execute(f"ALTER TABLE {table} ADD COLUMN {col} {ctype}") + print(f" ✅ {table}.{col} 已加") + done += 1 + else: + print(f" ⏭ {table}.{col} 已存在") + conn.commit() + conn.close() + print(f"\n迁移完成: {done} 列新增") + +if __name__ == "__main__": + main() \ No newline at end of file