2025-12-23 17:56:01 +08:00
|
|
|
"""
|
|
|
|
|
删除旧格式的指标表
|
|
|
|
|
旧格式: 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
|
2026-01-12 19:07:23 +08:00
|
|
|
from src.core.config import Config
|
2025-12-23 17:56:01 +08:00
|
|
|
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)
|
|
|
|
|
|
2026-01-12 19:07:23 +08:00
|
|
|
# 初始化配置和数据库管理器
|
|
|
|
|
Config.initialize()
|
2025-12-23 17:56:01 +08:00
|
|
|
db_manager = DatabaseManager()
|
2026-01-12 19:07:23 +08:00
|
|
|
database = Config.get("database.database")
|
2025-12-23 17:56:01 +08:00
|
|
|
|
|
|
|
|
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 = (
|
2026-01-12 19:07:23 +08:00
|
|
|
f"mysql+pymysql://{Config.get('database.user')}:"
|
|
|
|
|
f"{Config.get('database.password')}@"
|
|
|
|
|
f"{Config.get('database.host')}:{Config.get('database.port')}"
|
2025-12-23 17:56:01 +08:00
|
|
|
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)
|