2
0

💚 (db) Fix verification bulk delete

When it has more than 100000 records
This commit is contained in:
Baptiste Arnaud
2023-06-24 09:02:56 +02:00
parent 2abce89a46
commit 63e826f991

View File

@@ -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 () =>