fix: SQLite has no ADD COLUMN IF NOT EXISTS, crashed on startup
CI / web (push) Successful in 18s
CI / api (push) Successful in 23s

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 <noreply@anthropic.com>
This commit is contained in:
2026-07-12 23:05:54 -06:00
parent d614be4747
commit 7a685b9bd8
+8 -2
View File
@@ -55,8 +55,14 @@ export function openDb(path: string): Database.Database {
); );
`); `);
// Migration for the devices table pre-dating mdns_hostname (CREATE TABLE IF // Migration for the devices table pre-dating mdns_hostname (CREATE TABLE IF
// NOT EXISTS above doesn't touch already-existing tables). // NOT EXISTS above doesn't touch already-existing tables). SQLite's ALTER
db.exec(`ALTER TABLE devices ADD COLUMN IF NOT EXISTS mdns_hostname TEXT`); // 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; return db;
} }