flnxi-Wu/scripts/tools/clear-users-for-test.js
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

42 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 临时脚本:清空用户表用于测试首位用户权限功能
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function main() {
try {
console.log('正在检查用户数量...')
const userCount = await prisma.user.count()
console.log(`当前用户数量: ${userCount}`)
if (userCount === 0) {
console.log('✓ 用户表已经是空的,无需清空')
return
}
console.log('正在清空用户表...')
// 使用 deleteMany 而不是 TRUNCATE,因为 Prisma 不支持 TRUNCATE
// 注意:这会级联删除相关的 materials 和 material_likes 记录
const result = await prisma.user.deleteMany({})
console.log(`✓ 已删除 ${result.count} 个用户`)
// 验证
const newCount = await prisma.user.count()
console.log(`清空后用户数量: ${newCount}`)
if (newCount === 0) {
console.log('✓ 用户表已成功清空,可以开始测试首位用户注册功能')
} else {
console.log('⚠ 警告: 用户表清空后仍有数据,请检查')
}
} catch (error) {
console.error('错误:', error)
process.exit(1)
} finally {
await prisma.$disconnect()
}
}
main()