From b9684b010766c29cd3889addcc69227a77b62272 Mon Sep 17 00:00:00 2001 From: Fusion Date: Mon, 18 May 2026 23:05:30 +0200 Subject: [PATCH] fix: fail fast on startup if database migrations fail - Exit with error code 1 if init_database() returns False - Update health check to verify database connectivity - Prevents confusing 'table does not exist' errors later --- apps/api/src/main.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/api/src/main.py b/apps/api/src/main.py index 043f656..1ebb411 100644 --- a/apps/api/src/main.py +++ b/apps/api/src/main.py @@ -123,8 +123,9 @@ async def on_startup(): # Initialize database (run migrations) db_ready = await init_database() if not db_ready: - logger.error("Database initialization failed. API may not function correctly.") - # Continue anyway so the health endpoint remains available + logger.error("Database initialization failed. Shutting down.") + import sys + sys.exit(1) # Seed built-in data await seed_builtin_tool_types() @@ -132,7 +133,14 @@ async def on_startup(): @app.get("/health") async def health_check(): - return {"status": "healthy"} + try: + from sqlalchemy import text + async with SessionLocal() as session: + await session.execute(text("SELECT 1")) + return {"status": "healthy", "database": "connected"} + except Exception as exc: + logger.error("Health check failed: %s", exc) + return {"status": "unhealthy", "database": "disconnected", "error": str(exc)} app.include_router(auth_router) app.include_router(projects_router)