Skip to content

Commit 3c8c382

Browse files
authored
bug: check if exists for trophy migrations (#158)
1 parent 86d601d commit 3c8c382

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

  • hasura/migrations/default/1776430500000_tournament_trophies_unique_include_placement

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 ORDER BY a.attname)
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

0 commit comments

Comments
 (0)