2023-01-10 11:39:56 +01:00
|
|
|
import { PrismaClient } from 'db'
|
|
|
|
import { promptAndSetEnvironment } from './utils'
|
|
|
|
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
|
|
|
|
export const cleanDatabase = async () => {
|
|
|
|
await promptAndSetEnvironment('production')
|
|
|
|
|
|
|
|
console.log('Starting database cleanup...')
|
|
|
|
await deleteOldChatSessions()
|
2023-02-01 09:47:08 +01:00
|
|
|
await deleteExpiredAppSessions()
|
|
|
|
await deleteExpiredVerificationTokens()
|
2023-01-10 11:39:56 +01:00
|
|
|
console.log('Done!')
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteOldChatSessions = async () => {
|
|
|
|
const threeDaysAgo = new Date()
|
|
|
|
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
|
|
|
|
const { count } = await prisma.chatSession.deleteMany({
|
|
|
|
where: {
|
|
|
|
updatedAt: {
|
|
|
|
lte: threeDaysAgo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
console.log(`Deleted ${count} old chat sessions.`)
|
|
|
|
}
|
|
|
|
|
2023-02-01 09:47:08 +01:00
|
|
|
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.`)
|
|
|
|
}
|
|
|
|
|
2023-01-10 11:39:56 +01:00
|
|
|
cleanDatabase().then()
|