Add backend api and engine

This commit is contained in:
Xin Wang
2026-02-06 14:01:34 +08:00
parent 590014e821
commit d5c1ab34b3
61 changed files with 10351 additions and 1 deletions

52
api/init_db.py Normal file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""初始化数据库"""
import os
import sys
# 添加路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.db import Base, engine
from app.models import Voice
def init_db():
"""创建所有表"""
print("📦 创建数据库表...")
Base.metadata.drop_all(bind=engine) # 删除旧表
Base.metadata.create_all(bind=engine)
print("✅ 数据库表创建完成")
def init_default_voices():
"""初始化默认声音"""
from app.db import SessionLocal
db = SessionLocal()
try:
if db.query(Voice).count() == 0:
voices = [
Voice(id="v1", name="Xiaoyun", vendor="Ali", gender="Female", language="zh", description="Gentle and professional."),
Voice(id="v2", name="Kevin", vendor="Volcano", gender="Male", language="en", description="Deep and authoritative."),
Voice(id="v3", name="Abby", vendor="Minimax", gender="Female", language="en", description="Cheerful and lively."),
Voice(id="v4", name="Guang", vendor="Ali", gender="Male", language="zh", description="Standard newscast style."),
Voice(id="v5", name="Doubao", vendor="Volcano", gender="Female", language="zh", description="Cute and young."),
]
for v in voices:
db.add(v)
db.commit()
print("✅ 默认声音数据已初始化")
else:
print(" 声音数据已存在,跳过初始化")
finally:
db.close()
if __name__ == "__main__":
# 确保 data 目录存在
data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data")
os.makedirs(data_dir, exist_ok=True)
init_db()
init_default_voices()
print("🎉 数据库初始化完成!")