first commit
This commit is contained in:
74
reset_database.py
Normal file
74
reset_database.py
Normal file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
TSP助手数据库重置脚本
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from src.config.config import Config
|
||||
from src.utils.helpers import setup_logging
|
||||
from src.core.database import db_manager
|
||||
from src.core.models import Base
|
||||
|
||||
def reset_database():
|
||||
"""重置数据库"""
|
||||
print("=" * 50)
|
||||
print("TSP助手数据库重置")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
# 设置日志
|
||||
setup_logging(Config.LOG_LEVEL, Config.LOG_FILE)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 确认操作
|
||||
confirm = input("⚠️ 警告:此操作将删除所有数据!确定要继续吗?(y/N): ")
|
||||
if confirm.lower() != 'y':
|
||||
print("操作已取消")
|
||||
return False
|
||||
|
||||
# 删除所有表
|
||||
Base.metadata.drop_all(bind=db_manager.engine)
|
||||
print("✓ 数据库表删除成功")
|
||||
|
||||
# 重新创建所有表
|
||||
Base.metadata.create_all(bind=db_manager.engine)
|
||||
print("✓ 数据库表重新创建成功")
|
||||
|
||||
# 插入初始数据
|
||||
from init_database import insert_initial_data
|
||||
insert_initial_data()
|
||||
|
||||
print("✓ 数据库重置完成")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ 数据库重置失败: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("TSP助手数据库重置工具")
|
||||
print("=" * 50)
|
||||
print("⚠️ 注意:此操作将删除所有现有数据!")
|
||||
print("=" * 50)
|
||||
|
||||
if reset_database():
|
||||
print("\n" + "=" * 50)
|
||||
print("数据库重置成功!")
|
||||
print("=" * 50)
|
||||
print("现在您可以运行以下命令启动系统:")
|
||||
print("python start.py")
|
||||
else:
|
||||
print("\n" + "=" * 50)
|
||||
print("数据库重置失败!")
|
||||
print("=" * 50)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user