diff --git a/lib/db.ts b/lib/db.ts index b763018..9a87c73 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -13,10 +13,17 @@ mkdirSync(path.dirname(DB_PATH), { recursive: true }); const db = new Database(DB_PATH); db.pragma('foreign_keys = ON'); -// If two processes are ever misconfigured onto the same file, WAL + a busy -// timeout turns SQLITE_BUSY crashes into brief contention. -db.pragma('journal_mode = WAL'); +// Timeout first: every later statement, including the journal switch below, +// waits out a concurrent holder instead of failing with SQLITE_BUSY. db.pragma('busy_timeout = 5000'); +// WAL is best-effort. Switching journal mode needs the file to itself, and +// `next build` imports this module from several workers while the live server +// still holds the same file — a hard failure here fails the whole build. +try { + db.pragma('journal_mode = WAL'); +} catch { + // Another connection holds the file; keep its journal mode. +} db.exec(` CREATE TABLE IF NOT EXISTS instruments ( diff --git a/scripts/seed-settings.js b/scripts/seed-settings.js index 46c7af0..abd0662 100644 --- a/scripts/seed-settings.js +++ b/scripts/seed-settings.js @@ -37,8 +37,12 @@ const DB_PATH = process.env.AUTOFIRMER_DB mkdirSync(path.dirname(DB_PATH), { recursive: true }); const db = new Database(DB_PATH); -db.pragma('journal_mode = WAL'); db.pragma('busy_timeout = 5000'); +try { + db.pragma('journal_mode = WAL'); +} catch { + // Another connection holds the file; keep its journal mode. +} // Mirrors the schema in lib/db.ts for every table a restore touches. The app // has usually never run at this point, so the tables may not exist yet; the