seeding script initial

This commit is contained in:
Kalle 2021-02-15 11:57:21 +02:00
parent 463ebeee39
commit aa71886e80
4 changed files with 300 additions and 1 deletions

View File

@ -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",

204
prisma/mocks/plus.ts Normal file
View File

@ -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],
},
];
};

29
prisma/mocks/user.ts Normal file
View File

@ -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;
}

64
prisma/seed.ts Normal file
View File

@ -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();
});