Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

Reset legacy poll

Luke Vella08f25248698c
diff --git a/migrations/20220412112814_cascade_delete/migration.sql b/migrations/20220412112814_cascade_delete/migration.sql
new file mode 100644
index 000000000..f4ba63272
--- /dev/null
+++ b/migrations/20220412112814_cascade_delete/migration.sql
@@ -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;
diff --git a/pages/api/legacy/[urlId].ts b/pages/api/legacy/[urlId].ts
index 0e617de13..cb38fd781 100644
--- a/pages/api/legacy/[urlId].ts
+++ b/pages/api/legacy/[urlId].ts
@@ -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,
diff --git a/schema.prisma b/schema.prisma
index 3c68c1110..07d1349d5 100644
--- a/schema.prisma
+++ b/schema.prisma
@@ -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])