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
This commit is contained in:
+11
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user