Reset legacy poll

This commit is contained in:
Luke Vella 2022-04-12 12:32:34 +01:00
parent e830d0b0f4
commit 08f2524869
3 changed files with 25 additions and 10 deletions

View file

@ -0,0 +1,17 @@
-- DropForeignKey
ALTER TABLE "Comment" DROP CONSTRAINT "Comment_pollId_fkey";
-- DropForeignKey
ALTER TABLE "Link" DROP CONSTRAINT "Link_pollId_fkey";
-- DropForeignKey
ALTER TABLE "Option" DROP CONSTRAINT "Option_pollId_fkey";
-- AddForeignKey
ALTER TABLE "Link" ADD CONSTRAINT "Link_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("urlId") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Option" ADD CONSTRAINT "Option_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("urlId") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("urlId") ON DELETE CASCADE ON UPDATE CASCADE;

View file

@ -46,13 +46,7 @@ export default async function handler(
res: NextApiResponse<GetPollApiResponse>,
) {
const urlId = getQueryParam(req, "urlId");
const existingPoll = await prisma.poll.findUnique({ where: { urlId } });
if (existingPoll) {
// if the poll already exists with this id then we probably shouldn't be here
return res.status(400).end();
}
const reset = req.query.reset;
const client = await getMongoClient();
if (!client) {
@ -105,6 +99,10 @@ export default async function handler(
});
});
if (reset) {
await prisma.poll.delete({ where: { urlId: legacyPoll._id } });
}
const poll = await prisma.poll.create({
data: {
legacy: true,

View file

@ -57,7 +57,7 @@ model Link {
urlId String @id @unique
role Role
pollId String
poll Poll @relation(fields: [pollId], references: [urlId])
poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade)
createdAt DateTime @default(now())
@@unique([pollId, role])
@ -79,7 +79,7 @@ model Option {
id String @id @default(cuid())
value String
pollId String
poll Poll @relation(fields: [pollId], references: [urlId])
poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade)
votes Vote[]
}
@ -96,7 +96,7 @@ model Vote {
model Comment {
id String @id @default(cuid())
content String
poll Poll @relation(fields:[pollId], references: [urlId])
poll Poll @relation(fields:[pollId], references: [urlId], onDelete: Cascade)
pollId String
authorName String
user User? @relation(fields: [userId], references: [id])