From 7a685b9bd845ea7bfbc14a039f6b0be4322a2165 Mon Sep 17 00:00:00 2001 From: jhodgkin Date: Sun, 12 Jul 2026 23:05:54 -0600 Subject: [PATCH] fix: SQLite has no ADD COLUMN IF NOT EXISTS, crashed on startup Conflated CREATE TABLE/INDEX's IF NOT EXISTS support with ALTER TABLE ADD COLUMN, which SQLite has never supported -- syntax error, not a version issue (confirmed on 3.49.2). Check pragma table_info for the column first instead. Verified against both a fresh DB and one simulating the existing pre-migration production schema, and confirmed idempotent on a second open. Co-Authored-By: Claude Sonnet 5 --- apps/api/src/db/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/api/src/db/index.ts b/apps/api/src/db/index.ts index 7456689..330e8d3 100644 --- a/apps/api/src/db/index.ts +++ b/apps/api/src/db/index.ts @@ -55,8 +55,14 @@ export function openDb(path: string): Database.Database { ); `); // Migration for the devices table pre-dating mdns_hostname (CREATE TABLE IF - // NOT EXISTS above doesn't touch already-existing tables). - db.exec(`ALTER TABLE devices ADD COLUMN IF NOT EXISTS mdns_hostname TEXT`); + // NOT EXISTS above doesn't touch already-existing tables). SQLite's ALTER + // TABLE ADD COLUMN has no IF NOT EXISTS clause (unlike CREATE TABLE/INDEX), + // so check first -- confirmed the hard way, "ADD COLUMN IF NOT EXISTS" is a + // syntax error even on a SQLite version otherwise new enough for it. + const hasMdnsColumn = (db.pragma("table_info(devices)") as { name: string }[]).some( + (c) => c.name === "mdns_hostname" + ); + if (!hasMdnsColumn) db.exec(`ALTER TABLE devices ADD COLUMN mdns_hostname TEXT`); return db; }