feat: Add id column to song table and prep for UUID migration

- Add id serial4 PK to song table (was composite PK)
- Update queries to use soundtrack_id + path
- Add UUID columns to soundtrack and song (nullable)
- Add migration tracking table

TODO: Run sqlc generate, then create backfill migration (000008)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-06-01 21:58:21 +02:00
parent 176848bb6d
commit b0418b4f38
8 changed files with 174 additions and 50 deletions
@@ -0,0 +1,24 @@
-- Rollback: Remove id column and restore composite PK
-- Step 1: Drop indexes created in up migration
DROP INDEX IF EXISTS idx_song_soundtrack_id;
DROP INDEX IF EXISTS idx_song_path;
-- Step 2: Drop foreign key constraint
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_soundtrack_id_fkey;
-- Step 3: Drop new primary key
ALTER TABLE song DROP CONSTRAINT song_pkey;
-- Step 4: Drop unique constraint on id
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_id_unique;
-- Step 5: Restore composite primary key
ALTER TABLE song ADD CONSTRAINT song_pkey PRIMARY KEY (soundtrack_id, path);
-- Step 6: Drop the id column
ALTER TABLE song DROP COLUMN id;
-- Step 7: Recreate original foreign key (soundtrack_id references soundtrack.id)
ALTER TABLE song ADD CONSTRAINT song_soundtrack_id_fkey
FOREIGN KEY (soundtrack_id) REFERENCES soundtrack(id);
@@ -0,0 +1,36 @@
-- Migration: Add id column to song table and change PK from composite to single column
-- This prepares the song table for eventual UUID migration
-- Step 1: Add new id column (nullable initially)
ALTER TABLE song ADD COLUMN id serial4;
-- Step 2: Create unique constraint on id (allows backfilling)
ALTER TABLE song ADD CONSTRAINT song_id_unique UNIQUE (id);
-- Step 3: Backfill existing rows with sequential IDs
-- Use DEFAULT which pulls from the sequence
UPDATE song SET id = DEFAULT WHERE id IS NULL;
-- Step 4: Verify all rows have an id
-- If this returns 0, backfill worked
-- SELECT COUNT(*) FROM song WHERE id IS NULL;
-- Step 5: Drop the composite primary key (soundtrack_id, path)
ALTER TABLE song DROP CONSTRAINT song_pkey;
-- Step 6: Add new primary key on id column
ALTER TABLE song ADD CONSTRAINT song_pkey PRIMARY KEY (id);
-- Step 7: Ensure soundtrack_id remains a foreign key to soundtrack
-- First drop existing FK if it exists (from the rename migration)
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_soundtrack_id_fkey;
-- Then recreate it
ALTER TABLE song ADD CONSTRAINT song_soundtrack_id_fkey
FOREIGN KEY (soundtrack_id) REFERENCES soundtrack(id);
-- Step 8: Create index on soundtrack_id for query performance
CREATE INDEX IF NOT EXISTS idx_song_soundtrack_id ON song(soundtrack_id);
-- Step 9: Create index on path for lookups (previously part of PK)
CREATE INDEX IF NOT EXISTS idx_song_path ON song(path);