From bebe987b60c802c0081a0b1f166f4445d587d80a Mon Sep 17 00:00:00 2001 From: Patrick Mulligan Date: Thu, 26 Mar 2026 15:05:03 -0400 Subject: [PATCH] =?UTF-8?q?fix(state-store):=20handle=20FK=20constraints?= =?UTF-8?q?=20in=20v4=E2=86=92v5=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The table recreation migration failed on machines with existing transaction_bills/cassette_bills rows due to FK constraints on transactions(txid). Also clean up leftover transactions_new table from any previous failed migration attempt. Closes #38 Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/machine/electron/state-store.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/machine/electron/state-store.ts b/apps/machine/electron/state-store.ts index 1f7b41b..bddf1c4 100644 --- a/apps/machine/electron/state-store.ts +++ b/apps/machine/electron/state-store.ts @@ -158,8 +158,12 @@ export function initDatabase(dbPath?: string): void { if (existing && existing.value === '4') { // Migration v4 → v5: add manual_dispense type and remediated_by column - // SQLite cannot ALTER CHECK constraints, so recreate the table + // SQLite cannot ALTER CHECK constraints, so recreate the table. + // Disable FK constraints during migration to avoid errors from + // transaction_bills/cassette_bills referencing transactions(txid). + db.pragma('foreign_keys = OFF') db.exec(` + DROP TABLE IF EXISTS transactions_new; CREATE TABLE transactions_new ( txid TEXT PRIMARY KEY, type TEXT NOT NULL CHECK (type IN ('cash_in', 'cash_out', 'manual_dispense')), @@ -179,6 +183,7 @@ export function initDatabase(dbPath?: string): void { DROP TABLE transactions; ALTER TABLE transactions_new RENAME TO transactions; `) + db.pragma('foreign_keys = ON') db.exec(` CREATE TABLE IF NOT EXISTS operator_commands ( id INTEGER PRIMARY KEY AUTOINCREMENT,