68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
// 数据库清理脚本
|
||
const { PrismaClient } = require('@prisma/client')
|
||
|
||
const prisma = new PrismaClient()
|
||
|
||
async function cleanup() {
|
||
try {
|
||
console.log('开始数据库清理...\n')
|
||
|
||
// 1. 清理过期的邀请码(已使用且过期超过30天)
|
||
console.log('1. 清理过期的邀请码...')
|
||
const expiredInvitations = await prisma.$executeRawUnsafe(`
|
||
DELETE FROM invitations
|
||
WHERE "usedAt" IS NOT NULL
|
||
AND "expiresAt" IS NOT NULL
|
||
AND "expiresAt" < NOW() - INTERVAL '30 days'
|
||
`)
|
||
console.log(` 删除了 ${expiredInvitations} 条过期邀请码`)
|
||
|
||
// 2. 清理旧的弹幕数据(超过90天)
|
||
console.log('2. 清理旧的弹幕数据...')
|
||
const oldDanmakus = await prisma.$executeRawUnsafe(`
|
||
DELETE FROM danmakus
|
||
WHERE "createdAt" < NOW() - INTERVAL '90 days'
|
||
`)
|
||
console.log(` 删除了 ${oldDanmakus} 条旧弹幕`)
|
||
|
||
// 3. 获取清理后的统计信息
|
||
console.log('\n3. 获取清理后的统计信息...')
|
||
const stats = await prisma.$queryRawUnsafe(`
|
||
SELECT
|
||
'invitations' as table_name,
|
||
COUNT(*)::bigint as count
|
||
FROM invitations
|
||
UNION ALL
|
||
SELECT
|
||
'danmakus' as table_name,
|
||
COUNT(*)::bigint as count
|
||
FROM danmakus
|
||
UNION ALL
|
||
SELECT
|
||
'materials' as table_name,
|
||
COUNT(*)::bigint as count
|
||
FROM materials
|
||
UNION ALL
|
||
SELECT
|
||
'users' as table_name,
|
||
COUNT(*)::bigint as count
|
||
FROM users
|
||
`)
|
||
|
||
console.log('\n清理后的数据统计:')
|
||
stats.forEach((stat) => {
|
||
console.log(` ${stat.table_name}: ${stat.count}`)
|
||
})
|
||
|
||
console.log('\n数据库清理完成!')
|
||
} catch (error) {
|
||
console.error('清理失败:', error)
|
||
process.exit(1)
|
||
} finally {
|
||
await prisma.$disconnect()
|
||
}
|
||
}
|
||
|
||
cleanup()
|
||
|