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:
@@ -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);
|
||||
Reference in New Issue
Block a user