From aa71886e8034a74ab6aebd77ad8aafc3ef654549 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Mon, 15 Feb 2021 11:57:21 +0200 Subject: [PATCH] seeding script initial --- package.json | 4 +- prisma/mocks/plus.ts | 204 +++++++++++++++++++++++++++++++++++++++++++ prisma/mocks/user.ts | 29 ++++++ prisma/seed.ts | 64 ++++++++++++++ 4 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 prisma/mocks/plus.ts create mode 100644 prisma/mocks/user.ts create mode 100644 prisma/seed.ts diff --git a/package.json b/package.json index ea03f47e7..ff67c846b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ "top500": "ts-node prisma/scripts/top500jsons.ts", "league": "cross-env NODE_OPTIONS=--max-old-space-size=8192 ts-node prisma/scripts/leagueJsons.ts", "ex:clean": "cross-env NODE_ENV=development lingui extract --clean", - "compile": "lingui compile" + "compile": "lingui compile", + "restore": "pg_restore -d 'postgresql://sendou@localhost:5432/postgres' --jobs 4 dumped.sql --clean", + "seed": "prisma db seed --preview-feature" }, "dependencies": { "@chakra-ui/icons": "^1.0.5", diff --git a/prisma/mocks/plus.ts b/prisma/mocks/plus.ts new file mode 100644 index 000000000..144ae798e --- /dev/null +++ b/prisma/mocks/plus.ts @@ -0,0 +1,204 @@ +import { Prisma } from "@prisma/client"; + +export const getPlusStatusesData = (): Prisma.PlusStatusCreateManyInput[] => { + return [ + { + userId: 1, + region: "EU", + membershipTier: 1, + canVouchAgainAfter: new Date("2020-1-1"), + }, + { + userId: 2, + region: "NA", + membershipTier: 1, + }, + { + userId: 3, + region: "EU", + membershipTier: 1, + }, + { + userId: 4, + region: "NA", + membershipTier: 1, + }, + { + userId: 5, + region: "EU", + membershipTier: 1, + }, + { + userId: 6, + region: "NA", + membershipTier: 2, + vouchTier: 1, + }, + { + userId: 7, + region: "EU", + membershipTier: 2, + }, + { + userId: 8, + region: "NA", + membershipTier: 2, + }, + { + userId: 9, + region: "EU", + membershipTier: 2, + }, + ]; +}; + +export const getPlusSuggestionsData = (): Prisma.PlusSuggestionCreateManyInput[] => { + return [ + { + description: "yooo so cracked", + region: "NA", + tier: 2, + suggestedId: 10, + suggesterId: 1, + }, + ]; +}; + +export const getPlusVotingSummaryData = (): Prisma.PlusVotingSummaryCreateManyInput[] => { + return [ + { + userId: 1, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [0, 0, 0, 3], + countsNA: [0, 0, 2, 0], + }, + { + userId: 2, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [3, 0, 0, 0], + countsNA: [0, 2, 0, 0], + }, + { + userId: 3, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [1, 1, 1, 0], + countsNA: [0, 1, 1, 0], + }, + { + userId: 4, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [1, 0, 1, 1], + countsNA: [0, 2, 0, 0], + }, + { + userId: 5, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [1, 0, 1, 1], + countsNA: [0, 2, 0, 0], + }, + { + userId: 6, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: true, + year: 2020, + countsEU: [3, 0, 0, 0], + countsNA: [0, 2, 0, 0], + }, + { + userId: 7, + month: 1, + scoreTotal: -1, + tier: 1, + wasSuggested: false, + wasVouched: true, + year: 2020, + countsEU: [0, 0, 0, 3], + countsNA: [0, 0, 2, 0], + }, + // +2 + { + userId: 6, + month: 1, + scoreTotal: -1, + tier: 2, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [0, 0, 0, 2], + countsNA: [0, 0, 2, 0], + }, + + { + userId: 7, + month: 1, + scoreTotal: -1, + tier: 2, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [0, 1, 0, 1], + countsNA: [0, 1, 1, 0], + }, + { + userId: 8, + month: 1, + scoreTotal: -1, + tier: 2, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [0, 2, 0, 0], + countsNA: [0, 0, 2, 0], + }, + { + userId: 9, + month: 1, + scoreTotal: -1, + tier: 2, + wasSuggested: false, + wasVouched: false, + year: 2020, + countsEU: [1, 1, 0, 0], + countsNA: [0, 2, 0, 0], + }, + { + userId: 10, + month: 1, + scoreTotal: -1, + tier: 2, + wasSuggested: true, + wasVouched: false, + year: 2020, + countsEU: [0, 0, 0, 2], + countsNA: [0, 0, 2, 0], + }, + ]; +}; diff --git a/prisma/mocks/user.ts b/prisma/mocks/user.ts new file mode 100644 index 000000000..0ee35515e --- /dev/null +++ b/prisma/mocks/user.ts @@ -0,0 +1,29 @@ +import { Prisma } from "@prisma/client"; + +export const getUsersData = (): Prisma.UserCreateManyInput[] => { + return [ + { + id: 1, + discordId: "79237403620945920", + username: "Sendou", + discriminator: "4059", + patreonTier: 1, + discordAvatar: "1e0968214a6ea74aebce4bbd699d6aae", + }, + ...new Array(9).fill(null).map((_, i) => ({ + id: i + 2, + discordId: padWithZero(i, 17), + discriminator: padWithZero(i, 4), + username: `User${i + 2}`, + })), + ]; +}; + +function padWithZero(root: number, totalLength: number) { + let result = "" + root; + while (result.length < totalLength) { + result += "0"; + } + + return result; +} diff --git a/prisma/seed.ts b/prisma/seed.ts new file mode 100644 index 000000000..7c21449e8 --- /dev/null +++ b/prisma/seed.ts @@ -0,0 +1,64 @@ +import fs from "fs"; +import path from "path"; +import prisma from "./client"; +import { + getPlusVotingSummaryData, + getPlusSuggestionsData, + getPlusStatusesData, +} from "./mocks/plus"; +import { getUsersData } from "./mocks/user"; + +async function main() { + throwIfNotLocalhost(); + + await prisma.profile.deleteMany({}); + await prisma.build.deleteMany({}); + await prisma.salmonRunRecord.deleteMany({}); + await prisma.freeAgentPost.deleteMany({}); + await prisma.team.deleteMany({}); + await prisma.ladderPlayerTrueSkill.deleteMany({}); + await prisma.ladderMatchPlayer.deleteMany({}); + await prisma.plusVotingSummary.deleteMany({}); + await prisma.plusSuggestion.deleteMany({}); + await prisma.plusStatus.deleteMany({}); + await prisma.user.deleteMany({}); + + await prisma.user.createMany({ data: getUsersData() }); + await prisma.plusStatus.createMany({ data: getPlusStatusesData() }); + await prisma.plusSuggestion.createMany({ data: getPlusSuggestionsData() }); + await prisma.plusVotingSummary.createMany({ + data: getPlusVotingSummaryData(), + }); +} + +function throwIfNotLocalhost() { + fs.readFile( + path.join(process.cwd(), "prisma", ".env"), + function (err: any, data: any) { + if (!err) { + for (const line of data.toString().split("\n")) { + if (!line.startsWith("DATABASE_URL=")) { + continue; + } + + if (!line.includes("localhost:")) { + console.error("trying to seed a database not in localhost"); + process.exit(1); + } + } + } else { + console.error(err); + process.exit(1); + } + } + ); +} + +main() + .catch((e) => { + console.error(e); + process.exit(1); + }) + .finally(async () => { + await prisma.$disconnect(); + });