fix(db): rebuild entries table when CHECK constraint lacks sickday
Build and Push Docker Image / build (push) Failing after 2m8s
Build and Push Docker Image / build (push) Failing after 2m8s
This commit is contained in:
@@ -56,9 +56,40 @@ try {
|
|||||||
|
|
||||||
// Migration: Update CHECK constraint to include 'sickday' if needed
|
// Migration: Update CHECK constraint to include 'sickday' if needed
|
||||||
try {
|
try {
|
||||||
// SQLite doesn't support modifying CHECK constraints directly
|
// SQLite can't alter CHECK constraints, so the table has to be rebuilt
|
||||||
// The constraint will be updated when a new sickday entry is added
|
const { sql } = db.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'entries'").get();
|
||||||
console.log('Entry_type constraint check completed');
|
|
||||||
|
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) {
|
} catch (error) {
|
||||||
console.error('Error during entry_type constraint migration:', error);
|
console.error('Error during entry_type constraint migration:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user