2
0

💚 Fix cleanDatabase when deleting more than 100,000 sessions

This commit is contained in:
Baptiste Arnaud
2023-06-08 16:18:25 +02:00
parent e9c2deee5f
commit 4977481582

View File

@ -68,29 +68,35 @@ const deleteArchivedResults = async () => {
const deleteOldChatSessions = async () => { const deleteOldChatSessions = async () => {
const twoDaysAgo = new Date() const twoDaysAgo = new Date()
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2) twoDaysAgo.setDate(twoDaysAgo.getDate() - 2)
const chatSessions = await prisma.chatSession.findMany({ let totalChatSessions
where: { do {
updatedAt: { const chatSessions = await prisma.chatSession.findMany({
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({
where: { where: {
id: { updatedAt: {
in: chunk.map((chatSession) => chatSession.id), 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 () => { const deleteExpiredAppSessions = async () => {