2
0

👷 Improve monthly database cleaning script perf

This commit is contained in:
Baptiste Arnaud
2024-08-01 13:37:28 +02:00
parent 7210df4d79
commit 4ebd98855b
2 changed files with 13 additions and 13 deletions

View File

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

View File

@ -6,7 +6,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"playground": "SKIP_ENV_CHECK=true dotenv -e ./.env.local -- tsx playground.ts", "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:backup": "tsx backupDatabase.ts",
"db:restore": "tsx restoreDatabase.ts", "db:restore": "tsx restoreDatabase.ts",
"db:setCustomPlan": "tsx setCustomPlan.ts", "db:setCustomPlan": "tsx setCustomPlan.ts",
@ -58,4 +58,4 @@
"@typebot.io/billing": "workspace:*", "@typebot.io/billing": "workspace:*",
"@typebot.io/telemetry": "workspace:*" "@typebot.io/telemetry": "workspace:*"
} }
} }