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,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"playground": "tsx playground.ts",
|
"playground": "tsx playground.ts",
|
||||||
|
"db:cleanDatabase": "tsx cleanDatabase.ts",
|
||||||
"db:backup": "tsx backupDatabase.ts",
|
"db:backup": "tsx backupDatabase.ts",
|
||||||
"db:restore": "tsx restoreDatabase.ts",
|
"db:restore": "tsx restoreDatabase.ts",
|
||||||
"db:setCustomPlan": "tsx setCustomPlan.ts",
|
"db:setCustomPlan": "tsx setCustomPlan.ts",
|
||||||
|
@ -2,21 +2,25 @@ import { join } from 'path'
|
|||||||
import prompts from 'prompts'
|
import prompts from 'prompts'
|
||||||
import { isEmpty } from 'utils'
|
import { isEmpty } from 'utils'
|
||||||
|
|
||||||
export const promptAndSetEnvironment = async () => {
|
export const promptAndSetEnvironment = async (
|
||||||
const response = await prompts({
|
skipPrompt?: 'local' | 'staging' | 'production'
|
||||||
type: 'select',
|
) => {
|
||||||
name: 'env',
|
const response = skipPrompt
|
||||||
message: 'Pick an environment',
|
? { env: skipPrompt }
|
||||||
choices: [
|
: await prompts({
|
||||||
{
|
type: 'select',
|
||||||
title: 'Local',
|
name: 'env',
|
||||||
value: 'local',
|
message: 'Pick an environment',
|
||||||
},
|
choices: [
|
||||||
{ title: 'Staging', value: 'staging' },
|
{
|
||||||
{ title: 'Production', value: 'production' },
|
title: 'Local',
|
||||||
],
|
value: 'local',
|
||||||
initial: 0,
|
},
|
||||||
})
|
{ title: 'Staging', value: 'staging' },
|
||||||
|
{ title: 'Production', value: 'production' },
|
||||||
|
],
|
||||||
|
initial: 0,
|
||||||
|
})
|
||||||
|
|
||||||
if (isEmpty(response.env)) process.exit()
|
if (isEmpty(response.env)) process.exit()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user