From cfd8de5d1ddc0b9e1a0584376fe08bf41c9085f5 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 1 May 2021 16:27:14 +0300 Subject: [PATCH] Migration for western LB (#472) * Migration * Bot freeAgents API get profile updatedAt * Add isJP script --- package.json | 1 + pages/api/bot/freeagents.ts | 4 +- prisma/client.ts | 7 ++- .../20210501111558_add_is_jp/migration.sql | 24 ++++++++++ prisma/schema.prisma | 20 +------- prisma/scripts/isJP.ts | 48 +++++++++++++++++++ 6 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 prisma/migrations/20210501111558_add_is_jp/migration.sql create mode 100644 prisma/scripts/isJP.ts diff --git a/package.json b/package.json index 08974c7de..7f415191f 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "prebuild": "ts-node prisma/scripts/preBuild.ts", "mongo": "ts-node prisma/scripts/dataFromMongo.ts", "top500": "ts-node prisma/scripts/top500jsons.ts", + "isJP": "ts-node prisma/scripts/isJP.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 && npm run prettier:format", "compile": "lingui compile", diff --git a/pages/api/bot/freeagents.ts b/pages/api/bot/freeagents.ts index 62f4872da..4d26eb045 100644 --- a/pages/api/bot/freeagents.ts +++ b/pages/api/bot/freeagents.ts @@ -25,7 +25,9 @@ const teamsFreeAgentsHandler = async ( discordAvatar: true, username: true, discriminator: true, - profile: { select: { country: true, weaponPool: true } }, + profile: { + select: { country: true, weaponPool: true, updatedAt: true }, + }, }, }, }, diff --git a/prisma/client.ts b/prisma/client.ts index 2e5685302..3d6add0c4 100644 --- a/prisma/client.ts +++ b/prisma/client.ts @@ -4,8 +4,11 @@ import { PrismaClient } from "@prisma/client"; let prisma: PrismaClient; if (!global.prisma) { - //global.prisma = new PrismaClient({ log: ["query", "info", "warn"] }); - global.prisma = new PrismaClient(); + global.prisma = new PrismaClient( + process.env.NODE_ENV !== "production" + ? { log: ["query", "info", "warn"] } + : undefined + ); } prisma = global.prisma; diff --git a/prisma/migrations/20210501111558_add_is_jp/migration.sql b/prisma/migrations/20210501111558_add_is_jp/migration.sql new file mode 100644 index 000000000..a3ff28398 --- /dev/null +++ b/prisma/migrations/20210501111558_add_is_jp/migration.sql @@ -0,0 +1,24 @@ +/* + Warnings: + + - You are about to drop the column `canPostEvents` on the `User` table. All the data in the column will be lost. + - You are about to drop the `Feedback` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "Feedback" DROP CONSTRAINT "Feedback_userId_fkey"; + +-- AlterTable +ALTER TABLE "Player" ADD COLUMN "isJP" BOOLEAN NOT NULL DEFAULT false; + +-- AlterTable +ALTER TABLE "Profile" ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; + +-- AlterTable +ALTER TABLE "User" DROP COLUMN "canPostEvents"; + +-- DropTable +DROP TABLE "Feedback"; + +-- DropEnum +DROP TYPE "FeedbackEmoji"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 31f398ee8..dbbb7f072 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -17,7 +17,6 @@ model User { teamId Int? ladderTeamId Int? patreonTier Int? - canPostEvents Boolean @default(false) profile Profile? player Player? builds Build[] @@ -36,7 +35,6 @@ model User { votesMade PlusBallot[] votesReceived PlusBallot[] @relation("Voter") votingSummaries PlusVotingSummary[] - feedback Feedback[] calendarEvents CalendarEvent[] } @@ -49,6 +47,7 @@ model Profile { sensStick Float? bio String? weaponPool String[] + updatedAt DateTime @default(now()) @updatedAt customUrlPath String? @unique user User @relation(fields: [userId], references: [id]) userId Int @unique @@ -121,6 +120,7 @@ model Player { userId Int? @unique name String? principalId String? + isJP Boolean @default(false) placements XRankPlacement[] user User? @relation(fields: [userId], references: [id]) leaguePlacements LeagueSquadMember[] @@ -406,22 +406,6 @@ model PlusVotingSummary { @@unique([userId, tier, month, year]) } -enum FeedbackEmoji { - STARSTRUCK - SMILE - CONFUSED - SOB -} - -model Feedback { - id Int @id @default(autoincrement()) - text String - emoji FeedbackEmoji? - mobile Boolean @default(false) - userId Int? - user User? @relation(fields: [userId], references: [id]) -} - enum TournamentFormat { SE DE diff --git a/prisma/scripts/isJP.ts b/prisma/scripts/isJP.ts new file mode 100644 index 000000000..b7b3b7dcc --- /dev/null +++ b/prisma/scripts/isJP.ts @@ -0,0 +1,48 @@ +import prisma from "../client"; + +const main = async () => { + // const players = await prisma.player.findMany({ + // where: { NOT: { name: null } }, + // }); + + // const jpCharaRegex = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]/; + + // const playersWithWesternNames: string[] = []; + + // for (const player of players) { + // if (jpCharaRegex.test(player.name!)) { + // playersWithWesternNames.push(player.switchAccountId); + // } + // } + + const players = await prisma.player.findMany({ + where: { NOT: { name: null }, isJP: false }, + include: { leaguePlacements: { include: { squad: true } } }, + }); + + const actuallyJP: string[] = []; + + for (const player of players) { + let ratio = 0; + for (const placement of player.leaguePlacements) { + if (placement.squad.region === "JP") ratio--; + else ratio++; + } + if (ratio < 0) { + actuallyJP.push(player.switchAccountId); + } + } + + await prisma.player.updateMany({ + data: { isJP: true }, + where: { switchAccountId: { in: actuallyJP } }, + }); + + console.log("done", players.length); +}; + +main() + .catch((e) => console.error(e)) + .finally(async () => { + await prisma.$disconnect(); + });