Files
SuperSunday/backend/sql/migrate_matches_add_cols.sql
2025-08-25 12:39:54 +00:00

32 lines
1.3 KiB
SQL

-- Migrate existing matches table to expected columns
-- Add columns if they don't exist
ALTER TABLE IF EXISTS matches
ADD COLUMN IF NOT EXISTS player_a_id INT NULL,
ADD COLUMN IF NOT EXISTS player_b_id INT NULL,
ADD COLUMN IF NOT EXISTS court TEXT NULL,
ADD COLUMN IF NOT EXISTS start_time TIMESTAMP NULL,
ADD COLUMN IF NOT EXISTS score_a INT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS score_b INT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS finished BOOLEAN NOT NULL DEFAULT FALSE;
-- Optionally add FKs if participants table exists
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='participants') THEN
BEGIN
ALTER TABLE matches
ADD CONSTRAINT IF NOT EXISTS matches_player_a_fk FOREIGN KEY (player_a_id) REFERENCES participants(id) ON DELETE SET NULL,
ADD CONSTRAINT IF NOT EXISTS matches_player_b_fk FOREIGN KEY (player_b_id) REFERENCES participants(id) ON DELETE SET NULL;
EXCEPTION WHEN duplicate_object THEN
-- ignore if constraints already exist
NULL;
END;
END IF;
END $$;
-- Ensure finished default is respected on existing rows
UPDATE matches SET finished = COALESCE(finished, FALSE);
UPDATE matches SET score_a = COALESCE(score_a, 0);
UPDATE matches SET score_b = COALESCE(score_b, 0);