From ca71eec23cf828d80f5388f1f602a825f2dd52f5 Mon Sep 17 00:00:00 2001 From: Felix Schlusche Date: Fri, 2 Oct 2026 11:10:26 +0200 Subject: [PATCH] fix(db): rebuild entries table when CHECK constraint lacks sickday --- server.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index db0e005..7063f19 100644 --- a/server.js +++ b/server.js @@ -56,9 +56,40 @@ try { // Migration: Update CHECK constraint to include 'sickday' if needed try { - // SQLite doesn't support modifying CHECK constraints directly - // The constraint will be updated when a new sickday entry is added - console.log('Entry_type constraint check completed'); + // SQLite can't alter CHECK constraints, so the table has to be rebuilt + const { sql } = db.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'entries'").get(); + + if (!sql.includes("'sickday'")) { + console.log('Rebuilding entries table to allow sickday entries...'); + const seq = db.prepare("SELECT seq FROM sqlite_sequence WHERE name = 'entries'").get(); + + db.transaction(() => { + db.exec(` + CREATE TABLE entries_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + date TEXT NOT NULL UNIQUE, + start_time TEXT, + end_time TEXT, + pause_minutes INTEGER NOT NULL DEFAULT 0, + location TEXT DEFAULT 'office' CHECK(location IN ('office', 'home')), + entry_type TEXT DEFAULT 'work' CHECK(entry_type IN ('work', 'vacation', 'flextime', 'sickday')) + ) + `); + db.exec(` + INSERT INTO entries_new (id, date, start_time, end_time, pause_minutes, location, entry_type) + SELECT id, date, start_time, end_time, pause_minutes, location, entry_type FROM entries + `); + db.exec('DROP TABLE entries'); + db.exec('ALTER TABLE entries_new RENAME TO entries'); + + // Keep the AUTOINCREMENT counter so ids of deleted entries are never reused + if (seq) { + db.prepare("UPDATE sqlite_sequence SET seq = ? WHERE name = 'entries'").run(seq.seq); + } + })(); + + console.log('Entries table rebuilt successfully'); + } } catch (error) { console.error('Error during entry_type constraint migration:', error); }