From ba5d460e9269d712d8aaf88d197576a19f6465b2 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 27 Mar 2026 20:24:16 +0100 Subject: [PATCH] fix: Graceful fallback if Migration 031 not yet applied - Wrap focus_contributions loading in try/catch - If tables don't exist (migration not run), continue without them - Backward compatible with pre-migration state - Logs warning but doesn't crash --- backend/routers/goals.py | 49 ++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/backend/routers/goals.py b/backend/routers/goals.py index 32ef1a1..7604b1e 100644 --- a/backend/routers/goals.py +++ b/backend/routers/goals.py @@ -720,29 +720,34 @@ def get_goals_grouped(session: dict = Depends(require_auth)): focus_map = {} # goal_id → [contributions] if goal_ids: - placeholders = ','.join(['%s'] * len(goal_ids)) - cur.execute(f""" - SELECT - gfc.goal_id, gfc.contribution_weight, - fa.id as focus_area_id, fa.key, fa.name_de, fa.icon, fa.category - FROM goal_focus_contributions gfc - JOIN focus_area_definitions fa ON gfc.focus_area_id = fa.id - WHERE gfc.goal_id IN ({placeholders}) - ORDER BY gfc.contribution_weight DESC - """, tuple(goal_ids)) + try: + placeholders = ','.join(['%s'] * len(goal_ids)) + cur.execute(f""" + SELECT + gfc.goal_id, gfc.contribution_weight, + fa.id as focus_area_id, fa.key, fa.name_de, fa.icon, fa.category + FROM goal_focus_contributions gfc + JOIN focus_area_definitions fa ON gfc.focus_area_id = fa.id + WHERE gfc.goal_id IN ({placeholders}) + ORDER BY gfc.contribution_weight DESC + """, tuple(goal_ids)) - for row in cur.fetchall(): - gid = row['goal_id'] - if gid not in focus_map: - focus_map[gid] = [] - focus_map[gid].append({ - 'focus_area_id': row['focus_area_id'], - 'key': row['key'], - 'name_de': row['name_de'], - 'icon': row['icon'], - 'category': row['category'], - 'contribution_weight': float(row['contribution_weight']) - }) + for row in cur.fetchall(): + gid = row['goal_id'] + if gid not in focus_map: + focus_map[gid] = [] + focus_map[gid].append({ + 'focus_area_id': row['focus_area_id'], + 'key': row['key'], + 'name_de': row['name_de'], + 'icon': row['icon'], + 'category': row['category'], + 'contribution_weight': float(row['contribution_weight']) + }) + except Exception as e: + # Migration 031 not yet applied - focus_contributions tables don't exist + print(f"[WARNING] Could not load focus_contributions: {e}") + # Continue without focus_contributions (backward compatible) # Group by category and attach focus_contributions grouped = {}