From 179be420103ae9294894b52671bc369c4297c7cb Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Tue, 13 Sep 2022 23:13:33 +0300 Subject: [PATCH] update: db schema --- .../migration.sql | 52 +++++++++++++ prisma/schema.prisma | 69 +++++++++------- prisma/seed/index.js | 78 +++++++++++++++++++ 3 files changed, 172 insertions(+), 27 deletions(-) create mode 100644 prisma/migrations/20220913134026_update_tournament_image_rel/migration.sql diff --git a/prisma/migrations/20220913134026_update_tournament_image_rel/migration.sql b/prisma/migrations/20220913134026_update_tournament_image_rel/migration.sql new file mode 100644 index 0000000..038339d --- /dev/null +++ b/prisma/migrations/20220913134026_update_tournament_image_rel/migration.sql @@ -0,0 +1,52 @@ +/* + Warnings: + + - A unique constraint covering the columns `[thumbnail_image_id]` on the table `Tournament` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[cover_image_id]` on the table `Tournament` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[image_id]` on the table `TournamentEvent` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[avatar_id]` on the table `TournamentJudge` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[image_id]` on the table `TournamentPrize` will be added. If there are existing duplicate values, this will fail. + +*/ +-- AlterTable +ALTER TABLE "Tournament" ADD COLUMN "cover_image_id" INTEGER, +ADD COLUMN "thumbnail_image_id" INTEGER; + +-- AlterTable +ALTER TABLE "TournamentEvent" ADD COLUMN "image_id" INTEGER; + +-- AlterTable +ALTER TABLE "TournamentJudge" ADD COLUMN "avatar_id" INTEGER; + +-- AlterTable +ALTER TABLE "TournamentPrize" ADD COLUMN "image_id" INTEGER; + +-- CreateIndex +CREATE UNIQUE INDEX "Tournament_thumbnail_image_id_key" ON "Tournament"("thumbnail_image_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Tournament_cover_image_id_key" ON "Tournament"("cover_image_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "TournamentEvent_image_id_key" ON "TournamentEvent"("image_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "TournamentJudge_avatar_id_key" ON "TournamentJudge"("avatar_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "TournamentPrize_image_id_key" ON "TournamentPrize"("image_id"); + +-- AddForeignKey +ALTER TABLE "Tournament" ADD CONSTRAINT "Tournament_thumbnail_image_id_fkey" FOREIGN KEY ("thumbnail_image_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Tournament" ADD CONSTRAINT "Tournament_cover_image_id_fkey" FOREIGN KEY ("cover_image_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TournamentPrize" ADD CONSTRAINT "TournamentPrize_image_id_fkey" FOREIGN KEY ("image_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TournamentJudge" ADD CONSTRAINT "TournamentJudge_avatar_id_fkey" FOREIGN KEY ("avatar_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TournamentEvent" ADD CONSTRAINT "TournamentEvent_image_id_fkey" FOREIGN KEY ("image_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cad516d..5003892 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -296,29 +296,38 @@ model HostedImage { createdAt DateTime @default(now()) is_used Boolean @default(false) - ProjectThumbnail Project? @relation("Project_Thumbnail") - ProjectCoverImage Project? @relation("Project_CoverImage") - CategoryCoverImage Category? @relation("Category_CoverImage") - AwardImage Award? @relation("Award_Image") - HackathonCoverImage Hackathon? @relation("Hackathon_CoverImage") - StoryCoverImage Story? @relation("Story_CoverImage") - User User? @relation("User_Avatar") + ProjectThumbnail Project? @relation("Project_Thumbnail") + ProjectCoverImage Project? @relation("Project_CoverImage") + CategoryCoverImage Category? @relation("Category_CoverImage") + AwardImage Award? @relation("Award_Image") + HackathonCoverImage Hackathon? @relation("Hackathon_CoverImage") + StoryCoverImage Story? @relation("Story_CoverImage") + User User? @relation("User_Avatar") + TournamentThumbnail Tournament? @relation("Tournament_ThumbnailImage") + Tournament_CoverImage Tournament? @relation("Tournament_CoverImage") + TournamentPrize_Image TournamentPrize? @relation("TournamentPrize_Image") + TournamentJudge_Avatar TournamentJudge? @relation("TournamentJudge_Avatar") + TournamentEvent_Image TournamentEvent? @relation("TournamentEvent_Image") } // ----------------- // Tournament // ----------------- model Tournament { - id Int @id @default(autoincrement()) - title String - description String - thumbnail_image String - cover_image String - start_date DateTime - end_date DateTime - location String - website String - votes_count Int @default(0) + id Int @id @default(autoincrement()) + title String + description String + thumbnail_image String + thumbnail_image_id Int? @unique + thumbnail_image_rel HostedImage? @relation("Tournament_ThumbnailImage", fields: [thumbnail_image_id], references: [id]) + cover_image String + cover_image_id Int? @unique + cover_image_rel HostedImage? @relation("Tournament_CoverImage", fields: [cover_image_id], references: [id]) + start_date DateTime + end_date DateTime + location String + website String + votes_count Int @default(0) prizes TournamentPrize[] judges TournamentJudge[] @@ -329,21 +338,25 @@ model Tournament { } model TournamentPrize { - id Int @id @default(autoincrement()) - title String - amount String - image String + id Int @id @default(autoincrement()) + title String + amount String + image String + image_id Int? @unique + image_rel HostedImage? @relation("TournamentPrize_Image", fields: [image_id], references: [id]) tournament Tournament @relation(fields: [tournament_id], references: [id]) tournament_id Int } model TournamentJudge { - id Int @id @default(autoincrement()) - name String - avatar String - company String - twitter String? + id Int @id @default(autoincrement()) + name String + avatar String + avatar_id Int? @unique + avatar_rel HostedImage? @relation("TournamentJudge_Avatar", fields: [avatar_id], references: [id]) + company String + twitter String? tournament Tournament @relation(fields: [tournament_id], references: [id]) tournament_id Int @@ -359,9 +372,11 @@ model TournamentFAQ { } model TournamentEvent { - id Int @id @default(autoincrement()) + id Int @id @default(autoincrement()) title String image String + image_id Int? @unique + image_rel HostedImage? @relation("TournamentEvent_Image", fields: [image_id], references: [id]) description String starts_at DateTime ends_at DateTime diff --git a/prisma/seed/index.js b/prisma/seed/index.js index f7c2af2..beb24cd 100644 --- a/prisma/seed/index.js +++ b/prisma/seed/index.js @@ -232,6 +232,84 @@ async function migrateOldImages() { }) } } + + /** + * Tournament + **/ + const tournaments = await prisma.tournament.findMany({ + select: { + id: true, + thumbnail_image: true, + cover_image: true, + } + }) + for (const tournament of tournaments) { + if (tournament.thumbnail_image) { + let hostedImageId = await _insertInHostedImage(tournament.thumbnail_image) + await _updateObjectWithHostedImageId(prisma.tournament, tournament.id, { + thumbnail_image_id: hostedImageId, + }) + } + if (tournament.cover_image) { + let hostedImageId = await _insertInHostedImage(tournament.cover_image) + await _updateObjectWithHostedImageId(prisma.tournament, tournament.id, { + cover_image_id: hostedImageId, + }) + } + } + + /** + * TournamentPrize + **/ + const tournamentPrizes = await prisma.tournamentPrize.findMany({ + select: { + id: true, + image: true, + } + }) + for (const tournament of tournamentPrizes) { + if (tournament.image) { + let hostedImageId = await _insertInHostedImage(tournament.image) + await _updateObjectWithHostedImageId(prisma.tournamentPrize, tournament.id, { + image_id: hostedImageId, + }) + } + } + + /** + * TournamentJudge + **/ + const tournamentJudges = await prisma.tournamentJudge.findMany({ + select: { + id: true, + avatar: true, + } + }) + for (const tournament of tournamentJudges) { + if (tournament.avatar) { + let hostedImageId = await _insertInHostedImage(tournament.avatar) + await _updateObjectWithHostedImageId(prisma.tournamentJudge, tournament.id, { + avatar_id: hostedImageId, + }) + } + } + /** + * TournamentEvent + **/ + const tournamentEvents = await prisma.tournamentEvent.findMany({ + select: { + id: true, + image: true, + } + }) + for (const tournament of tournamentEvents) { + if (tournament.image) { + let hostedImageId = await _insertInHostedImage(tournament.image) + await _updateObjectWithHostedImageId(prisma.tournamentEvent, tournament.id, { + image_id: hostedImageId, + }) + } + } } async function _insertInHostedImage(url) {