From b8d45c441325bd12433d0b2dd5fa30a1384eb662 Mon Sep 17 00:00:00 2001 From: soolar Date: Fri, 24 Nov 2023 16:10:55 -0800 Subject: [PATCH] Populate Greenbox table via the migration instead of a script --- makegreenboxtable.ts | 31 ------------------- .../migration.sql | 5 +++ 2 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 makegreenboxtable.ts diff --git a/makegreenboxtable.ts b/makegreenboxtable.ts deleted file mode 100644 index b133ea2..0000000 --- a/makegreenboxtable.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { prisma } from "./src/clients/database.js"; - -export async function makeGreenboxes() { - try { - const playersWithGreenbox = await prisma.player.findMany({ - where: { - greenboxString: { not: null }, - greenboxLastUpdate: { not: null }, - }, - select: { - playerId: true, - greenboxString: true, - greenboxLastUpdate: true, - }, - orderBy: { playerId: "asc" }, - }); - await prisma.greenbox.createMany({ - data: playersWithGreenbox.map((data) => { - return { - playerId: data.playerId, - data: data.greenboxString, - time: data.greenboxLastUpdate, - }; - }), - }); - } catch (error) { - console.log(error); - } -} - -makeGreenboxes(); diff --git a/prisma/migrations/20231124193505_greenbox_history/migration.sql b/prisma/migrations/20231124193505_greenbox_history/migration.sql index 9caa1b3..f3971ed 100644 --- a/prisma/migrations/20231124193505_greenbox_history/migration.sql +++ b/prisma/migrations/20231124193505_greenbox_history/migration.sql @@ -10,3 +10,8 @@ CREATE TABLE "Greenbox" ( -- AddForeignKey ALTER TABLE "Greenbox" ADD CONSTRAINT "Greenbox_playerId_fkey" FOREIGN KEY ("playerId") REFERENCES "Player"("playerId") ON DELETE RESTRICT ON UPDATE CASCADE; + +INSERT INTO "Greenbox" ("playerId", "data", "time") +SELECT "playerId", "greenboxString" as "data", "greenboxLastUpdate" as "time" +FROM "Player" +WHERE "greenboxString" IS NOT NULL AND "greenboxLastUpdate" IS NOT NULL;