37 lines
1011 B
Python
37 lines
1011 B
Python
"""
|
|
数据库初始化 Python 脚本
|
|
使用 SQLAlchemy 创建表结构
|
|
"""
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# 添加项目根目录到 Python 路径
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from sqlalchemy import create_engine
|
|
from app.core.config import settings
|
|
from app.core.database import Base
|
|
from app.models import * # 导入所有模型
|
|
from app.core.security import get_password_hash
|
|
|
|
|
|
def init_database():
|
|
"""初始化数据库"""
|
|
print("开始初始化数据库...")
|
|
|
|
# 创建同步引擎(用于表创建)
|
|
engine = create_engine(settings.SYNC_DATABASE_URL, echo=True)
|
|
|
|
# 创建所有表
|
|
print("创建数据库表...")
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
print("✓ 数据库表创建完成")
|
|
print("\n请执行 SQL 脚本插入初始数据:")
|
|
print(f" mysql -h{settings.DB_HOST} -u{settings.DB_USER} -p{settings.DB_PASSWORD} {settings.DB_NAME} < scripts/init_database.sql")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
init_database()
|