fix: startup crash - init_db() jetzt mit Error-Handling
All checks were successful
Deploy Development / deploy (push) Successful in 55s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 12s

PROBLEM:
- Backend crasht beim Start auf Prod
- Login zeigt HTML statt JSON (Backend nicht erreichbar)
- Ursache: init_db() wirft Exception beim Startup

FIX:
1. startup_event() wrapped in try-except (non-fatal)
2. init_db() prüft ob ai_prompts Tabelle existiert
3. init_db() hat eigenen try-except
4. Bessere Fehlermeldungen in stdout

ERGEBNIS:
- Backend startet auch wenn init_db() fehlschlägt
- Pipeline-Prompt kann manuell angelegt werden falls nötig
- Login funktioniert wieder

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-19 08:26:09 +01:00
parent c40b30737a
commit 85f48907a4

View File

@ -43,7 +43,11 @@ AVATAR_COLORS = ['#1D9E75','#378ADD','#D85A30','#EF9F27','#7F77DD','#D4537E','#6
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
"""Run migrations and initialization on startup.""" """Run migrations and initialization on startup."""
init_db() try:
init_db()
except Exception as e:
print(f"⚠️ init_db() failed (non-fatal): {e}")
# Don't crash on startup - pipeline prompt can be created manually
def init_db(): def init_db():
"""Initialize database - Schema is loaded by startup.sh""" """Initialize database - Schema is loaded by startup.sh"""
@ -51,23 +55,39 @@ def init_db():
# This function kept for backwards compatibility # This function kept for backwards compatibility
# Ensure "pipeline" master prompt exists # Ensure "pipeline" master prompt exists
with get_db() as conn: try:
cur = get_cursor(conn) with get_db() as conn:
cur.execute("SELECT COUNT(*) as count FROM ai_prompts WHERE slug='pipeline'") cur = get_cursor(conn)
if cur.fetchone()['count'] == 0: # Check if table exists first
cur.execute(""" cur.execute("""
INSERT INTO ai_prompts (slug, name, description, template, active, sort_order) SELECT EXISTS (
VALUES ( SELECT FROM information_schema.tables
'pipeline', WHERE table_schema = 'public'
'Mehrstufige Gesamtanalyse', AND table_name = 'ai_prompts'
'Master-Schalter für die gesamte Pipeline. Deaktiviere diese Analyse, um die Pipeline komplett zu verstecken.', ) as table_exists
'PIPELINE_MASTER',
true,
-10
)
""") """)
conn.commit() if not cur.fetchone()['table_exists']:
print("✓ Pipeline master prompt created") print("⚠️ ai_prompts table doesn't exist yet - skipping pipeline prompt creation")
return
cur.execute("SELECT COUNT(*) as count FROM ai_prompts WHERE slug='pipeline'")
if cur.fetchone()['count'] == 0:
cur.execute("""
INSERT INTO ai_prompts (slug, name, description, template, active, sort_order)
VALUES (
'pipeline',
'Mehrstufige Gesamtanalyse',
'Master-Schalter für die gesamte Pipeline. Deaktiviere diese Analyse, um die Pipeline komplett zu verstecken.',
'PIPELINE_MASTER',
true,
-10
)
""")
conn.commit()
print("✓ Pipeline master prompt created")
except Exception as e:
print(f"⚠️ Could not create pipeline prompt: {e}")
# Don't fail startup - prompt can be created manually
# ── Helper: get profile_id from header ─────────────────────────────────────── # ── Helper: get profile_id from header ───────────────────────────────────────
def get_pid(x_profile_id: Optional[str] = Header(default=None)) -> str: def get_pid(x_profile_id: Optional[str] = Header(default=None)) -> str: