Skip to content

Commit

Permalink
👷 Add expired records database cleanup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 1, 2023
1 parent 6e0f0e4 commit 1b060da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/scripts/bulkUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'>[]
Expand Down
28 changes: 28 additions & 0 deletions packages/scripts/cleanDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const cleanDatabase = async () => {

console.log('Starting database cleanup...')
await deleteOldChatSessions()
await deleteExpiredAppSessions()
await deleteExpiredVerificationTokens()
console.log('Done!')
}

Expand All @@ -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()

0 comments on commit 1b060da

Please sign in to comment.