[backend] fix: 在get_db中检查并创建表结构
This commit is contained in:
@@ -1,14 +1,23 @@
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||||
from sqlalchemy.orm import declarative_base
|
from sqlalchemy.orm import declarative_base
|
||||||
from .settings import get_settings
|
from .settings import get_settings
|
||||||
|
import os
|
||||||
|
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
|
||||||
DATABASE_URL = (
|
# 检测是否使用测试环境(SQLite)
|
||||||
|
IS_TEST = settings.DEBUG or os.getenv("USE_SQLITE", "false").lower() == "true"
|
||||||
|
|
||||||
|
if IS_TEST:
|
||||||
|
# 测试环境使用SQLite
|
||||||
|
DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||||
|
else:
|
||||||
|
# 生产环境使用MySQL
|
||||||
|
DATABASE_URL = (
|
||||||
f"mysql+aiomysql://{settings.DB_USER}:{settings.DB_PASSWORD}"
|
f"mysql+aiomysql://{settings.DB_USER}:{settings.DB_PASSWORD}"
|
||||||
f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
|
f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
|
||||||
f"?charset={settings.DB_CHARSET}"
|
f"?charset={settings.DB_CHARSET}"
|
||||||
)
|
)
|
||||||
|
|
||||||
engine = create_async_engine(DATABASE_URL, echo=settings.DEBUG, future=True)
|
engine = create_async_engine(DATABASE_URL, echo=settings.DEBUG, future=True)
|
||||||
|
|
||||||
@@ -24,9 +33,26 @@ async def get_db():
|
|||||||
|
|
||||||
用于FastAPI依赖注入,自动管理数据库会话的生命周期
|
用于FastAPI依赖注入,自动管理数据库会话的生命周期
|
||||||
成功时自动提交,异常时自动回滚,最后确保关闭会话
|
成功时自动提交,异常时自动回滚,最后确保关闭会话
|
||||||
|
|
||||||
|
第一次调用时自动创建表结构
|
||||||
"""
|
"""
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
try:
|
try:
|
||||||
|
# 检查表是否存在,如果不存在则创建
|
||||||
|
from sqlalchemy import inspect, text
|
||||||
|
from src.models.user import User
|
||||||
|
from src.models.project import Project
|
||||||
|
|
||||||
|
inspector = inspect(engine)
|
||||||
|
existing_tables = inspector.get_table_names()
|
||||||
|
|
||||||
|
if "users" not in existing_tables:
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all, tables=[User.__tablename__])
|
||||||
|
elif "projects" not in existing_tables:
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all, tables=[Project.__tablename__])
|
||||||
|
|
||||||
yield session
|
yield session
|
||||||
await session.commit()
|
await session.commit()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user