Skip to content

Commit 28a3725

Browse files
committed
feat: add dev fixture data system with load/remove actions and startup integration
1 parent ee32169 commit 28a3725

8 files changed

Lines changed: 900 additions & 0 deletions

File tree

hasura/fixtures/cleanup.sql

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
-- Cleanup fixture data
2+
-- Removes all fixture data inserted by fixtures.sql
3+
-- Fixture players use steam_ids 76561198000000001 through 76561198000000030
4+
5+
DO $$
6+
DECLARE
7+
fixture_steam_ids bigint[] := ARRAY(SELECT generate_series(76561198000000001::bigint, 76561198000000030::bigint));
8+
fixture_team_ids uuid[] := ARRAY[
9+
'a0000000-0000-0000-0000-000000000001'::uuid,
10+
'a0000000-0000-0000-0000-000000000002'::uuid,
11+
'a0000000-0000-0000-0000-000000000003'::uuid,
12+
'a0000000-0000-0000-0000-000000000004'::uuid,
13+
'a0000000-0000-0000-0000-000000000005'::uuid,
14+
'a0000000-0000-0000-0000-000000000006'::uuid
15+
];
16+
fixture_tournament_ids uuid[] := ARRAY[
17+
'b0000000-0000-0000-0000-000000000001'::uuid,
18+
'b0000000-0000-0000-0000-000000000002'::uuid,
19+
'b0000000-0000-0000-0000-000000000003'::uuid
20+
];
21+
match_ids uuid[];
22+
BEGIN
23+
-- Collect all match IDs that belong to fixture lineups
24+
SELECT ARRAY(
25+
SELECT DISTINCT m.id FROM matches m
26+
JOIN match_lineups ml ON (m.lineup_1_id = ml.id OR m.lineup_2_id = ml.id)
27+
WHERE ml.team_id = ANY(fixture_team_ids)
28+
) INTO match_ids;
29+
30+
-- If no matches found, also check for matches with fixture players in lineups
31+
IF match_ids IS NULL OR array_length(match_ids, 1) IS NULL THEN
32+
SELECT ARRAY(
33+
SELECT DISTINCT m.id FROM matches m
34+
JOIN match_lineups ml ON (m.lineup_1_id = ml.id OR m.lineup_2_id = ml.id)
35+
JOIN match_lineup_players mlp ON mlp.match_lineup_id = ml.id
36+
WHERE mlp.steam_id = ANY(fixture_steam_ids)
37+
) INTO match_ids;
38+
END IF;
39+
40+
IF match_ids IS NOT NULL AND array_length(match_ids, 1) > 0 THEN
41+
-- Disable triggers to avoid errors from Hasura session vars
42+
-- Note: hypertables (player_kills, player_damages, player_assists, player_flashes,
43+
-- player_objectives, player_utility) don't support DISABLE TRIGGER
44+
ALTER TABLE player_unused_utility DISABLE TRIGGER ALL;
45+
ALTER TABLE match_map_rounds DISABLE TRIGGER ALL;
46+
ALTER TABLE match_maps DISABLE TRIGGER ALL;
47+
ALTER TABLE match_lineup_players DISABLE TRIGGER ALL;
48+
ALTER TABLE matches DISABLE TRIGGER ALL;
49+
ALTER TABLE match_lineups DISABLE TRIGGER ALL;
50+
ALTER TABLE match_options DISABLE TRIGGER ALL;
51+
52+
-- Delete player event data
53+
DELETE FROM player_kills WHERE match_id = ANY(match_ids);
54+
DELETE FROM player_damages WHERE match_id = ANY(match_ids);
55+
DELETE FROM player_assists WHERE match_id = ANY(match_ids);
56+
DELETE FROM player_flashes WHERE match_id = ANY(match_ids);
57+
DELETE FROM player_objectives WHERE match_id = ANY(match_ids);
58+
DELETE FROM player_unused_utility WHERE match_id = ANY(match_ids);
59+
DELETE FROM player_utility WHERE match_id = ANY(match_ids);
60+
61+
-- Delete match map data
62+
DELETE FROM match_map_rounds WHERE match_map_id IN (
63+
SELECT id FROM match_maps WHERE match_id = ANY(match_ids)
64+
);
65+
DELETE FROM match_maps WHERE match_id = ANY(match_ids);
66+
67+
-- Delete match lineup players
68+
DELETE FROM match_lineup_players WHERE match_lineup_id IN (
69+
SELECT ml.id FROM match_lineups ml
70+
JOIN matches m ON (m.lineup_1_id = ml.id OR m.lineup_2_id = ml.id)
71+
WHERE m.id = ANY(match_ids)
72+
);
73+
74+
-- Collect lineup and option IDs before deleting matches
75+
DECLARE
76+
lineup_ids uuid[];
77+
option_ids uuid[];
78+
BEGIN
79+
SELECT ARRAY(
80+
SELECT DISTINCT unnest(ARRAY[m.lineup_1_id, m.lineup_2_id])
81+
FROM matches m WHERE m.id = ANY(match_ids)
82+
) INTO lineup_ids;
83+
84+
SELECT ARRAY(
85+
SELECT DISTINCT m.match_options_id
86+
FROM matches m WHERE m.id = ANY(match_ids) AND m.match_options_id IS NOT NULL
87+
) INTO option_ids;
88+
89+
-- Delete matches
90+
DELETE FROM matches WHERE id = ANY(match_ids);
91+
92+
-- Delete lineups
93+
DELETE FROM match_lineups WHERE id = ANY(lineup_ids);
94+
95+
-- Delete match options (only fixture-created ones)
96+
DELETE FROM match_options WHERE id = ANY(option_ids);
97+
END;
98+
99+
-- Re-enable triggers (excluding hypertables)
100+
ALTER TABLE player_unused_utility ENABLE TRIGGER ALL;
101+
ALTER TABLE match_map_rounds ENABLE TRIGGER ALL;
102+
ALTER TABLE match_maps ENABLE TRIGGER ALL;
103+
ALTER TABLE match_lineup_players ENABLE TRIGGER ALL;
104+
ALTER TABLE matches ENABLE TRIGGER ALL;
105+
ALTER TABLE match_lineups ENABLE TRIGGER ALL;
106+
ALTER TABLE match_options ENABLE TRIGGER ALL;
107+
END IF;
108+
109+
-- Delete tournament data
110+
ALTER TABLE tournament_brackets DISABLE TRIGGER ALL;
111+
ALTER TABLE tournament_team_roster DISABLE TRIGGER ALL;
112+
ALTER TABLE tournament_teams DISABLE TRIGGER ALL;
113+
ALTER TABLE tournament_stages DISABLE TRIGGER ALL;
114+
ALTER TABLE tournaments DISABLE TRIGGER ALL;
115+
116+
DELETE FROM tournament_brackets WHERE tournament_stage_id IN (
117+
SELECT id FROM tournament_stages WHERE tournament_id = ANY(fixture_tournament_ids)
118+
);
119+
DELETE FROM tournament_team_roster WHERE tournament_id = ANY(fixture_tournament_ids);
120+
DELETE FROM tournament_teams WHERE tournament_id = ANY(fixture_tournament_ids);
121+
DELETE FROM tournament_stages WHERE tournament_id = ANY(fixture_tournament_ids);
122+
DELETE FROM tournaments WHERE id = ANY(fixture_tournament_ids);
123+
124+
ALTER TABLE tournament_brackets ENABLE TRIGGER ALL;
125+
ALTER TABLE tournament_team_roster ENABLE TRIGGER ALL;
126+
ALTER TABLE tournament_teams ENABLE TRIGGER ALL;
127+
ALTER TABLE tournament_stages ENABLE TRIGGER ALL;
128+
ALTER TABLE tournaments ENABLE TRIGGER ALL;
129+
130+
-- Delete player stats for fixture players
131+
DELETE FROM player_stats WHERE player_steam_id = ANY(fixture_steam_ids);
132+
DELETE FROM player_kills_by_weapon WHERE player_steam_id = ANY(fixture_steam_ids);
133+
134+
-- Delete team roster and teams
135+
ALTER TABLE team_roster DISABLE TRIGGER ALL;
136+
ALTER TABLE teams DISABLE TRIGGER ALL;
137+
138+
DELETE FROM team_roster WHERE team_id = ANY(fixture_team_ids);
139+
DELETE FROM teams WHERE id = ANY(fixture_team_ids);
140+
141+
ALTER TABLE team_roster ENABLE TRIGGER ALL;
142+
ALTER TABLE teams ENABLE TRIGGER ALL;
143+
144+
-- Delete fixture players
145+
ALTER TABLE players DISABLE TRIGGER ALL;
146+
DELETE FROM players WHERE steam_id = ANY(fixture_steam_ids);
147+
ALTER TABLE players ENABLE TRIGGER ALL;
148+
149+
-- Remove the settings flag
150+
DELETE FROM settings WHERE name = 'dev.fixtures_loaded';
151+
END $$;

0 commit comments

Comments
 (0)