Skip to content

Commit 89772ed

Browse files
committed
bug: check if exists for trophy migrations (#158)
1 parent 86d601d commit 89772ed

7 files changed

Lines changed: 87 additions & 78 deletions

File tree

hasura/functions/leaderboard/get_leaderboard.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ BEGIN
358358
SUM(CASE WHEN tt.placement = 3 THEN 1 ELSE 0 END)::int as bronze,
359359
COUNT(*)::int as total
360360
FROM tournament_trophies tt
361-
WHERE (_window_days = 0 OR tt.tournament_start >= NOW() - make_interval(days => _window_days))
361+
JOIN tournaments t ON t.id = tt.tournament_id
362+
WHERE tt.player_steam_id IS NOT NULL
363+
AND (_window_days = 0 OR t.start >= NOW() - make_interval(days => _window_days))
362364
GROUP BY tt.player_steam_id
363365
)
364366
SELECT

hasura/migrations/default/1776430000000_add_tournament_trophies/up.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE public.tournament_trophies (
1+
CREATE TABLE IF NOT EXISTS public.tournament_trophies (
22
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
33
tournament_id uuid NOT NULL REFERENCES public.tournaments(id) ON DELETE CASCADE,
44
tournament_team_id uuid NOT NULL REFERENCES public.tournament_teams(id) ON DELETE CASCADE,
@@ -22,15 +22,15 @@ CREATE TABLE public.tournament_trophies (
2222
UNIQUE (tournament_id, tournament_team_id, player_steam_id)
2323
);
2424

25-
CREATE INDEX idx_tournament_trophies_player
25+
CREATE INDEX IF NOT EXISTS idx_tournament_trophies_player
2626
ON public.tournament_trophies(player_steam_id, placement);
27-
CREATE INDEX idx_tournament_trophies_tournament
27+
CREATE INDEX IF NOT EXISTS idx_tournament_trophies_tournament
2828
ON public.tournament_trophies(tournament_id);
29-
CREATE UNIQUE INDEX tournament_trophies_one_mvp_per_tournament
29+
CREATE UNIQUE INDEX IF NOT EXISTS tournament_trophies_one_mvp_per_tournament
3030
ON public.tournament_trophies(tournament_id)
3131
WHERE placement = 0;
3232

33-
CREATE TABLE public.tournament_trophy_configs (
33+
CREATE TABLE IF NOT EXISTS public.tournament_trophy_configs (
3434
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
3535
tournament_id uuid NOT NULL REFERENCES public.tournaments(id) ON DELETE CASCADE,
3636
placement int NOT NULL CHECK (placement IN (0, 1, 2, 3)),
@@ -42,5 +42,5 @@ CREATE TABLE public.tournament_trophy_configs (
4242
UNIQUE (tournament_id, placement)
4343
);
4444

45-
CREATE INDEX idx_tournament_trophy_configs_tournament
45+
CREATE INDEX IF NOT EXISTS idx_tournament_trophy_configs_tournament
4646
ON public.tournament_trophy_configs(tournament_id);

hasura/migrations/default/1776430500000_tournament_trophies_unique_include_placement/down.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ALTER TABLE public.tournament_trophies
2-
DROP CONSTRAINT tournament_trophies_tournament_team_player_placement_key;
2+
DROP CONSTRAINT IF EXISTS tournament_trophies_tournament_team_player_placement_key;
33

44
ALTER TABLE public.tournament_trophies
55
ADD CONSTRAINT tournament_trophies_tournament_id_tournament_team_id_player_key
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
-- Allow a player to hold both a placement trophy (gold/silver/bronze) and MVP
22
-- on the same tournament. The old uniqueness key blocked the second insert
33
-- when the MVP was on the winning team.
4-
ALTER TABLE public.tournament_trophies
5-
DROP CONSTRAINT tournament_trophies_tournament_id_tournament_team_id_player_key;
4+
--
5+
-- Idempotent: the previous constraint's name is auto-generated by Postgres
6+
-- (inline UNIQUE in the original migration), and truncation varies by
7+
-- version / NAMEDATALEN, so look it up by column set. Also guard the new
8+
-- constraint so re-running is safe.
9+
DO $$
10+
DECLARE
11+
v_conname text;
12+
BEGIN
13+
SELECT c.conname
14+
INTO v_conname
15+
FROM pg_constraint c
16+
WHERE c.conrelid = 'public.tournament_trophies'::regclass
17+
AND c.contype = 'u'
18+
AND (
19+
SELECT array_agg(a.attname::text ORDER BY a.attname::text)
20+
FROM unnest(c.conkey) AS k(attnum)
21+
JOIN pg_attribute a
22+
ON a.attrelid = c.conrelid AND a.attnum = k.attnum
23+
) = ARRAY['player_steam_id', 'tournament_id', 'tournament_team_id']::text[];
624

7-
ALTER TABLE public.tournament_trophies
8-
ADD CONSTRAINT tournament_trophies_tournament_team_player_placement_key
9-
UNIQUE (tournament_id, tournament_team_id, player_steam_id, placement);
25+
IF v_conname IS NOT NULL THEN
26+
EXECUTE 'ALTER TABLE public.tournament_trophies DROP CONSTRAINT ' || quote_ident(v_conname);
27+
END IF;
28+
END $$;
29+
30+
DO $$
31+
BEGIN
32+
IF NOT EXISTS (
33+
SELECT 1 FROM pg_constraint
34+
WHERE conrelid = 'public.tournament_trophies'::regclass
35+
AND conname = 'tournament_trophies_tournament_team_player_placement_key'
36+
) THEN
37+
ALTER TABLE public.tournament_trophies
38+
ADD CONSTRAINT tournament_trophies_tournament_team_player_placement_key
39+
UNIQUE (tournament_id, tournament_team_id, player_steam_id, placement);
40+
END IF;
41+
END $$;

hasura/migrations/default/1776431000000_add_player_elo_impact/up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- In-match performance multiplier (0.8 - 1.2) driven by KDA-vs-team and damage share.
22
-- Stored as a level metric independent of ELO swings so MVP and similar
33
-- consumers can rank players without favoring those whose ELO moved most.
4-
ALTER TABLE public.player_elo ADD COLUMN impact numeric;
4+
ALTER TABLE public.player_elo ADD COLUMN IF NOT EXISTS impact numeric;
55

66
-- Backfill from raw player_kills / player_assists / player_damages.
77
-- Mirrors the pre-loss-transform formula in get_player_elo_for_match.

hasura/migrations/default/1776432000000_redesign_tournament_trophies/up.sql

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
-- Per-tournament trophy toggle. Lets organizers skip / clear trophies
22
-- on test or casual tournaments without affecting prior awards elsewhere.
33
ALTER 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).
99
ALTER 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).
1818
ALTER 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.
2424
ALTER 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

2727
ALTER TABLE public.tournament_trophies
2828
ALTER COLUMN player_steam_id DROP NOT NULL;
@@ -35,26 +35,44 @@ ALTER TABLE public.tournament_trophies
3535

3636
DROP 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

hasura/triggers/tournament_trophy_configs.sql

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)