19
.github/workflows/clean-database.yml
vendored
Normal file
19
.github/workflows/clean-database.yml
vendored
Normal 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
|
27
packages/scripts/cleanDatabase.ts
Normal file
27
packages/scripts/cleanDatabase.ts
Normal 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()
|
@ -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",
|
||||
|
@ -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',
|
||||
|
Reference in New Issue
Block a user