2
0

👷 Add daily database cleanup action

Closes #201
This commit is contained in:
Baptiste Arnaud
2023-01-10 11:39:56 +01:00
parent b142dc18eb
commit 4c2eaf9b79
4 changed files with 66 additions and 15 deletions

19
.github/workflows/clean-database.yml vendored Normal file
View File

@ -0,0 +1,19 @@
name: Daily database cleanup
on:
schedule:
- cron: '0 6 * * *'
jobs:
publish:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./packages/scripts
env:
DATABASE_URL: '${{ secrets.DATABASE_URL }}'
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.2.2
- run: pnpm i --frozen-lockfile
- run: pnpm db:cleanDatabase

View File

@ -0,0 +1,27 @@
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()
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.`)
}
cleanDatabase().then()

View File

@ -6,6 +6,7 @@
"private": true,
"scripts": {
"playground": "tsx playground.ts",
"db:cleanDatabase": "tsx cleanDatabase.ts",
"db:backup": "tsx backupDatabase.ts",
"db:restore": "tsx restoreDatabase.ts",
"db:setCustomPlan": "tsx setCustomPlan.ts",

View File

@ -2,8 +2,12 @@ import { join } from 'path'
import prompts from 'prompts'
import { isEmpty } from 'utils'
export const promptAndSetEnvironment = async () => {
const response = await prompts({
export const promptAndSetEnvironment = async (
skipPrompt?: 'local' | 'staging' | 'production'
) => {
const response = skipPrompt
? { env: skipPrompt }
: await prompts({
type: 'select',
name: 'env',
message: 'Pick an environment',