♻️ Keep guest ids in separate column (#1468)

This commit is contained in:
Luke Vella 2024-12-27 11:12:19 +01:00 committed by GitHub
parent 2d7315f45a
commit 5b3c4ad2f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 171 additions and 58 deletions

View file

@ -0,0 +1,57 @@
-- AlterTable
ALTER TABLE "comments" ADD COLUMN "guest_id" TEXT;
-- AlterTable
ALTER TABLE "participants" ADD COLUMN "guest_id" TEXT;
-- AlterTable
ALTER TABLE "polls" ADD COLUMN "guest_id" TEXT,
ALTER COLUMN "user_id" DROP NOT NULL;
-- CreateIndex
CREATE INDEX "comments_guest_id_idx" ON "comments" USING HASH ("guest_id");
-- CreateIndex
CREATE INDEX "participants_guest_id_idx" ON "participants" USING HASH ("guest_id");
-- CreateIndex
CREATE INDEX "polls_guest_id_idx" ON "polls" USING HASH ("guest_id");
-- Migrate polls
UPDATE "polls" p
SET
"guest_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = p.user_id) THEN p.user_id
ELSE NULL
END,
"user_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = p.user_id) THEN NULL
ELSE p.user_id
END
WHERE p.user_id IS NOT NULL;
-- Migrate participants
UPDATE "participants" p
SET
"guest_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = p.user_id) THEN p.user_id
ELSE NULL
END,
"user_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = p.user_id) THEN NULL
ELSE p.user_id
END
WHERE p.user_id IS NOT NULL;
-- Migrate comments
UPDATE "comments" c
SET
"guest_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = c.user_id) THEN c.user_id
ELSE NULL
END,
"user_id" = CASE
WHEN NOT EXISTS (SELECT 1 FROM "users" u WHERE u.id = c.user_id) THEN NULL
ELSE c.user_id
END
WHERE c.user_id IS NOT NULL;

View file

@ -124,7 +124,8 @@ model Poll {
title String
description String?
location String?
userId String @map("user_id")
userId String? @map("user_id")
guestId String? @map("guest_id")
timeZone String? @map("time_zone")
closed Boolean @default(false) // @deprecated
status PollStatus @default(live)
@ -147,6 +148,7 @@ model Poll {
comments Comment[]
@@index([userId], type: Hash)
@@index([guestId], type: Hash)
@@map("polls")
}
@ -184,6 +186,7 @@ model Participant {
name String
email String?
userId String? @map("user_id")
guestId String? @map("guest_id")
poll Poll @relation(fields: [pollId], references: [id])
pollId String @map("poll_id")
votes Vote[]
@ -194,6 +197,7 @@ model Participant {
deletedAt DateTime? @map("deleted_at")
@@index([pollId], type: Hash)
@@index([guestId], type: Hash)
@@map("participants")
}
@ -241,10 +245,12 @@ model Comment {
authorName String @map("author_name")
user User? @relation(fields: [userId], references: [id])
userId String? @map("user_id")
guestId String? @map("guest_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
@@index([userId], type: Hash)
@@index([guestId], type: Hash)
@@index([pollId], type: Hash)
@@map("comments")
}