Skip to content

Commit 94b8a14

Browse files
committed
bug: check if exists
1 parent 86d601d commit 94b8a14

4 files changed

Lines changed: 29 additions & 47 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/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

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
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+
-- The previous constraint was created via an inline UNIQUE (...) in the
6+
-- original migration, so its name is auto-generated by Postgres. The exact
7+
-- truncation of that name varies across Postgres versions / NAMEDATALEN, so
8+
-- look it up by column set instead of relying on the expected name.
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[];
24+
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 $$;
629

730
ALTER TABLE public.tournament_trophies
831
ADD CONSTRAINT tournament_trophies_tournament_team_player_placement_key

hasura/triggers/tournament_trophy_configs.sql

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

0 commit comments

Comments
 (0)