refactor: 数据层重构 — 统一 SQLite 访问层 + 多脚本双写
新建 mofin_db.py 共享数据库模块: - get_conn() 统一连接管理 (WAL + Row factory + 外键) - init_all_tables() 幂等建表 (12张表: market/sector/stock/kline/fundamentals/sectors/holdings/strategies/watchlist/candidates/score_history/events/evaluations) - write_market_snapshot() 市场快照双写 - write_klines() K线数据双写 (stocks + daily/weekly/monthly + fundamentals) - write_price_event() 价格事件双写 - migrate_stock_sectors() 一次性迁移 stock_sector_map.json - query_*() 通用查询函数 (sector_trend/top_inflow/consecutive_inflow/market_mood/db_stats) 重构现有脚本: - market_watch.py: 删除内联 DB 代码,改用 mofin_db - multi_timeframe.py: _save_local_history() 加 SQLite 双写 - price_monitor.py: record_event() 加 SQLite 双写 - mofin_query.py: 改用 mofin_db 查询函数 新增: - migrate_sectors.py: 一次性迁移脚本 清理: - get_realtime_prices.py: 死代码 (只读 portfolio.json,不调API)
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
"""一次性迁移脚本:stock_sector_map.json → stock_sectors 表
|
||||
|
||||
运行方式:
|
||||
python3 migrate_sectors.py
|
||||
"""
|
||||
from mofin_db import get_conn, init_all_tables, migrate_stock_sectors
|
||||
|
||||
conn = get_conn()
|
||||
init_all_tables(conn)
|
||||
stocks, mappings = migrate_stock_sectors(conn)
|
||||
print(f"迁移完成: {stocks} 只股票, {mappings} 条板块映射")
|
||||
|
||||
# 验证
|
||||
rows = conn.execute("SELECT COUNT(*) FROM stock_sectors").fetchone()[0]
|
||||
print(f"stock_sectors 表总行数: {rows}")
|
||||
|
||||
# 样例
|
||||
for r in conn.execute("SELECT * FROM stock_sectors LIMIT 5").fetchall():
|
||||
print(f" {r[0]} → {r[1]}")
|
||||
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user