53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
清理MySQL数据库,重新开始迁移
|
||
"""
|
||
|
||
import pymysql
|
||
import sys
|
||
from pathlib import Path
|
||
from loguru import logger
|
||
|
||
# 添加项目根目录到路径
|
||
current_dir = Path(__file__).parent
|
||
sys.path.insert(0, str(current_dir))
|
||
|
||
from config.mysql_config import MYSQL_CONFIG
|
||
|
||
|
||
def clean_mysql_database():
|
||
"""清理MySQL数据库"""
|
||
logger.info("🧹 清理MySQL数据库...")
|
||
|
||
try:
|
||
with pymysql.connect(**MYSQL_CONFIG.to_dict()) as conn:
|
||
cursor = conn.cursor()
|
||
|
||
# 删除视图
|
||
try:
|
||
cursor.execute("DROP VIEW IF EXISTS latest_signals_view")
|
||
cursor.execute("DROP VIEW IF EXISTS strategy_stats_view")
|
||
logger.info("✅ 删除视图")
|
||
except Exception as e:
|
||
logger.warning(f"删除视图警告: {e}")
|
||
|
||
# 删除表(注意外键约束顺序)
|
||
tables = ['pullback_alerts', 'stock_signals', 'scan_sessions', 'strategies']
|
||
|
||
for table in tables:
|
||
try:
|
||
cursor.execute(f"DROP TABLE IF EXISTS {table}")
|
||
logger.info(f"✅ 删除表: {table}")
|
||
except Exception as e:
|
||
logger.warning(f"删除表 {table} 警告: {e}")
|
||
|
||
conn.commit()
|
||
logger.info("✅ MySQL数据库清理完成")
|
||
|
||
except Exception as e:
|
||
logger.error(f"❌ 清理MySQL数据库失败: {e}")
|
||
raise
|
||
|
||
|
||
if __name__ == "__main__":
|
||
clean_mysql_database() |