- 从服务器拉取完整代码 - 按框架规范整理项目结构 - 配置 Drone CI 测试环境部署 - 包含后端(FastAPI)、前端(Vue3)、管理端 技术栈: Vue3 + TypeScript + FastAPI + MySQL
94 lines
2.7 KiB
Python
94 lines
2.7 KiB
Python
"""
|
||
执行陪练场景表创建和初始数据插入脚本
|
||
"""
|
||
import asyncio
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# 添加项目根目录到Python路径
|
||
project_root = Path(__file__).parent.parent
|
||
sys.path.insert(0, str(project_root))
|
||
|
||
from sqlalchemy import text
|
||
from app.core.database import async_engine
|
||
|
||
|
||
async def run_sql_file():
|
||
"""执行SQL文件"""
|
||
sql_file = project_root / "scripts" / "create_practice_scenes.sql"
|
||
|
||
if not sql_file.exists():
|
||
print(f"❌ SQL文件不存在: {sql_file}")
|
||
return False
|
||
|
||
# 读取SQL文件
|
||
with open(sql_file, 'r', encoding='utf-8') as f:
|
||
sql_content = f.read()
|
||
|
||
# 分割SQL语句(按分号分隔)
|
||
statements = [s.strip() for s in sql_content.split(';') if s.strip() and not s.strip().startswith('--')]
|
||
|
||
print(f"📝 准备执行 {len(statements)} 条SQL语句...")
|
||
|
||
async with async_engine.begin() as conn:
|
||
for i, statement in enumerate(statements, 1):
|
||
if not statement:
|
||
continue
|
||
try:
|
||
# 跳过注释
|
||
if statement.strip().startswith('--'):
|
||
continue
|
||
|
||
print(f" [{i}/{len(statements)}] 执行中...")
|
||
result = await conn.execute(text(statement))
|
||
|
||
# 如果是SELECT语句,打印结果
|
||
if statement.strip().upper().startswith('SELECT'):
|
||
rows = result.fetchall()
|
||
print(f" ✅ 查询返回 {len(rows)} 行数据")
|
||
for row in rows:
|
||
print(f" {row}")
|
||
else:
|
||
print(f" ✅ 执行成功")
|
||
|
||
except Exception as e:
|
||
print(f" ❌ 执行失败: {e}")
|
||
if "already exists" not in str(e).lower() and "duplicate" not in str(e).lower():
|
||
raise
|
||
|
||
print("\n✅ 所有SQL语句执行完成!")
|
||
return True
|
||
|
||
|
||
async def main():
|
||
"""主函数"""
|
||
try:
|
||
print("=" * 60)
|
||
print("陪练场景表创建和初始数据插入")
|
||
print("=" * 60)
|
||
|
||
success = await run_sql_file()
|
||
|
||
if success:
|
||
print("\n" + "=" * 60)
|
||
print("✅ 陪练场景表创建成功!")
|
||
print("=" * 60)
|
||
else:
|
||
print("\n" + "=" * 60)
|
||
print("❌ 执行失败")
|
||
print("=" * 60)
|
||
sys.exit(1)
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ 发生错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
sys.exit(1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|
||
|
||
|
||
|