QTrade/scripts/drop_old_indicator_tables.py
cuizhan 37a5df8bf6 重构配置系统:改为Config/Logger单例模式,拆分环境配置文件
主要改动:
1. Config类改为单例模式,支持根据global.yaml的current_mode自动加载环境配置
2. Logger类改为单例模式,通过类方法直接访问
3. 拆分配置文件:global.yaml仅指定模式,backtest.yaml/simulation.yaml/production.yaml存储各环境配置
4. 移除所有config参数传递,使用Config.get()直接访问
5. 移除所有logger实例传递,使用Logger.info()等类方法直接访问
6. 修复初始化顺序:Config.initialize() -> Logger.initialize() -> 其他组件
7. 修复Logger参数默认值,确保所有方法可正常调用
8. 新增三个环境配置文件结构统一,只有参数值不同
9. 客户端URL端口修正为8030(回测)/8020(模拟)/8010(实盘)

配置文件变更:
- configs/global.yaml:简化为仅包含current_mode
- configs/backtest.yaml:回测环境完整配置
- configs/simulation.yaml:模拟环境完整配置
- configs/production.yaml:实盘环境完整配置

代码重构:
- src/core/config.py:单例模式,自动加载环境配置
- src/core/logger.py:单例模式,类方法访问
- 所有模块:移除config/logger参数,使用单例访问
2026-01-12 19:07:23 +08:00

124 lines
3.3 KiB
Python

"""
删除旧格式的指标表
旧格式: macd_1d, kdj_1d, ma_1d 等
新格式: indicators_1d_macd, indicators_1d_kdj 等
"""
import sys
import os
from pathlib import Path
# 添加项目根目录到路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from src.database_manager import DatabaseManager
from src.core.config import Config
from sqlalchemy import text
def identify_old_tables(all_tables):
"""识别旧格式的指标表"""
old_indicators = ["macd", "kdj", "boll", "rsi", "ma", "ema"]
old_periods = ["1m", "5m", "15m", "30m", "60m", "1d", "1w"]
old_tables = []
for indicator in old_indicators:
for period in old_periods:
table_name = f"{indicator}_{period}"
if table_name in all_tables:
old_tables.append(table_name)
return old_tables
def main():
print("\n" + "=" * 70)
print(" 删除旧格式的指标表")
print("=" * 70)
# 初始化配置和数据库管理器
Config.initialize()
db_manager = DatabaseManager()
database = Config.get("database.database")
print(f"\n当前数据库: {database}")
# 获取所有表
all_tables = db_manager.list_tables(database)
# 识别旧格式的表
old_tables = identify_old_tables(all_tables)
if not old_tables:
print("\n✓ 没有发现旧格式的指标表")
return
print(f"\n发现 {len(old_tables)} 个旧格式的指标表:")
for table in sorted(old_tables):
print(f" • {table}")
# 确认删除
print("\n⚠️ 警告: 这些表将被永久删除!")
print("旧格式: {indicator}_{period} (例如: macd_1d)")
print("新格式: indicators_{period}_{indicator} (例如: indicators_1d_macd)")
response = input("\n确认删除这些旧表? (输入 'YES' 确认): ").strip()
if response != "YES":
print("\n✗ 操作已取消")
return
# 删除表
print("\n开始删除...")
db_url = (
f"mysql+pymysql://{Config.get('database.user')}:"
f"{Config.get('database.password')}@"
f"{Config.get('database.host')}:{Config.get('database.port')}"
f"/{database}"
)
from sqlalchemy import create_engine
engine = create_engine(db_url, echo=False)
success_count = 0
fail_count = 0
try:
with engine.connect() as conn:
# 禁用外键检查
conn.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
for table in old_tables:
try:
conn.execute(text(f"DROP TABLE `{table}`"))
conn.commit()
print(f"✓ 删除成功: {table}")
success_count += 1
except Exception as e:
print(f"✗ 删除失败: {table} - {str(e)}")
fail_count += 1
# 恢复外键检查
conn.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
conn.commit()
finally:
engine.dispose()
print("\n" + "=" * 70)
print(f"完成: 成功删除 {success_count} 个表, 失败 {fail_count} 个")
print("=" * 70 + "\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n✗ 操作被用户中断")
sys.exit(1)
except Exception as e:
print(f"\n✗ 错误: {str(e)}")
sys.exit(1)