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,6 +68,8 @@ const deleteArchivedResults = async () => {
const deleteOldChatSessions = async () => {
const twoDaysAgo = new Date()
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2)
let totalChatSessions
do {
const chatSessions = await prisma.chatSession.findMany({
where: {
updatedAt: {
@ -77,8 +79,11 @@ const deleteOldChatSessions = async () => {
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) {
@ -91,6 +96,7 @@ const deleteOldChatSessions = async () => {
},
})
}
} while (totalChatSessions === 80000)
}
const deleteExpiredAppSessions = async () => {