fix: guard migrations against fresh-database installs
All checks were successful
Deploy / deploy (push) Successful in 36s

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 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 12:35:45 +01:00
parent 9b982ad9a5
commit 000391f7fc

View File

@@ -138,6 +138,13 @@ def _migrate_add_routes_unique_index(conn, verbose=True):
Collapses any pre-existing duplicate (scan_id, destination) rows first Collapses any pre-existing duplicate (scan_id, destination) rows first
(keeps the row with the lowest id) before creating the index. (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( cursor = conn.execute(
"SELECT name FROM sqlite_master WHERE type='index' AND name='uq_routes_scan_dest'" "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)") cursor = conn.execute("PRAGMA table_info(scans)")
columns = [row[1] for row in cursor.fetchall()] 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: if 'scheduled_scan_id' in columns:
return # Already migrated return # Already migrated