From 357d4e03fd22c1dc4fd89906c32f42f39b1b6782 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Tue, 30 Nov 2021 21:34:00 +0200 Subject: [PATCH] Switch from serial to UUID's --- .../to/$organization.$tournament/register.tsx | 4 +-- .../to/$organization.$tournament/teams.tsx | 8 +++--- app/services/tournament.ts | 6 ++--- app/utils/index.ts | 2 +- package.json | 1 + .../migration.sql | 26 +++++++++---------- prisma/schema.prisma | 24 ++++++++--------- prisma/seed.ts | 8 +++--- 8 files changed, 40 insertions(+), 39 deletions(-) rename prisma/migrations/{20211117122514_initial => 20211130192438_initial}/migration.sql (91%) diff --git a/app/routes/to/$organization.$tournament/register.tsx b/app/routes/to/$organization.$tournament/register.tsx index 742b4e7ad..e5b9bad18 100644 --- a/app/routes/to/$organization.$tournament/register.tsx +++ b/app/routes/to/$organization.$tournament/register.tsx @@ -41,10 +41,10 @@ export const action: ActionFunction = async ({ }): Promise => { const formData = await request.formData(); const teamName = formData.get("teamName"); - const tournamentId = Number(formData.get("tournamentId")); + const tournamentId = formData.get("tournamentId"); invariant(typeof teamName === "string", "Invalid type for team name."); invariant( - typeof tournamentId === "number", + typeof tournamentId === "string", "Invalid type for tournament id." ); diff --git a/app/routes/to/$organization.$tournament/teams.tsx b/app/routes/to/$organization.$tournament/teams.tsx index 596715a72..faa365dde 100644 --- a/app/routes/to/$organization.$tournament/teams.tsx +++ b/app/routes/to/$organization.$tournament/teams.tsx @@ -10,7 +10,7 @@ export default function TeamsTab() { const sortedTeams = teams // TODO: user id here - .sort(sortOwnTeamsAndFullTeamsFirst(-1)) + .sort(sortOwnTeamsAndFullTeamsFirst("")) .map((team) => { return { ...team, @@ -31,10 +31,10 @@ function sortCaptainFirst(a: { captain: boolean }, b: { captain: boolean }) { return Number(b.captain) - Number(a.captain); } -function sortOwnTeamsAndFullTeamsFirst(userId?: number) { +function sortOwnTeamsAndFullTeamsFirst(userId?: string) { return function ( - a: { members: { member: { id: number } }[] }, - b: { members: { member: { id: number } }[] } + a: { members: { member: { id: string } }[] }, + b: { members: { member: { id: string } }[] } ) { if (userId) { const aSortValue = Number( diff --git a/app/services/tournament.ts b/app/services/tournament.ts index 0c7c831bc..1baf91f52 100644 --- a/app/services/tournament.ts +++ b/app/services/tournament.ts @@ -14,7 +14,7 @@ export async function findTournamentByNameForUrl({ }: { organizationNameForUrl: string; tournamentNameForUrl: string; - userId?: number; + userId?: string; }) { const tournaments = await db.tournament.findMany({ where: { @@ -117,9 +117,9 @@ export function createTournamentTeam({ teamName, tournamentId, }: { - userId: number; + userId: string; teamName: string; - tournamentId: number; + tournamentId: string; }) { return db.tournamentTeam.create({ data: { diff --git a/app/utils/index.ts b/app/utils/index.ts index 54f397f75..54e2bb165 100644 --- a/app/utils/index.ts +++ b/app/utils/index.ts @@ -22,7 +22,7 @@ export const getUser = (ctx: any) => { }; export type LoggedInUser = { - id: number; + id: string; discordId: string; discordAvatar: string; } | null; diff --git a/package.json b/package.json index 783925d9b..1a64c8300 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "seed": "npx prisma db seed", "seed:reset": "npx prisma migrate reset --force --skip-generate", "lint:styles": "stylelint \"app/styles/**/*.css\"", + "typecheck": "tsc --noEmit", "cy:open": "npx cypress open", "cy:run": "npx cypress run" }, diff --git a/prisma/migrations/20211117122514_initial/migration.sql b/prisma/migrations/20211130192438_initial/migration.sql similarity index 91% rename from prisma/migrations/20211117122514_initial/migration.sql rename to prisma/migrations/20211130192438_initial/migration.sql index 5ae115694..88554a382 100644 --- a/prisma/migrations/20211117122514_initial/migration.sql +++ b/prisma/migrations/20211130192438_initial/migration.sql @@ -3,7 +3,7 @@ CREATE TYPE "Mode" AS ENUM ('TW', 'SZ', 'TC', 'RM', 'CB'); -- CreateTable CREATE TABLE "User" ( - "id" SERIAL NOT NULL, + "id" TEXT NOT NULL, "discordId" TEXT NOT NULL, "discordName" TEXT NOT NULL, "discordDiscriminator" TEXT NOT NULL, @@ -21,10 +21,10 @@ CREATE TABLE "User" ( -- CreateTable CREATE TABLE "Organization" ( - "id" SERIAL NOT NULL, + "id" TEXT NOT NULL, "name" TEXT NOT NULL, "nameForUrl" TEXT NOT NULL, - "ownerId" INTEGER NOT NULL, + "ownerId" TEXT NOT NULL, "discordInvite" TEXT NOT NULL, "twitter" TEXT, @@ -33,7 +33,7 @@ CREATE TABLE "Organization" ( -- CreateTable CREATE TABLE "Tournament" ( - "id" SERIAL NOT NULL, + "id" TEXT NOT NULL, "name" TEXT NOT NULL, "nameForUrl" TEXT NOT NULL, "description" TEXT, @@ -41,7 +41,7 @@ CREATE TABLE "Tournament" ( "checkInTime" TIMESTAMP(3) NOT NULL, "bannerBackground" TEXT NOT NULL, "bannerTextHSLArgs" TEXT NOT NULL, - "organizerId" INTEGER NOT NULL, + "organizerId" TEXT NOT NULL, CONSTRAINT "Tournament_pkey" PRIMARY KEY ("id") ); @@ -57,10 +57,10 @@ CREATE TABLE "Stage" ( -- CreateTable CREATE TABLE "TournamentTeam" ( - "id" SERIAL NOT NULL, + "id" TEXT NOT NULL, "name" TEXT NOT NULL, "checkedIn" BOOLEAN NOT NULL DEFAULT false, - "tournamentId" INTEGER NOT NULL, + "tournamentId" TEXT NOT NULL, "inviteCode" TEXT NOT NULL, CONSTRAINT "TournamentTeam_pkey" PRIMARY KEY ("id") @@ -68,23 +68,23 @@ CREATE TABLE "TournamentTeam" ( -- CreateTable CREATE TABLE "TournamentTeamMember" ( - "memberId" INTEGER NOT NULL, - "teamId" INTEGER NOT NULL, - "tournamentId" INTEGER NOT NULL, + "memberId" TEXT NOT NULL, + "teamId" TEXT NOT NULL, + "tournamentId" TEXT NOT NULL, "captain" BOOLEAN NOT NULL DEFAULT false ); -- CreateTable CREATE TABLE "TrustRelationships" ( - "trustGiverId" INTEGER NOT NULL, - "trustReceiverId" INTEGER NOT NULL, + "trustGiverId" TEXT NOT NULL, + "trustReceiverId" TEXT NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP ); -- CreateTable CREATE TABLE "_StageToTournament" ( "A" INTEGER NOT NULL, - "B" INTEGER NOT NULL + "B" TEXT NOT NULL ); -- CreateIndex diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c5c396219..91330765d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -8,7 +8,7 @@ datasource db { } model User { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) discordId String @unique discordName String discordDiscriminator String @@ -27,11 +27,11 @@ model User { } model Organization { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) name String /// Name in lower case to show in URL nameForUrl String @unique - ownerId Int @unique + ownerId String @unique owner User @relation(fields: [ownerId], references: [id]) discordInvite String twitter String? @@ -39,7 +39,7 @@ model Organization { } model Tournament { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) name String /// Name in lower case to show in URL nameForUrl String @@ -51,7 +51,7 @@ model Tournament { /// CSS for tournament banner's color value bannerTextHSLArgs String mapPool Stage[] - organizerId Int + organizerId String organizer Organization @relation(fields: [organizerId], references: [id]) teams TournamentTeam[] @@ -75,10 +75,10 @@ model Stage { } model TournamentTeam { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) name String checkedIn Boolean @default(false) - tournamentId Int + tournamentId String tournament Tournament @relation(fields: [tournamentId], references: [id]) inviteCode String @default(uuid()) members TournamentTeamMember[] @@ -87,9 +87,9 @@ model TournamentTeam { } model TournamentTeamMember { - memberId Int - teamId Int - tournamentId Int + memberId String + teamId String + tournamentId String captain Boolean @default(false) team TournamentTeam @relation(fields: [teamId], references: [id]) member User @relation(fields: [memberId], references: [id]) @@ -101,8 +101,8 @@ model TournamentTeamMember { // to add them to a tournament team // / LFG group without them having to click a link. model TrustRelationships { - trustGiverId Int - trustReceiverId Int + trustGiverId String + trustReceiverId String trustGiver User @relation("trustGiver", fields: [trustGiverId], references: [id]) trustReceiver User @relation("trustReceiver", fields: [trustReceiverId], references: [id]) createdAt DateTime @default(now()) diff --git a/prisma/seed.ts b/prisma/seed.ts index 2c95d0f89..903eae202 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -103,7 +103,7 @@ async function users() { }); } -async function tournamentTeams(tournamentId: number, users: number[]) { +async function tournamentTeams(tournamentId: string, users: string[]) { const randomIds = faker.helpers.shuffle(users); for (let index = 0; index < 24; index++) { const team = await prisma.tournamentTeam.create({ @@ -127,7 +127,7 @@ async function tournamentTeams(tournamentId: number, users: number[]) { } } -async function organizations(userId: number) { +async function organizations(userId: string) { return prisma.organization.create({ data: { name: "Sendou's Tournaments", @@ -141,7 +141,7 @@ async function organizations(userId: number) { const modesList = ["TW", "SZ", "TC", "RM", "CB"] as const; -async function tournaments(organizationId: number) { +async function tournaments(organizationId: string) { return prisma.tournament.create({ data: { bannerBackground: "linear-gradient(to bottom, #9796f0, #fbc7d4)", @@ -157,7 +157,7 @@ async function tournaments(organizationId: number) { } // TODO: why this can't be done while creating? -async function tournamentAddMaps(id: number) { +async function tournamentAddMaps(id: string) { const ids = Array.from( new Set( new Array(24)