2
0

👷 Add expired records database cleanup scripts

This commit is contained in:
Baptiste Arnaud
2023-02-01 09:47:08 +01:00
parent 6e0f0e487b
commit 1b060dac2d
2 changed files with 29 additions and 1 deletions

View File

@@ -21,7 +21,7 @@ const bulkUpdate = async () => {
const results = (await prisma.result.findMany({
where: {
variables: { isEmpty: false },
variables: { equals: [] },
},
select: { variables: true, id: true },
})) as Pick<Result, 'variables' | 'id'>[]

View File

@@ -8,6 +8,8 @@ export const cleanDatabase = async () => {
console.log('Starting database cleanup...')
await deleteOldChatSessions()
await deleteExpiredAppSessions()
await deleteExpiredVerificationTokens()
console.log('Done!')
}
@@ -24,4 +26,30 @@ const deleteOldChatSessions = async () => {
console.log(`Deleted ${count} old chat sessions.`)
}
const deleteExpiredAppSessions = async () => {
const threeDaysAgo = new Date()
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
const { count } = await prisma.session.deleteMany({
where: {
expires: {
lte: threeDaysAgo,
},
},
})
console.log(`Deleted ${count} expired user sessions.`)
}
const deleteExpiredVerificationTokens = async () => {
const threeDaysAgo = new Date()
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
const { count } = await prisma.verificationToken.deleteMany({
where: {
expires: {
lte: threeDaysAgo,
},
},
})
console.log(`Deleted ${count} expired verifiations tokens.`)
}
cleanDatabase().then()