From 000391f7fc9028d36f025ef014f894a3328e7d71 Mon Sep 17 00:00:00 2001 From: domverse Date: Sat, 28 Feb 2026 12:35:45 +0100 Subject: [PATCH] fix: guard migrations against fresh-database installs On a fresh DB, migrations ran before the schema was applied and tried to operate on tables that didn't exist yet (routes, scans), causing: "no such table: routes" on _migrate_add_routes_unique_index "no such table: scans" on _migrate_add_scheduled_scan_id_to_scans Added table-existence checks so both migrations bail out when the table isn't there yet. The schema's CREATE TABLE IF NOT EXISTS handles fresh installs correctly. Co-Authored-By: Claude Sonnet 4.6 --- flight-comparator/database/init_db.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/flight-comparator/database/init_db.py b/flight-comparator/database/init_db.py index d024102..d992820 100644 --- a/flight-comparator/database/init_db.py +++ b/flight-comparator/database/init_db.py @@ -138,6 +138,13 @@ def _migrate_add_routes_unique_index(conn, verbose=True): Collapses any pre-existing duplicate (scan_id, destination) rows first (keeps the row with the lowest id) before creating the index. """ + # Fresh install: routes table doesn't exist yet — schema will create the index + cursor = conn.execute( + "SELECT name FROM sqlite_master WHERE type='table' AND name='routes'" + ) + if not cursor.fetchone(): + return + cursor = conn.execute( "SELECT name FROM sqlite_master WHERE type='index' AND name='uq_routes_scan_dest'" ) @@ -177,6 +184,8 @@ def _migrate_add_scheduled_scan_id_to_scans(conn, verbose=True): """ cursor = conn.execute("PRAGMA table_info(scans)") columns = [row[1] for row in cursor.fetchall()] + if not columns: + return # Fresh install: scans table doesn't exist yet — schema will create the column if 'scheduled_scan_id' in columns: return # Already migrated