clean-dups.ts 900 B

12345678910111213141516171819202122232425262728
  1. const { prisma } = require('../src/models');
  2. (async () => {
  3. // Find duplicate chapterIds in TtsTask
  4. const dups = await prisma.ttsTask.groupBy({
  5. by: ['chapterId'],
  6. _count: { id: true },
  7. having: { id: { _count: { gt: 1 } } },
  8. });
  9. console.log(`Chapters with duplicate TtsTask entries: ${dups.length}`);
  10. let totalDeleted = 0;
  11. for (const d of dups) {
  12. const tasks = await prisma.ttsTask.findMany({
  13. where: { chapterId: d.chapterId },
  14. orderBy: { id: 'desc' },
  15. select: { id: true, status: true }
  16. });
  17. const keepId = tasks[0].id;
  18. const del = await prisma.ttsTask.deleteMany({
  19. where: { chapterId: d.chapterId, id: { not: keepId } }
  20. });
  21. totalDeleted += del.count;
  22. }
  23. const remaining = await prisma.ttsTask.count();
  24. console.log(`Deleted ${totalDeleted} duplicates. Remaining: ${remaining}`);
  25. await prisma.$disconnect();
  26. })();