Files
MusicServer/internal/db/migrations/000005_rename_and_add_uuids.up.sql
T
Sansan e86c0f8e04 Combine Issue #28 (UUID Phase 2 - NOT NULL) into migration 000005
- Add NOT NULL constraints to soundtrack.uuid, song.uuid, and song.soundtrack_uuid
- Add corresponding DROP NOT NULL in down migration
- Update header comments to reflect combined Phase 1 & 2

This eliminates the need for a separate migration 000007 while maintaining
proper rollback capability. The NOT NULL constraints are added AFTER
all UUID backfills are complete, ensuring data integrity.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-07-28 14:16:04 +02:00

100 lines
4.0 KiB
SQL

-- Migration: Rename game to soundtrack AND add UUID columns
-- Combines original migrations 000005 (rename), 000006 (UUID columns), and Issue #28 (Phase 2: NOT NULL constraints)
-- Phase 1 & 2: Dual-write with required UUIDs (integer IDs still exist alongside UUIDs)
-- ============ PART 1: RENAME (from original migration 000005) ============
-- Rename game table to soundtrack
ALTER TABLE game RENAME TO soundtrack;
-- Rename primary key sequence
ALTER SEQUENCE game_id_seq RENAME TO soundtrack_id_seq;
-- Rename columns in soundtrack table
ALTER TABLE soundtrack RENAME COLUMN game_name TO soundtrack_name;
-- Update song table: rename game_id to soundtrack_id
ALTER TABLE song RENAME COLUMN game_id TO soundtrack_id;
-- Update song_list table references
ALTER TABLE song_list RENAME COLUMN game_name TO soundtrack_name;
-- Rename foreign key constraint
ALTER TABLE song DROP CONSTRAINT IF EXISTS song_game_id_fkey;
ALTER TABLE song ADD CONSTRAINT song_soundtrack_id_fkey
FOREIGN KEY (soundtrack_id) REFERENCES soundtrack(id);
-- Rename indexes
ALTER INDEX IF EXISTS idx_game_deleted RENAME TO idx_soundtrack_deleted;
ALTER INDEX IF EXISTS idx_game_hash RENAME TO idx_soundtrack_hash;
ALTER INDEX IF EXISTS idx_game_path RENAME TO idx_soundtrack_path;
ALTER INDEX IF EXISTS idx_game_name RENAME TO idx_soundtrack_name;
ALTER INDEX IF EXISTS idx_song_game_id RENAME TO idx_song_soundtrack_id;
ALTER INDEX IF EXISTS idx_song_game_id_song_name RENAME TO idx_song_soundtrack_id_song_name;
ALTER INDEX IF EXISTS song_list_game_name_idx RENAME TO song_list_soundtrack_name_idx;
-- ============ PART 2: UUID COLUMNS (from original migration 000006) ============
-- Step 0: Enable uuid-ossp extension for UUID generation
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Add UUID column to soundtrack table
ALTER TABLE soundtrack ADD COLUMN uuid UUID DEFAULT gen_random_uuid();
-- Add unique constraint on soundtrack.uuid
ALTER TABLE soundtrack ADD CONSTRAINT soundtrack_uuid_unique UNIQUE (uuid);
-- Backfill existing soundtracks with UUIDs (should already have values from DEFAULT)
UPDATE soundtrack SET uuid = gen_random_uuid() WHERE uuid IS NULL;
-- Add index on soundtrack.uuid for query performance
CREATE INDEX IF NOT EXISTS idx_soundtrack_uuid ON soundtrack(uuid);
-- Add UUID columns to song table
ALTER TABLE song ADD COLUMN uuid UUID DEFAULT gen_random_uuid();
ALTER TABLE song ADD COLUMN soundtrack_uuid UUID;
-- Backfill existing songs with UUIDs
UPDATE song SET uuid = gen_random_uuid() WHERE uuid IS NULL;
-- Backfill song.soundtrack_uuid from soundtrack.uuid via JOIN
UPDATE song s
SET soundtrack_uuid = st.uuid
FROM soundtrack st
WHERE s.soundtrack_id = st.id AND s.soundtrack_uuid IS NULL;
-- Add foreign key constraint from song.soundtrack_uuid to soundtrack.uuid
ALTER TABLE song ADD CONSTRAINT song_soundtrack_uuid_fkey
FOREIGN KEY (soundtrack_uuid) REFERENCES soundtrack(uuid);
-- Add indexes on song UUID columns
CREATE INDEX IF NOT EXISTS idx_song_uuid ON song(uuid);
CREATE INDEX IF NOT EXISTS idx_song_soundtrack_uuid ON song(soundtrack_uuid);
-- Drop the composite primary key (soundtrack_id, path)
ALTER TABLE song DROP CONSTRAINT song_pkey;
-- Add new primary key on uuid column
ALTER TABLE song ADD CONSTRAINT song_pkey PRIMARY KEY (uuid);
-- Add unique constraint to maintain same semantics as old PK
-- This ensures (soundtrack, path) combinations remain unique
ALTER TABLE song ADD CONSTRAINT song_soundtrack_path_unique UNIQUE (soundtrack_uuid, path);
-- Create index on soundtrack_id for query performance
CREATE INDEX IF NOT EXISTS idx_song_soundtrack_id ON song(soundtrack_id);
-- Create index on path for lookups
CREATE INDEX IF NOT EXISTS idx_song_path ON song(path);
-- ============ PART 3: MAKE UUID COLUMNS NOT NULL (Issue #28 - Phase 2) ============
-- Add NOT NULL constraint to soundtrack.uuid
ALTER TABLE soundtrack ALTER COLUMN uuid SET NOT NULL;
-- Add NOT NULL constraint to song.uuid
ALTER TABLE song ALTER COLUMN uuid SET NOT NULL;
-- Add NOT NULL constraint to song.soundtrack_uuid
ALTER TABLE song ALTER COLUMN soundtrack_uuid SET NOT NULL;