diff --git a/README.md b/README.md index eaf988147..79a643185 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ DATABASE_URL=postgresql://sendou@localhost:5432 _You can see [Prisma's guide on how to set up a PostgreSQL database running locally](https://www.prisma.io/dataguide/postgresql/setting-up-a-local-postgresql-database) for more info._ 6. Use `npm run migrate` to get the database formatted with the right tables. -7. There should be a seeding script but this doesn't exist yet. If anyone is interested in contributing this is probably a good starting point (see issue #197). +7. Seed some example data in the database by running `npm run seed`. (This seed data is incomplete – see issue #197 if you would like to improve the seed data!) ### Enable logging in diff --git a/cypress/support/index.ts b/cypress/support/index.ts index 328571d58..065ebc68a 100644 --- a/cypress/support/index.ts +++ b/cypress/support/index.ts @@ -13,5 +13,6 @@ Cypress.Commands.add("login", (user: "sendou" | "nzap") => { }); beforeEach(() => { - cy.exec("npm run seed"); + // TODO: use database transactions, instead of dropping and recreating the database with each individual test + cy.exec("npm run migrate:reset -- --force"); }); diff --git a/package-lock.json b/package-lock.json index 2cff0f5bd..030ad6b99 100644 --- a/package-lock.json +++ b/package-lock.json @@ -62,6 +62,7 @@ "@types/uuid": "^8.3.0", "cross-env": "^7.0.3", "cypress": "^6.8.0", + "fishery": "^1.2.0", "prettier": "^2.2.1", "prisma": "^2.19.0", "ts-node": "^9.1.1", @@ -4024,6 +4025,15 @@ "node": ">=8" } }, + "node_modules/fishery": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fishery/-/fishery-1.2.0.tgz", + "integrity": "sha512-0GG029KHF3p8Q0NiAl/ZOK1fvyAprOiHdtRWUNS46x9QXuQhMwzcGLNDbZ7XIEEBowwBmMsw7StkaU0ek9dSbg==", + "dev": true, + "dependencies": { + "lodash.mergewith": "^4.6.2" + } + }, "node_modules/focus-lock": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/focus-lock/-/focus-lock-0.8.1.tgz", @@ -12164,6 +12174,15 @@ "path-exists": "^4.0.0" } }, + "fishery": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fishery/-/fishery-1.2.0.tgz", + "integrity": "sha512-0GG029KHF3p8Q0NiAl/ZOK1fvyAprOiHdtRWUNS46x9QXuQhMwzcGLNDbZ7XIEEBowwBmMsw7StkaU0ek9dSbg==", + "dev": true, + "requires": { + "lodash.mergewith": "^4.6.2" + } + }, "focus-lock": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/focus-lock/-/focus-lock-0.8.1.tgz", diff --git a/package.json b/package.json index f96905373..82a0b3eb9 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "start": "next start", "migrate": "prisma migrate deploy --preview-feature", "migrate:save": "prisma migrate dev --create-only --preview-feature", + "migrate:reset": "prisma migrate reset", "gen": "npx prisma generate", "prebuild": "ts-node prisma/scripts/preBuild.ts", "mongo": "ts-node prisma/scripts/dataFromMongo.ts", @@ -77,6 +78,7 @@ "@types/uuid": "^8.3.0", "cross-env": "^7.0.3", "cypress": "^6.8.0", + "fishery": "^1.2.0", "prettier": "^2.2.1", "prisma": "^2.19.0", "ts-node": "^9.1.1", diff --git a/prisma/factories/user.ts b/prisma/factories/user.ts new file mode 100644 index 000000000..ffa8217df --- /dev/null +++ b/prisma/factories/user.ts @@ -0,0 +1,21 @@ +import { Factory } from "fishery"; +import { User } from "@prisma/client"; +import prisma from "../client"; + +export default Factory.define(({ sequence, onCreate }) => { + onCreate(user => { + return prisma.user.create({ data: user }); + }); + + return { + id: sequence, + discordId: sequence.toString().padStart(17, '0'), + discordAvatar: null, + discriminator: sequence.toString().padStart(4, '0'), + username: `User${sequence}`, + patreonTier: 0, + canPostEvents: false, + teamId: null, + ladderTeamId: null + }; +}); diff --git a/prisma/mocks/user.ts b/prisma/mocks/user.ts deleted file mode 100644 index e333a139f..000000000 --- a/prisma/mocks/user.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Prisma } from "@prisma/client"; - -export const getUsersData = (): Prisma.UserCreateManyInput[] => { - return [ - ...new Array(10).fill(null).map((_, i) => ({ - id: i + 1, - discordId: padWithZero(i, 17), - discriminator: padWithZero(i, 4), - username: `User${i + 1}`, - })), - { - id: 11, - discordId: "79237403620945920", - username: "Sendou", - discriminator: "4059", - patreonTier: 1, - discordAvatar: "1e0968214a6ea74aebce4bbd699d6aae", - }, - { - id: 12, - discordId: "455039198672453645", - username: "NZAP", - discriminator: "6227", - discordAvatar: "f809176af93132c3db5f0a5019e96339", - }, - ]; -}; - -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 index 7c21449e8..e50dba669 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -6,29 +6,11 @@ import { getPlusSuggestionsData, getPlusStatusesData, } from "./mocks/plus"; -import { getUsersData } from "./mocks/user"; +import userFactory from "./factories/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(), - }); + await seedNewData(); } function throwIfNotLocalhost() { @@ -54,6 +36,27 @@ function throwIfNotLocalhost() { ); } +async function seedNewData() { + await seedUsers(); + await prisma.plusStatus.createMany({ data: getPlusStatusesData() }); + await prisma.plusSuggestion.createMany({ data: getPlusSuggestionsData() }); + await prisma.plusVotingSummary.createMany({ + data: getPlusVotingSummaryData(), + }); +} + +async function seedUsers() { + const randomUsers = [...Array(10)].map((_, _i) => { + return userFactory.build(); + }) + + await prisma.user.createMany({data: [ + ...randomUsers, + userFactory.build({username: "Sendou", patreonTier: 1}), + userFactory.build({username: "NZAP"}) + ]}) +} + main() .catch((e) => { console.error(e);