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; }