From d73a2f53ef708f63eecc7c6c6effe350a8cbf5d2 Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Tue, 17 Dec 2024 19:07:21 +0000 Subject: [PATCH] Update house keeping tasks --- .github/workflows/house-keeping.yml | 3 +- .../src/app/api/house-keeping/[task]/route.ts | 70 ++----------------- .../migration.sql | 14 ++++ .../migration.sql | 32 +++++++++ .../20241217190416_deleted_at/migration.sql | 2 + packages/database/prisma/schema.prisma | 11 ++- 6 files changed, 63 insertions(+), 69 deletions(-) create mode 100644 packages/database/prisma/migrations/20241217183445_add_poll_id_index/migration.sql create mode 100644 packages/database/prisma/migrations/20241217185339_update_indexes/migration.sql create mode 100644 packages/database/prisma/migrations/20241217190416_deleted_at/migration.sql diff --git a/.github/workflows/house-keeping.yml b/.github/workflows/house-keeping.yml index 2d0bc1077..e3a91b7b8 100644 --- a/.github/workflows/house-keeping.yml +++ b/.github/workflows/house-keeping.yml @@ -18,5 +18,4 @@ jobs: run: | curl -X "POST" --fail "https://app.rallly.co/api/house-keeping/remove-deleted-polls" \ -H "Content-Type: application/json" \ - -H "Authorization: Bearer ${API_SECRET}" \ - -d '{"take": 1000}' + -H "Authorization: Bearer ${API_SECRET}" diff --git a/apps/web/src/app/api/house-keeping/[task]/route.ts b/apps/web/src/app/api/house-keeping/[task]/route.ts index 8d96fe6a2..f570f65c0 100644 --- a/apps/web/src/app/api/house-keeping/[task]/route.ts +++ b/apps/web/src/app/api/house-keeping/[task]/route.ts @@ -2,7 +2,7 @@ import { prisma } from "@rallly/database"; import { headers } from "next/headers"; import { NextResponse } from "next/server"; -export async function POST(req: Request, ctx: { params: { task: string } }) { +export async function POST(_req: Request, ctx: { params: { task: string } }) { const headersList = headers(); const authorization = headersList.get("authorization"); @@ -20,7 +20,7 @@ export async function POST(req: Request, ctx: { params: { task: string } }) { return await deleteInactivePolls(); } case "remove-deleted-polls": { - return await removeDeletedPolls(req); + return await removeDeletedPolls(); } } } @@ -70,82 +70,20 @@ async function deleteInactivePolls() { /** * Remove polls and corresponding data that have been marked deleted for more than 7 days. */ -async function removeDeletedPolls(req: Request) { - const options = (await req.json()) as { take?: number } | undefined; - // First get the ids of all the polls that have been marked as deleted for at least 7 days - const deletedPolls = await prisma.poll.findMany({ - select: { - id: true, - }, +async function removeDeletedPolls() { + const { count: deletedPollCount } = await prisma.poll.deleteMany({ where: { deleted: true, deletedAt: { lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), }, }, - take: options?.take ?? 1000, - }); - - const deletedPollIds = deletedPolls.map((poll) => poll.id); - - const { count: deletedWatcherCount } = await prisma.watcher.deleteMany({ - where: { - pollId: { - in: deletedPollIds, - }, - }, - }); - - const { count: deletedVoteCount } = await prisma.vote.deleteMany({ - where: { - pollId: { - in: deletedPollIds, - }, - }, - }); - - const { count: deletedParticipantCount } = - await prisma.participant.deleteMany({ - where: { - pollId: { - in: deletedPollIds, - }, - }, - }); - - const { count: deletedOptionCount } = await prisma.option.deleteMany({ - where: { - pollId: { - in: deletedPollIds, - }, - }, - }); - - const { count: deletedCommentCount } = await prisma.comment.deleteMany({ - where: { - pollId: { - in: deletedPollIds, - }, - }, - }); - - const { count: deletedPollCount } = await prisma.poll.deleteMany({ - where: { - id: { - in: deletedPollIds, - }, - }, }); return NextResponse.json({ success: true, summary: { deleted: { - votes: deletedVoteCount, - options: deletedOptionCount, - participants: deletedParticipantCount, - comments: deletedCommentCount, - watchers: deletedWatcherCount, polls: deletedPollCount, }, }, diff --git a/packages/database/prisma/migrations/20241217183445_add_poll_id_index/migration.sql b/packages/database/prisma/migrations/20241217183445_add_poll_id_index/migration.sql new file mode 100644 index 000000000..2d5db29ed --- /dev/null +++ b/packages/database/prisma/migrations/20241217183445_add_poll_id_index/migration.sql @@ -0,0 +1,14 @@ +-- CreateIndex +CREATE INDEX "comments_poll_id_idx" ON "comments"("poll_id"); + +-- CreateIndex +CREATE INDEX "options_poll_id_idx" ON "options"("poll_id"); + +-- CreateIndex +CREATE INDEX "participants_poll_id_idx" ON "participants"("poll_id"); + +-- CreateIndex +CREATE INDEX "polls_deleted_touched_at_idx" ON "polls"("deleted", "touched_at"); + +-- CreateIndex +CREATE INDEX "votes_poll_id_idx" ON "votes"("poll_id"); diff --git a/packages/database/prisma/migrations/20241217185339_update_indexes/migration.sql b/packages/database/prisma/migrations/20241217185339_update_indexes/migration.sql new file mode 100644 index 000000000..ae5175c21 --- /dev/null +++ b/packages/database/prisma/migrations/20241217185339_update_indexes/migration.sql @@ -0,0 +1,32 @@ +-- DropIndex +DROP INDEX "comments_poll_id_idx"; + +-- DropIndex +DROP INDEX "options_poll_id_idx"; + +-- DropIndex +DROP INDEX "participants_poll_id_idx"; + +-- DropIndex +DROP INDEX "votes_poll_id_idx"; + +-- CreateIndex +CREATE INDEX "comments_poll_id_idx" ON "comments" USING HASH ("poll_id"); + +-- CreateIndex +CREATE INDEX "options_poll_id_idx" ON "options" USING HASH ("poll_id"); + +-- CreateIndex +CREATE INDEX "participants_poll_id_idx" ON "participants" USING HASH ("poll_id"); + +-- CreateIndex +CREATE INDEX "votes_poll_id_idx" ON "votes" USING HASH ("poll_id"); + +-- CreateIndex +CREATE INDEX "votes_participant_id_idx" ON "votes" USING HASH ("participant_id"); + +-- CreateIndex +CREATE INDEX "votes_option_id_idx" ON "votes" USING HASH ("option_id"); + +-- CreateIndex +CREATE INDEX "watchers_poll_id_idx" ON "watchers" USING HASH ("poll_id"); diff --git a/packages/database/prisma/migrations/20241217190416_deleted_at/migration.sql b/packages/database/prisma/migrations/20241217190416_deleted_at/migration.sql new file mode 100644 index 000000000..dbf3a2dc8 --- /dev/null +++ b/packages/database/prisma/migrations/20241217190416_deleted_at/migration.sql @@ -0,0 +1,2 @@ +-- CreateIndex +CREATE INDEX "polls_deleted_deleted_at_idx" ON "polls"("deleted", "deleted_at"); diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index 2fd6e0362..046dbc9c1 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -146,6 +146,8 @@ model Poll { comments Comment[] @relation("PollToComments") votes Vote[] @relation("PollToVotes") + @@index([deleted, touchedAt]) + @@index([deleted, deletedAt]) @@map("polls") } @@ -173,6 +175,7 @@ model Watcher { poll Poll @relation("PollToWatchers", fields: [pollId], references: [id], onDelete: Cascade) + @@index([pollId], type: Hash) @@map("watchers") } @@ -181,7 +184,6 @@ model Participant { name String email String? userId String? @map("user_id") - poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade) pollId String @map("poll_id") locale String? createdAt DateTime @default(now()) @map("created_at") @@ -190,7 +192,9 @@ model Participant { deletedAt DateTime? @map("deleted_at") votes Vote[] @relation("ParticipantToVotes") + poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade) + @@index([pollId], type: Hash) @@map("participants") } @@ -202,6 +206,7 @@ model Option { poll Poll @relation("PollToOptions", fields: [pollId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) @map("created_at") + @@index([pollId], type: Hash) @@map("options") } @@ -225,6 +230,9 @@ model Vote { participant Participant @relation("ParticipantToVotes", fields: [participantId], references: [id], onDelete: Cascade) poll Poll @relation("PollToVotes", fields: [pollId], references: [id], onDelete: Cascade) + @@index([pollId], type: Hash) + @@index([participantId], type: Hash) + @@index([optionId], type: Hash) @@map("votes") } @@ -240,6 +248,7 @@ model Comment { poll Poll @relation("PollToComments", fields: [pollId], references: [id], onDelete: Cascade) + @@index([pollId], type: Hash) @@map("comments") }