-- Redesign ratings table for single JSON timeseries entry with soft deletes DROP TABLE IF EXISTS rating_new; CREATE TABLE rating_new ( id TEXT PRIMARY KEY, participant_id TEXT NOT NULL REFERENCES participant(id), audio_file_id TEXT NOT NULL REFERENCES audio_file(id), final_value REAL NOT NULL, timeseries_data TEXT NOT NULL, created_at INTEGER NOT NULL, deleted_at INTEGER DEFAULT NULL ); -- Copy existing data if any (transform to new format) INSERT INTO rating_new (id, participant_id, audio_file_id, final_value, timeseries_data, created_at, deleted_at) SELECT id, participant_id, audio_file_id, value as final_value, '[{"timestamp":' || timestamp || ',"value":' || value || ',"time":' || (created_at * 1000) || '}]' as timeseries_data, created_at, CASE WHEN is_completed = 1 THEN NULL ELSE strftime('%s', 'now') * 1000 END as deleted_at FROM rating WHERE is_completed = 1; -- Only keep completed ratings, mark others as deleted -- Drop old table and rename DROP TABLE rating; ALTER TABLE rating_new RENAME TO rating; -- Create unique index for active ratings only CREATE UNIQUE INDEX participant_audio_active ON rating (participant_id, audio_file_id) WHERE deleted_at IS NULL;