40 lines
993 B
Python
40 lines
993 B
Python
"""
|
|
初始化数据库表
|
|
"""
|
|
import sys
|
|
sys.path.append('.')
|
|
|
|
from app.database import Base, engine
|
|
from app.models.risk_detection import (
|
|
DetectionRule,
|
|
DetectionTask,
|
|
RuleExecution,
|
|
TaskExecution,
|
|
DetectionResult
|
|
)
|
|
import asyncio
|
|
|
|
|
|
async def init_tables():
|
|
"""创建所有数据库表"""
|
|
try:
|
|
print("正在创建数据库表...")
|
|
async with engine.begin() as conn:
|
|
# 删除所有表(仅用于开发环境)
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
# 创建所有表
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
print("✅ 数据库表创建成功!")
|
|
return True
|
|
except Exception as e:
|
|
print(f"❌ 创建数据库表失败: {e}")
|
|
print(f"错误详情: {type(e).__name__}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = asyncio.run(init_tables())
|
|
sys.exit(0 if success else 1)
|