const { prisma } = require('../src/models'); (async () => { // Find duplicate chapterIds in TtsTask const dups = await prisma.ttsTask.groupBy({ by: ['chapterId'], _count: { id: true }, having: { id: { _count: { gt: 1 } } }, }); console.log(`Chapters with duplicate TtsTask entries: ${dups.length}`); let totalDeleted = 0; for (const d of dups) { const tasks = await prisma.ttsTask.findMany({ where: { chapterId: d.chapterId }, orderBy: { id: 'desc' }, select: { id: true, status: true } }); const keepId = tasks[0].id; const del = await prisma.ttsTask.deleteMany({ where: { chapterId: d.chapterId, id: { not: keepId } } }); totalDeleted += del.count; } const remaining = await prisma.ttsTask.count(); console.log(`Deleted ${totalDeleted} duplicates. Remaining: ${remaining}`); await prisma.$disconnect(); })();