Skip to content

Commit

Permalink
💚 (db) Fix verification bulk delete
Browse files Browse the repository at this point in the history
When it has more than 100000 records
  • Loading branch information
baptisteArno committed Jun 24, 2023
1 parent 2abce89 commit 63e826f
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions packages/scripts/cleanDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,35 @@ const deleteExpiredAppSessions = async () => {
const deleteExpiredVerificationTokens = async () => {
const threeDaysAgo = new Date()
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
const { count } = await prisma.verificationToken.deleteMany({
where: {
expires: {
lte: threeDaysAgo,
let totalVerificationTokens
do {
const verificationTokens = await prisma.verificationToken.findMany({
where: {
expires: {
lte: threeDaysAgo,
},
},
},
})
console.log(`Deleted ${count} expired verifiations tokens.`)
select: {
token: true,
},
take: 80000,
})

totalVerificationTokens = verificationTokens.length

console.log(`Deleting ${verificationTokens.length} expired tokens...`)
const chunkSize = 1000
for (let i = 0; i < verificationTokens.length; i += chunkSize) {
const chunk = verificationTokens.slice(i, i + chunkSize)
await prisma.verificationToken.deleteMany({
where: {
token: {
in: chunk.map((verificationToken) => verificationToken.token),
},
},
})
}
} while (totalVerificationTokens === 80000)
}

const resetQuarantinedWorkspaces = async () =>
Expand Down

0 comments on commit 63e826f

Please sign in to comment.