fix: 修复数据库初始化顺序问题

- docker-compose 启动时先运行 alembic upgrade head
- 优化数据库表不存在时的错误提示
This commit is contained in:
lintsinghua 2025-12-05 19:31:47 +08:00
parent d9cd0d73f9
commit db3d8fd9f8
2 changed files with 8 additions and 2 deletions

View File

@ -22,12 +22,18 @@ async def lifespan(app: FastAPI):
logger.info("XCodeReviewer 后端服务启动中...")
# 初始化数据库(创建默认账户)
# 注意:需要先运行 alembic upgrade head 创建表结构
try:
async with AsyncSessionLocal() as db:
await init_db(db)
logger.info("✓ 数据库初始化完成")
except Exception as e:
logger.warning(f"数据库初始化跳过(可能数据库未就绪): {e}")
# 表不存在时静默跳过,等待用户运行数据库迁移
error_msg = str(e)
if "does not exist" in error_msg or "UndefinedTableError" in error_msg:
logger.info("数据库表未创建,请先运行: alembic upgrade head")
else:
logger.warning(f"数据库初始化跳过: {e}")
logger.info("=" * 50)
logger.info("XCodeReviewer 后端服务已启动")

View File

@ -32,7 +32,7 @@ services:
depends_on:
db:
condition: service_healthy
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
command: sh -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"
networks:
- xcodereviewer-network