Skip to content

Commit

Permalink
👷 Improve monthly database cleaning script perf
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 12, 2024
1 parent 7210df4 commit 4ebd988
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions packages/scripts/cleanDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const deleteArchivedTypebots = async () => {
lastDayTwoMonthsAgo.setMonth(lastDayTwoMonthsAgo.getMonth() - 1)
lastDayTwoMonthsAgo.setDate(0)

console.log(`Fetching archived typebots...`)
const typebots = await prisma.typebot.findMany({
where: {
updatedAt: {
Expand Down Expand Up @@ -54,21 +55,20 @@ const deleteArchivedTypebots = async () => {
}

const deleteArchivedResults = async () => {
const resultsBatch = 10000
const lastDayTwoMonthsAgo = new Date()
lastDayTwoMonthsAgo.setMonth(lastDayTwoMonthsAgo.getMonth() - 1)
lastDayTwoMonthsAgo.setDate(0)
let totalResults
do {
const results = await prisma.result.findMany({
where: {
createdAt: {
lte: lastDayTwoMonthsAgo,
},
isArchived: true,
},
select: { id: true },
take: 80000,
})
console.log(`Fetching ${resultsBatch} archived results...`)
const results = (await prisma.$queryRaw`
SELECT id
FROM Result
WHERE createdAt <= ${lastDayTwoMonthsAgo}
AND isArchived = true
LIMIT ${resultsBatch}
`) as { id: string }[]
totalResults = results.length
console.log(`Deleting ${results.length} archived results...`)
const chunkSize = 1000
Expand All @@ -82,7 +82,7 @@ const deleteArchivedResults = async () => {
},
})
}
} while (totalResults === 80000)
} while (totalResults === resultsBatch)

console.log('Done!')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"scripts": {
"playground": "SKIP_ENV_CHECK=true dotenv -e ./.env.local -- tsx playground.ts",
"db:cleanDatabase": "tsx cleanDatabase.ts",
"db:cleanDatabase": "SKIP_ENV_CHECK=true tsx cleanDatabase.ts",
"db:backup": "tsx backupDatabase.ts",
"db:restore": "tsx restoreDatabase.ts",
"db:setCustomPlan": "tsx setCustomPlan.ts",
Expand Down Expand Up @@ -58,4 +58,4 @@
"@typebot.io/billing": "workspace:*",
"@typebot.io/telemetry": "workspace:*"
}
}
}

0 comments on commit 4ebd988

Please sign in to comment.