From 49774815820c6dffb8d4dc1c0493743521c055e4 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 8 Jun 2023 16:18:25 +0200 Subject: [PATCH] :green_heart: Fix cleanDatabase when deleting more than 100,000 sessions --- packages/scripts/cleanDatabase.ts | 44 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/scripts/cleanDatabase.ts b/packages/scripts/cleanDatabase.ts index f8be14450f..5af580b18a 100644 --- a/packages/scripts/cleanDatabase.ts +++ b/packages/scripts/cleanDatabase.ts @@ -68,29 +68,35 @@ const deleteArchivedResults = async () => { const deleteOldChatSessions = async () => { const twoDaysAgo = new Date() twoDaysAgo.setDate(twoDaysAgo.getDate() - 2) - const chatSessions = await prisma.chatSession.findMany({ - where: { - updatedAt: { - lte: twoDaysAgo, - }, - }, - select: { - id: true, - }, - }) - - console.log(`Deleting ${chatSessions.length} old chat sessions...`) - const chunkSize = 1000 - for (let i = 0; i < chatSessions.length; i += chunkSize) { - const chunk = chatSessions.slice(i, i + chunkSize) - await prisma.chatSession.deleteMany({ + let totalChatSessions + do { + const chatSessions = await prisma.chatSession.findMany({ where: { - id: { - in: chunk.map((chatSession) => chatSession.id), + updatedAt: { + lte: twoDaysAgo, }, }, + select: { + id: true, + }, + take: 80000, }) - } + + totalChatSessions = chatSessions.length + + console.log(`Deleting ${chatSessions.length} old chat sessions...`) + const chunkSize = 1000 + for (let i = 0; i < chatSessions.length; i += chunkSize) { + const chunk = chatSessions.slice(i, i + chunkSize) + await prisma.chatSession.deleteMany({ + where: { + id: { + in: chunk.map((chatSession) => chatSession.id), + }, + }, + }) + } + } while (totalChatSessions === 80000) } const deleteExpiredAppSessions = async () => {