From 0cd4bc999c34e00663873510c1be5575ad8ca24c Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Mon, 2 Feb 2026 15:58:31 +0000 Subject: [PATCH] fix: correct the database migration for gear --- ...07_update_timeline_constraint_for_gear.sql | 29 +++++++++++++++++++ migrations/0007_update_timeline_for_gear.sql | 17 ----------- 2 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 migrations/0007_update_timeline_constraint_for_gear.sql delete mode 100644 migrations/0007_update_timeline_for_gear.sql diff --git a/migrations/0007_update_timeline_constraint_for_gear.sql b/migrations/0007_update_timeline_constraint_for_gear.sql new file mode 100644 index 0000000..84e02ac --- /dev/null +++ b/migrations/0007_update_timeline_constraint_for_gear.sql @@ -0,0 +1,29 @@ +-- Update timeline_events CHECK constraint to include 'gear' +-- SQLite requires recreating the table to modify CHECK constraints + +-- Step 1: Rename old table +ALTER TABLE timeline_events RENAME TO timeline_events_old; + +-- Step 2: Create new table with updated constraint +CREATE TABLE timeline_events ( + id INTEGER PRIMARY KEY, + entity_type TEXT NOT NULL CHECK (entity_type IN ('roaster', 'roast', 'bag', 'gear')), + entity_id INTEGER NOT NULL, + occurred_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), + title TEXT NOT NULL, + details_json TEXT, + tasting_notes_json TEXT, + action TEXT NOT NULL DEFAULT 'added' +); + +-- Step 3: Copy data from old table +INSERT INTO timeline_events (id, entity_type, entity_id, occurred_at, title, details_json, tasting_notes_json, action) +SELECT id, entity_type, entity_id, occurred_at, title, details_json, tasting_notes_json, action +FROM timeline_events_old; + +-- Step 4: Drop old table +DROP TABLE timeline_events_old; + +-- Step 5: Recreate indexes +CREATE INDEX idx_timeline_events_entity ON timeline_events(entity_type, entity_id); +CREATE INDEX idx_timeline_events_occurred_at ON timeline_events(occurred_at); diff --git a/migrations/0007_update_timeline_for_gear.sql b/migrations/0007_update_timeline_for_gear.sql deleted file mode 100644 index 583284b..0000000 --- a/migrations/0007_update_timeline_for_gear.sql +++ /dev/null @@ -1,17 +0,0 @@ --- Add 'gear' to timeline_events entity_type constraint --- SQLite doesn't support ALTER COLUMN CHECK, so we need to recreate the constraint --- This is safe because CHECK constraints in SQLite are not enforced retroactively - --- For SQLite: The CHECK constraint will be validated on INSERT/UPDATE --- We just need to ensure the application code uses 'gear' correctly --- The constraint in the original migration (0001_init.sql) would need to be updated --- to include 'gear' in a clean deployment, but for existing databases this migration --- documents that 'gear' is now a valid entity_type - --- For PostgreSQL (if using that feature flag): --- ALTER TABLE timeline_events DROP CONSTRAINT IF EXISTS timeline_events_entity_type_check; --- ALTER TABLE timeline_events ADD CONSTRAINT timeline_events_entity_type_check --- CHECK (entity_type IN ('roaster', 'roast', 'bag', 'gear')); - --- SQLite: No actual schema change needed, application-level validation ensures correctness --- This migration serves as documentation that 'gear' is now a valid entity_type