fix: correct the database migration for gear

This commit is contained in:
Jon Seager 2026-02-02 15:58:31 +00:00
parent 7fc1d4db70
commit 0cd4bc999c
No known key found for this signature in database
2 changed files with 29 additions and 17 deletions

View file

@ -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);

View file

@ -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