From 3b443a2663f6aa51168296fdcd63fe6ad2809da5 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Mon, 8 Nov 2021 11:45:08 +0200 Subject: [PATCH] Separate seeding script --- postgraphile/migrations/0001_initial.sql | 11 ---- postgraphile/package.json | 3 +- postgraphile/scripts/seed.js | 83 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 postgraphile/scripts/seed.js diff --git a/postgraphile/migrations/0001_initial.sql b/postgraphile/migrations/0001_initial.sql index 28d592f15..d375d2e01 100644 --- a/postgraphile/migrations/0001_initial.sql +++ b/postgraphile/migrations/0001_initial.sql @@ -140,14 +140,3 @@ create table sendou_ink.map_pool ( create index map_pool_tournament_identifier on sendou_ink.map_pool(tournament_identifier); create index map_pool_map_mode_id on sendou_ink.map_pool(map_mode_id); - --- seed for dev - -insert into sendou_ink.account (discord_username, discord_discriminator, discord_avatar, twitch, twitter, youtube_id, youtube_name) -values ('Sendou', '0043', 'fcfd65a3bea598905abb9ca25296816b', 'Sendou', 'Sendouc', 'UCWbJLXByvsfQvTcR4HLPs5Q', 'Sendou'); - -insert into sendou_ink.organization (identifier, name, discord_invite_code, twitter, owner_id) -values ('sendous', 'Sendou´s tournaments', 'sendou', 'sendouc', 1); - -insert into sendou_ink.tournament (identifier, name, description, start_time, banner_background, banner_text_hsl_args, organization_identifier) -values ('in-the-zone-x', 'In The Zone X', 'In The Zone eXtremeeeee', '2022-06-22 20:00:00', 'linear-gradient(to bottom, #9796f0, #fbc7d4)', '31 9% 16%', 'sendous'); diff --git a/postgraphile/package.json b/postgraphile/package.json index 37a9be8d9..65761b72b 100644 --- a/postgraphile/package.json +++ b/postgraphile/package.json @@ -16,7 +16,8 @@ "dev": "ts-node-dev index.ts", "delete-migrations": "node scripts/delete-migrations.js", "migrate": "node migrations/index.mjs", - "migrate:reset": "npm run delete-migrations && npm run migrate", + "seed": "node scripts/seed.js", + "migrate:reset": "npm run delete-migrations && npm run migrate && npm run seed", "test": "jest" }, "jest": { diff --git a/postgraphile/scripts/seed.js b/postgraphile/scripts/seed.js new file mode 100644 index 000000000..17ce87638 --- /dev/null +++ b/postgraphile/scripts/seed.js @@ -0,0 +1,83 @@ +require("dotenv").config(); +const { Client } = require("pg"); + +const stages = [ + "The Reef", + "Musselforge Fitness", + "Starfish Mainstage", + "Humpback Pump Track", + "Inkblot Art Academy", + "Sturgeon Shipyard", + "Moray Towers", + "Port Mackerel", + "Manta Maria", + "Kelp Dome", + "Snapper Canal", + "Blackbelly Skatepark", + "MakoMart", + "Walleye Warehouse", + "Shellendorf Institute", + "Arowana Mall", + "Goby Arena", + "Piranha Pit", + "Camp Triggerfish", + "Wahoo World", + "New Albacore Hotel", + "Ancho-V Games", + "Skipper Pavilion", +]; + +const modesShort = ["TW", "SZ", "TC", "RM", "CB"]; + +async function main() { + const client = new Client({ connectionString: process.env.DATABASE_URL }); + + try { + await client.connect(); + await client.query(` + insert into sendou_ink.account (discord_username, discord_discriminator, discord_avatar, twitch, twitter, youtube_id, youtube_name) + values ('Sendou', '0043', 'fcfd65a3bea598905abb9ca25296816b', 'Sendou', 'Sendouc', 'UCWbJLXByvsfQvTcR4HLPs5Q', 'Sendou'); + `); + await client.query(` + insert into sendou_ink.organization (identifier, name, discord_invite_code, twitter, owner_id) + values ('sendous', 'Sendou´s tournaments', 'sendou', 'sendouc', 1); + `); + await client.query(` + insert into sendou_ink.tournament (identifier, name, description, start_time, banner_background, banner_text_hsl_args, organization_identifier) + values ('in-the-zone-x', 'In The Zone X', 'In The Zone eXtremeeeee', '2022-06-22 20:00:00', 'linear-gradient(to bottom, #9796f0, #fbc7d4)', '31 9% 16%', 'sendous'); + `); + + for (const stage of stages) { + for (const mode of modesShort) { + await client.query(` + insert into sendou_ink.map_mode (stage, game_mode) + values ('${stage}', '${mode}'); + `); + } + } + + const ids = Array.from( + new Set(new Array(24).fill(null).map(() => getRandomInt(115))) + ); + + for (const id of ids) { + await client.query(` + insert into sendou_ink.map_pool (tournament_identifier, map_mode_id) + values ('in-the-zone-x', '${id}'); + `); + } + } finally { + client.end(); + } +} + +function getRandomInt(maxInclusive) { + let result = -1; + + while (result < 24) { + result = Math.floor(Math.random() * maxInclusive) + 1; + } + return result; +} + +main();