41 lines
1.0 KiB
Python
Executable File
41 lines
1.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""
|
||
数据库初始化脚本
|
||
用于创建数据库表和运行初始迁移
|
||
"""
|
||
|
||
import asyncio
|
||
import sys
|
||
import os
|
||
|
||
# 添加项目根目录到Python路径
|
||
sys.path.append(os.path.dirname(__file__))
|
||
|
||
from app.core.database import init_db, close_db
|
||
from app.core.config import settings
|
||
|
||
|
||
async def main():
|
||
"""主函数:初始化数据库"""
|
||
try:
|
||
print(f"正在连接数据库: {settings.DATABASE_URL}")
|
||
print("正在创建数据库表...")
|
||
|
||
# 初始化数据库表
|
||
await init_db()
|
||
|
||
print("✅ 数据库表创建成功!")
|
||
print("💡 提示: 如果需要使用Alembic管理迁移,请运行:")
|
||
print(" alembic stamp head")
|
||
print(" alembic revision --autogenerate -m '描述'")
|
||
print(" alembic upgrade head")
|
||
|
||
except Exception as e:
|
||
print(f"❌ 数据库初始化失败: {e}")
|
||
sys.exit(1)
|
||
finally:
|
||
await close_db()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main()) |