DatabaseCreation: dml.sql

File dml.sql, 21.1 KB (added by 231058, 5 days ago)
Line 
1delete from participant_stats;
2delete from stat_types;
3delete from match_participants;
4delete from match_teams;
5delete from matches;
6delete from tournament_teams;
7delete from tournaments;
8delete from team_memberships;
9delete from teams;
10delete from user_achievements;
11delete from achievements;
12delete from reviews;
13delete from notifications;
14delete from preferences;
15delete from payments;
16delete from subscriptions;
17delete from direct_messages;
18delete from friendships;
19delete from linked_accounts;
20delete from servers;
21delete from game_modes;
22delete from game_developers;
23delete from game_genres;
24delete from genres;
25delete from developers;
26
27
28insert into linked_account_providers (id, name) values
29(1, 'steam'),
30(2, 'discord'),
31(3, 'google'),
32(4, 'github')
33on conflict (id) do nothing;
34
35insert into friendship_statuses (id, name) values
36(1, 'pending'),
37(2, 'accepted'),
38(3, 'rejected'),
39(4, 'blocked')
40on conflict (id) do nothing;
41
42insert into subscription_plans (id, name) values
43(1, 'free'),
44(2, 'basic'),
45(3, 'premium'),
46(4, 'pro')
47on conflict (id) do nothing;
48
49insert into notification_types (id, name) values
50(1, 'system'),
51(2, 'friend_request'),
52(3, 'message'),
53(4, 'achievement'),
54(5, 'tournament'),
55(6, 'subscription')
56on conflict (id) do nothing;
57
58insert into team_member_roles (id, name) values
59(1, 'member'),
60(2, 'captain'),
61(3, 'coach'),
62(4, 'owner')
63on conflict (id) do nothing;
64
65insert into match_statuses (id, name) values
66(1, 'scheduled'),
67(2, 'in_progress'),
68(3, 'completed'),
69(4, 'cancelled')
70on conflict (id) do nothing;
71
72insert into participant_results (id, name) values
73(1, 'win'),
74(2, 'loss'),
75(3, 'draw')
76on conflict (id) do nothing;
77
78
79insert into locations (id, latitude, longitude, country, city)
80values (0, 41.998100, 21.426400, 'Unknown', 'Unknown')
81on conflict (id) do nothing;
82
83insert into users (id, username, email, password_hash, location_id, created_at, updated_at)
84values (
85 0,
86 'unknown_user',
87 'unknown_user@test.com',
88 md5('unknown_user'),
89 0,
90 current_timestamp,
91 current_timestamp
92)
93on conflict (id) do nothing;
94
95insert into developers (id, name, country)
96values (0, 'Unknown Developer', 'Unknown')
97on conflict (id) do nothing;
98
99insert into genres (id, name)
100values (0, 'Unknown')
101on conflict (id) do nothing;
102
103insert into games (id, title, release_date, created_at)
104values (0, 'Unknown Game', current_date, current_timestamp)
105on conflict (id) do nothing;
106
107insert into game_modes (id, game_id, mode_name)
108values (0, 0, 'Unknown')
109on conflict (id) do nothing;
110
111
112insert into developers(id, name, country)
113select *
114from (
115 values
116 (1, 'Valve', 'United States'),
117 (2, 'Riot Games', 'United States'),
118 (3, 'Blizzard Entertainment', 'United States'),
119 (4, 'Ubisoft', 'France'),
120 (5, 'Epic Games', 'United States'),
121 (6, 'Respawn Entertainment', 'United States'),
122 (7, 'Mojang Studios', 'Sweden'),
123 (8, 'CD Projekt Red', 'Poland'),
124 (9, 'FromSoftware', 'Japan'),
125 (10, 'Supercell', 'Finland'),
126 (11, 'ArenaNet Studios', 'North Macedonia'),
127 (12, 'Digital Storm Games', 'Serbia'),
128 (13, 'Blue Forge Interactive', 'Germany'),
129 (14, 'NovaByte Games', 'Croatia'),
130 (15, 'IronFox Studio', 'Bulgaria')
131) as d(id, name, country)
132on conflict (id) do nothing;
133
134
135insert into genres(id, name)
136select *
137from (
138 values
139 (1, 'FPS'),
140 (2, 'MOBA'),
141 (3, 'Battle Royale'),
142 (4, 'Strategy'),
143 (5, 'RPG'),
144 (6, 'MMORPG'),
145 (7, 'Sports'),
146 (8, 'Racing'),
147 (9, 'Fighting'),
148 (10, 'Survival'),
149 (11, 'Tactical Shooter'),
150 (12, 'Card Game'),
151 (13, 'Simulation'),
152 (14, 'Action'),
153 (15, 'Adventure')
154) as g(id, name)
155on conflict (id) do nothing;
156
157
158insert into games(id, title, release_date, created_at)
159select *
160from (
161 values
162 (1, 'Counter-Strike 2', date '2023-09-27', current_timestamp),
163 (2, 'Dota 2', date '2013-07-09', current_timestamp),
164 (3, 'Valorant', date '2020-06-02', current_timestamp),
165 (4, 'League of Legends', date '2009-10-27', current_timestamp),
166 (5, 'Overwatch 2', date '2022-10-04', current_timestamp),
167 (6, 'Rainbow Six Siege', date '2015-12-01', current_timestamp),
168 (7, 'Fortnite', date '2017-07-21', current_timestamp),
169 (8, 'Apex Legends', date '2019-02-04', current_timestamp),
170 (9, 'Minecraft', date '2011-11-18', current_timestamp),
171 (10, 'The Witcher 3', date '2015-05-19', current_timestamp),
172 (11, 'Elden Ring', date '2022-02-25', current_timestamp),
173 (12, 'Clash Royale', date '2016-03-02', current_timestamp),
174 (13, 'FIFA 23', date '2022-09-30', current_timestamp),
175 (14, 'Rocket League', date '2015-07-07', current_timestamp),
176 (15, 'Guild Wars 2', date '2012-08-28', current_timestamp)
177) as g(id, title, release_date, created_at)
178on conflict (id) do nothing;
179
180
181insert into game_genres(game_id, genre_id)
182select g.id, ((g.id - 1) % 15) + 1
183from games g
184where g.id between 1 and 15
185on conflict do nothing;
186
187insert into game_genres(game_id, genre_id)
188select g.id, ((g.id + 5) % 15) + 1
189from games g
190where g.id between 1 and 15
191on conflict do nothing;
192
193
194insert into game_developers(game_id, developer_id)
195select g.id, ((g.id - 1) % 15) + 1
196from games g
197where g.id between 1 and 15
198on conflict do nothing;
199
200
201insert into game_modes(id, game_id, mode_name)
202select
203 ((g.id - 1) * 5) + m.mode_id as id,
204 g.id as game_id,
205 m.mode_name
206from games g
207cross join (
208 values
209 (1, '1v1'),
210 (2, '2v2'),
211 (3, '3v3'),
212 (4, '5v5'),
213 (5, 'Practice')
214) as m(mode_id, mode_name)
215where g.id between 1 and 15
216on conflict (id) do nothing;
217
218insert into game_modes(id, game_id, mode_name)
219values (0, 0, 'Unknown')
220on conflict (id) do nothing;
221
222
223insert into servers(id, game_id, location_id, server_name, is_active)
224select
225 ((g.id - 1) * 5) + s.server_no as id,
226 g.id as game_id,
227 l.id as location_id,
228 g.title || ' Server ' || s.server_no as server_name,
229 true as is_active
230from games g
231cross join generate_series(1, 5) s(server_no)
232join lateral (
233 select id
234 from locations
235 where id <> 0
236 order by random()
237 limit 1
238) l on true
239where g.id between 1 and 15
240on conflict (id) do nothing;
241
242insert into servers(id, game_id, location_id, server_name, is_active)
243values (0, 0, 0, 'Unknown Server', false)
244on conflict (id) do nothing;
245
246
247insert into linked_accounts(id, user_id, provider_id, external_user_id, nickname, avatar_url, linked_at)
248select
249 gs as id,
250 gs as user_id,
251 1 as provider_id,
252 'external_' || gs as external_user_id,
253 'player_' || gs as nickname,
254 'https://cdn.arenanet.local/avatar/' || gs || '.png' as avatar_url,
255 now() - (random() * interval '3 years') as linked_at
256from generate_series(1, 300000) gs
257where exists (select 1 from users u where u.id = gs)
258on conflict (id) do nothing;
259
260
261insert into friendships(id, user_id_low, user_id_high, requested_by_user_id, status_id, created_at, updated_at)
262select
263 gs as id,
264 gs as user_id_low,
265 gs + 1 as user_id_high,
266 gs as requested_by_user_id,
267 2 as status_id,
268 t.created_at,
269 t.created_at + (random() * interval '2 years') as updated_at
270from generate_series(1, 500000) gs
271cross join lateral (
272 select now() - (random() * interval '4 years') as created_at
273) t
274where exists (select 1 from users u where u.id = gs)
275 and exists (select 1 from users u where u.id = gs + 1)
276on conflict (id) do nothing;
277
278
279insert into direct_messages(id, sender_id, receiver_id, content, sent_at, read_at)
280select
281 gs as id,
282 gs as sender_id,
283 gs + 1 as receiver_id,
284 case
285 when gs % 5 = 0 then 'Want to queue ranked later?'
286 when gs % 5 = 1 then 'Good game, well played.'
287 when gs % 5 = 2 then 'Join our team for the tournament.'
288 when gs % 5 = 3 then 'Can you review the match stats?'
289 else 'Let us practice tonight.'
290 end as content,
291 msg.sent_at,
292 case
293 when gs % 4 = 0 then null
294 else msg.sent_at + (random() * interval '30 days')
295 end as read_at
296from generate_series(1, 700000) gs
297cross join lateral (
298 select now() - (random() * interval '2 years') as sent_at
299) msg
300where exists (select 1 from users u where u.id = gs)
301 and exists (select 1 from users u where u.id = gs + 1)
302on conflict (id) do nothing;
303
304
305insert into subscriptions(id, user_id, plan_id, start_at, end_at, is_active)
306select
307 gs as id,
308 gs as user_id,
309 ((gs - 1) % 4) + 1 as plan_id,
310 now() - (random() * interval '2 years') as start_at,
311 null as end_at,
312 true as is_active
313from generate_series(1, 500000) gs
314where exists (select 1 from users u where u.id = gs)
315on conflict (id) do nothing;
316
317
318insert into payments(id, user_id, subscription_id, amount, currency_code, paid_at, provider_reference)
319select
320 s.id as id,
321 s.user_id,
322 s.id as subscription_id,
323 case
324 when s.id % 3 = 0 then 4.99
325 when s.id % 3 = 1 then 9.99
326 else 14.99
327 end as amount,
328 'EUR' as currency_code,
329 s.start_at + interval '5 minutes' as paid_at,
330 'PAY-' || s.id as provider_reference
331from subscriptions s
332where s.id between 1 and 500000
333on conflict (id) do nothing;
334
335
336insert into preferences(user_id, game_id, is_favorite, created_at)
337select
338 ((gs - 1) / 5) + 1 as user_id,
339 ((gs - 1) % 5) + 1 as game_id,
340 true as is_favorite,
341 now() - (random() * interval '3 years') as created_at
342from generate_series(1, 500000) gs
343where exists (select 1 from users u where u.id = ((gs - 1) / 5) + 1)
344 and exists (select 1 from games g where g.id = ((gs - 1) % 5) + 1)
345on conflict do nothing;
346
347
348insert into notifications
349(
350 id,
351 user_id,
352 type_id,
353 title,
354 message,
355 is_read,
356 created_at,
357 preference_user_id,
358 preference_game_id
359)
360select
361 gs as id,
362 ((gs - 1) / 5) + 1 as user_id,
363 1 as type_id,
364 case
365 when gs % 3 = 0 then 'Tournament reminder'
366 when gs % 3 = 1 then 'New match available'
367 else 'Game recommendation'
368 end as title,
369 case
370 when gs % 3 = 0 then 'A tournament for one of your favorite games starts soon.'
371 when gs % 3 = 1 then 'A new match has been scheduled.'
372 else 'You may like this game based on your preferences.'
373 end as message,
374 gs % 4 = 0 as is_read,
375 now() - (random() * interval '1 year') as created_at,
376 ((gs - 1) / 5) + 1 as preference_user_id,
377 ((gs - 1) % 5) + 1 as preference_game_id
378from generate_series(1, 100000) gs
379where exists (
380 select 1
381 from preferences p
382 where p.user_id = ((gs - 1) / 5) + 1
383 and p.game_id = ((gs - 1) % 5) + 1
384)
385on conflict (id) do nothing;
386
387
388insert into reviews(id, user_id, game_id, rating, comment, created_at)
389select
390 gs as id,
391 ((gs - 1) / 15) + 1 as user_id,
392 ((gs - 1) % 15) + 1 as game_id,
393 1 + floor(random() * 10)::int as rating,
394 case
395 when gs % 4 = 0 then 'Very competitive and fun.'
396 when gs % 4 = 1 then 'Good matchmaking and active community.'
397 when gs % 4 = 2 then 'Needs better servers, but gameplay is solid.'
398 else 'Great for tournaments and team play.'
399 end as comment,
400 now() - (random() * interval '3 years') as created_at
401from generate_series(1, 20000) gs
402where exists (select 1 from users u where u.id = ((gs - 1) / 15) + 1)
403on conflict (id) do nothing;
404
405
406insert into achievements(id, game_id, title, description)
407select
408 ((g.id - 1) * 5) + a.achievement_no as id,
409 g.id as game_id,
410 case a.achievement_no
411 when 1 then 'First Victory'
412 when 2 then 'Sharp Shooter'
413 when 3 then 'Team Player'
414 when 4 then 'Tournament Debut'
415 else 'Elite Competitor'
416 end as title,
417 case a.achievement_no
418 when 1 then 'Win your first match.'
419 when 2 then 'Achieve a high accuracy score.'
420 when 3 then 'Play multiple matches with a team.'
421 when 4 then 'Join your first tournament.'
422 else 'Reach a high competitive ranking.'
423 end as description
424from games g
425cross join generate_series(1, 5) a(achievement_no)
426where g.id between 1 and 15
427on conflict (id) do nothing;
428
429insert into achievements(id, game_id, title, description)
430values (0, 0, 'Unknown Achievement', 'Default achievement.')
431on conflict (id) do nothing;
432
433
434insert into user_achievements(id, user_id, achievement_id, unlocked_at)
435select
436 gs as id,
437 gs as user_id,
438 ((gs - 1) % 24) + 1 as achievement_id,
439 now() - (random() * interval '3 years') as unlocked_at
440from generate_series(1, 500000) gs
441where exists (select 1 from users u where u.id = gs)
442on conflict (id) do nothing;
443
444
445insert into teams(id, name, created_at)
446select
447 gs as id,
448 'Team_' || gs as name,
449 now() - (random() * interval '4 years') as created_at
450from generate_series(1, 100000) gs
451on conflict (id) do nothing;
452
453
454insert into team_memberships(team_id, user_id, role_id, joined_at, is_active)
455select
456 ((gs - 1) / 5) + 1 as team_id,
457 gs as user_id,
458 case
459 when gs % 5 = 1 then 4
460 else 1
461 end as role_id,
462 now() - (random() * interval '3 years') as joined_at,
463 true as is_active
464from generate_series(1, 500000) gs
465where exists (select 1 from users u where u.id = gs)
466on conflict do nothing;
467
468
469delete from participant_stats;
470delete from stat_types;
471delete from match_participants;
472delete from match_teams;
473delete from matches;
474delete from tournament_teams;
475delete from tournaments;
476
477
478insert into teams(id, name, created_at)
479values (0, 'Unknown Team', now())
480on conflict (id) do nothing;
481
482
483insert into tournaments
484(
485 id,
486 game_id,
487 name,
488 organizer_team_id,
489 prize_pool,
490 starts_at,
491 ends_at,
492 created_at
493)
494select
495 gs as id,
496 g.id as game_id,
497 'Tournament_' || gs as name,
498 ((gs - 1) % 100000) + 1 as organizer_team_id,
499 case
500 when gs % 5 = 0 then 10000
501 when gs % 5 = 1 then 5000
502 when gs % 5 = 2 then 2500
503 when gs % 5 = 3 then 1000
504 else 500
505 end as prize_pool,
506 now() - interval '1 year' + (gs * interval '3 hours') as starts_at,
507 now() - interval '1 year' + (gs * interval '3 hours') + interval '6 hours' as ends_at,
508 now() - interval '1 year' + (gs * interval '2 hours') as created_at
509from generate_series(1, 2000) gs
510join lateral (
511 select id
512 from games
513 where id <> 0
514 order by id
515 offset ((gs - 1) % (select count(*) from games where id <> 0))
516 limit 1
517) g on true
518on conflict (id) do nothing;
519
520
521insert into tournament_teams(tournament_id, team_id, joined_at)
522select
523 t.id as tournament_id,
524 ((t.id - 1) * 8) + slot.slot_no as team_id,
525 t.created_at + interval '1 hour' as joined_at
526from tournaments t
527cross join generate_series(1, 8) slot(slot_no)
528where t.id between 1 and 2000
529on conflict do nothing;
530
531
532insert into matches
533(
534 id,
535 game_id,
536 game_mode_id,
537 tournament_id,
538 server_id,
539 status_id,
540 started_at,
541 ended_at,
542 created_at
543)
544select
545 gs as id,
546 t.game_id,
547 gm.id as game_mode_id,
548 t.id as tournament_id,
549 coalesce(
550 (
551 select s.id
552 from servers s
553 where s.game_id = t.game_id
554 order by s.id
555 limit 1
556 ),
557 0
558 ) as server_id,
559 3 as status_id,
560 t.starts_at + ((gs % 20) * interval '30 minutes') as started_at,
561 t.starts_at + ((gs % 20) * interval '30 minutes') + interval '45 minutes' as ended_at,
562 t.created_at as created_at
563from generate_series(1, 1000000) gs
564join tournaments t
565 on t.id = ((gs - 1) % 2000) + 1
566join game_modes gm
567 on gm.game_id = t.game_id
568 and lower(gm.mode_name) =
569 case
570 when gs <= 300000 then '5v5'
571 when gs <= 600000 then '3v3'
572 when gs <= 800000 then '2v2'
573 else '1v1'
574 end
575on conflict (id) do nothing;
576
577
578insert into match_teams(match_id, team_id, side_label)
579select
580 m.id as match_id,
581 ((m.tournament_id - 1) * 8) + 1 as team_id,
582 'A' as side_label
583from matches m
584where m.id between 1 and 1000000
585on conflict do nothing;
586
587insert into match_teams(match_id, team_id, side_label)
588select
589 m.id as match_id,
590 ((m.tournament_id - 1) * 8) + 2 as team_id,
591 'B' as side_label
592from matches m
593where m.id between 1 and 1000000
594on conflict do nothing;
595
596
597insert into match_participants
598(
599 id,
600 match_id,
601 user_id,
602 team_id,
603 result_id,
604 score,
605 elo_change,
606 joined_at
607)
608select
609 row_number() over (order by m.id, mt.side_label, member.slot_no) as id,
610 m.id as match_id,
611 member.user_id,
612 mt.team_id,
613 case
614 when mt.side_label = 'A' then 1
615 else 2
616 end as result_id,
617 case
618 when mt.side_label = 'A' then 16 + floor(random() * 10)::int
619 else 5 + floor(random() * 11)::int
620 end as score,
621 case
622 when mt.side_label = 'A' then 10 + floor(random() * 20)::int
623 else -20 + floor(random() * 10)::int
624 end as elo_change,
625 m.started_at - interval '5 minutes' as joined_at
626from matches m
627join game_modes gm on gm.id = m.game_mode_id
628join match_teams mt on mt.match_id = m.id
629join lateral (
630 select
631 tm.user_id,
632 row_number() over (order by tm.user_id) as slot_no
633 from team_memberships tm
634 where tm.team_id = mt.team_id
635 and tm.is_active = true
636 order by tm.user_id
637 limit
638 case lower(gm.mode_name)
639 when '5v5' then 5
640 when '3v3' then 3
641 when '2v2' then 2
642 when '1v1' then 1
643 else 0
644 end
645) member on true
646where m.id between 1 and 1000000
647on conflict (id) do nothing;
648
649
650insert into stat_types(id, game_id, name, unit)
651select
652 ((g.id - 1) * 5) + s.stat_no as id,
653 g.id as game_id,
654 case s.stat_no
655 when 1 then 'kills'
656 when 2 then 'deaths'
657 when 3 then 'assists'
658 when 4 then 'damage'
659 else 'accuracy'
660 end as name,
661 case s.stat_no
662 when 4 then 'points'
663 when 5 then '%'
664 else 'count'
665 end as unit
666from games g
667cross join generate_series(1, 5) s(stat_no)
668where g.id <> 0
669on conflict (id) do nothing;
670
671insert into stat_types(id, game_id, name, unit)
672values (0, 0, 'unknown', null)
673on conflict (id) do nothing;
674
675
676insert into participant_stats(match_participant_id, stat_type_id, value)
677select
678 mp.id as match_participant_id,
679 ((m.game_id - 1) * 5) + st.stat_no as stat_type_id,
680 case st.stat_no
681 when 1 then floor(random() * 35)
682 when 2 then floor(random() * 25)
683 when 3 then floor(random() * 20)
684 when 4 then floor(random() * 4000)
685 else round((40 + random() * 60)::numeric, 2)
686 end as value
687from match_participants mp
688join matches m on m.id = mp.match_id
689cross join generate_series(1, 5) st(stat_no)
690where mp.id between 1 and 6000000
691on conflict do nothing;
692
693
694select 'tournaments' as table_name, count(*) from tournaments
695union all select 'tournament_teams', count(*) from tournament_teams
696union all select 'matches', count(*) from matches
697union all select 'match_teams', count(*) from match_teams
698union all select 'match_participants', count(*) from match_participants
699union all select 'stat_types', count(*) from stat_types
700union all select 'participant_stats', count(*) from participant_stats;
701
702
703select
704 gm.mode_name,
705 count(*) as matches
706from matches m
707join game_modes gm on gm.id = m.game_mode_id
708group by gm.mode_name
709order by gm.mode_name;
710
711
712select 'developers' as table_name, count(*) from developers
713union all select 'genres', count(*) from genres
714union all select 'games', count(*) from games
715union all select 'game_modes', count(*) from game_modes
716union all select 'servers', count(*) from servers
717union all select 'linked_accounts', count(*) from linked_accounts
718union all select 'friendships', count(*) from friendships
719union all select 'direct_messages', count(*) from direct_messages
720union all select 'subscriptions', count(*) from subscriptions
721union all select 'payments', count(*) from payments
722union all select 'preferences', count(*) from preferences
723union all select 'notifications', count(*) from notifications
724union all select 'reviews', count(*) from reviews
725union all select 'achievements', count(*) from achievements
726union all select 'user_achievements', count(*) from user_achievements
727union all select 'teams', count(*) from teams
728union all select 'team_memberships', count(*) from team_memberships
729union all select 'tournaments', count(*) from tournaments
730union all select 'tournament_teams', count(*) from tournament_teams
731union all select 'matches', count(*) from matches
732union all select 'match_teams', count(*) from match_teams
733union all select 'match_participants', count(*) from match_participants
734union all select 'stat_types', count(*) from stat_types
735union all select 'participant_stats', count(*) from participant_stats;