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>, res: NextApiResponse<GetPollApiResponse>,
) { ) {
const urlId = getQueryParam(req, "urlId"); const urlId = getQueryParam(req, "urlId");
const reset = req.query.reset;
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 client = await getMongoClient(); const client = await getMongoClient();
if (!client) { 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({ const poll = await prisma.poll.create({
data: { data: {
legacy: true, legacy: true,

View file

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