11-- Per-tournament trophy toggle. Lets organizers skip / clear trophies
22-- on test or casual tournaments without affecting prior awards elsewhere.
33ALTER TABLE public .tournaments
4- ADD COLUMN trophies_enabled boolean NOT NULL DEFAULT true;
4+ ADD COLUMN IF NOT EXISTS trophies_enabled boolean NOT NULL DEFAULT true;
55
66-- Drop the denormalized copies. Tournament metadata is read via the
77-- existing `tournament` relation; visuals come from tournament_trophy_configs
88-- through a new manual relationship on (tournament_id, placement).
99ALTER TABLE public .tournament_trophies
10- DROP COLUMN tournament_name,
11- DROP COLUMN tournament_start,
12- DROP COLUMN tournament_type,
13- DROP COLUMN custom_name,
14- DROP COLUMN silhouette,
15- DROP COLUMN image_url;
10+ DROP COLUMN IF EXISTS tournament_name,
11+ DROP COLUMN IF EXISTS tournament_start,
12+ DROP COLUMN IF EXISTS tournament_type,
13+ DROP COLUMN IF EXISTS custom_name,
14+ DROP COLUMN IF EXISTS silhouette,
15+ DROP COLUMN IF EXISTS image_url;
1616
1717-- Flag rows awarded outside the standard bracket calc (manual imports).
1818ALTER TABLE public .tournament_trophies
19- ADD COLUMN manual boolean NOT NULL DEFAULT false;
19+ ADD COLUMN IF NOT EXISTS manual boolean NOT NULL DEFAULT false;
2020
2121-- Tournament trophies can now be awarded to either a player or a real team.
2222-- Team trophies are only created for tournament_teams rows that point at
2323-- public.teams via tournament_teams.team_id.
2424ALTER TABLE public .tournament_trophies
25- ADD COLUMN team_id uuid REFERENCES public .teams (id) ON DELETE CASCADE ;
25+ ADD COLUMN IF NOT EXISTS team_id uuid REFERENCES public .teams (id) ON DELETE CASCADE ;
2626
2727ALTER TABLE public .tournament_trophies
2828 ALTER COLUMN player_steam_id DROP NOT NULL ;
@@ -35,26 +35,44 @@ ALTER TABLE public.tournament_trophies
3535
3636DROP INDEX IF EXISTS public .tournament_trophies_tournament_team_player_placement_key ;
3737
38- ALTER TABLE public .tournament_trophies
39- ADD CONSTRAINT tournament_trophies_one_recipient_check
40- CHECK (
41- (CASE WHEN player_steam_id IS NULL THEN 0 ELSE 1 END) +
42- (CASE WHEN team_id IS NULL THEN 0 ELSE 1 END) = 1
43- );
38+ DO $$
39+ BEGIN
40+ IF NOT EXISTS (
41+ SELECT 1 FROM pg_constraint
42+ WHERE conrelid = ' public.tournament_trophies' ::regclass
43+ AND conname = ' tournament_trophies_one_recipient_check'
44+ ) THEN
45+ ALTER TABLE public .tournament_trophies
46+ ADD CONSTRAINT tournament_trophies_one_recipient_check
47+ CHECK (
48+ (CASE WHEN player_steam_id IS NULL THEN 0 ELSE 1 END) +
49+ (CASE WHEN team_id IS NULL THEN 0 ELSE 1 END) = 1
50+ );
51+ END IF;
52+ END $$;
4453
45- ALTER TABLE public .tournament_trophies
46- ADD CONSTRAINT tournament_trophies_mvp_requires_player_check
47- CHECK (placement <> 0 OR player_steam_id IS NOT NULL );
54+ DO $$
55+ BEGIN
56+ IF NOT EXISTS (
57+ SELECT 1 FROM pg_constraint
58+ WHERE conrelid = ' public.tournament_trophies' ::regclass
59+ AND conname = ' tournament_trophies_mvp_requires_player_check'
60+ ) THEN
61+ ALTER TABLE public .tournament_trophies
62+ ADD CONSTRAINT tournament_trophies_mvp_requires_player_check
63+ CHECK (placement <> 0 OR player_steam_id IS NOT NULL );
64+ END IF;
65+ END $$;
4866
49- CREATE UNIQUE INDEX tournament_trophies_player_recipient_key
67+ CREATE UNIQUE INDEX IF NOT EXISTS tournament_trophies_player_recipient_key
5068 ON public .tournament_trophies (tournament_id, tournament_team_id, player_steam_id, placement)
5169 WHERE player_steam_id IS NOT NULL ;
5270
53- CREATE UNIQUE INDEX tournament_trophies_team_recipient_key
71+ CREATE UNIQUE INDEX IF NOT EXISTS tournament_trophies_team_recipient_key
5472 ON public .tournament_trophies (tournament_id, tournament_team_id, team_id, placement)
5573 WHERE team_id IS NOT NULL ;
5674
57- CREATE INDEX idx_tournament_trophies_team
75+ CREATE INDEX IF NOT EXISTS idx_tournament_trophies_team
5876 ON public .tournament_trophies (team_id, placement)
5977 WHERE team_id IS NOT NULL ;
6078
0 commit comments