import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function cleanupOrphanedData() { console.log('๐Ÿ” Checking for orphaned video references...'); try { // Get all existing video IDs const existingVideos = await prisma.video.findMany({ select: { id: true } }); const existingVideoIds = new Set(existingVideos.map(v => v.id)); console.log(`๐Ÿ“น Found ${existingVideoIds.size} existing videos`); let totalCleaned = 0; // 1. Clean orphaned VideoProgress records const allProgress = await prisma.videoProgress.findMany({ select: { id: true, videoId: true, userId: true } }); const orphanedProgress = allProgress.filter(p => !existingVideoIds.has(p.videoId)); if (orphanedProgress.length > 0) { console.log(`๐Ÿงน Found ${orphanedProgress.length} orphaned VideoProgress records`); await prisma.videoProgress.deleteMany({ where: { id: { in: orphanedProgress.map(p => p.id) } } }); console.log(`โœ… Deleted ${orphanedProgress.length} orphaned VideoProgress records`); totalCleaned += orphanedProgress.length; } // 2. Clean orphaned VideoWatchSegment records const allSegments = await prisma.videoWatchSegment.findMany({ select: { id: true, videoId: true, userId: true } }); const orphanedSegments = allSegments.filter(s => !existingVideoIds.has(s.videoId)); if (orphanedSegments.length > 0) { console.log(`๐Ÿงน Found ${orphanedSegments.length} orphaned VideoWatchSegment records`); await prisma.videoWatchSegment.deleteMany({ where: { id: { in: orphanedSegments.map(s => s.id) } } }); console.log(`โœ… Deleted ${orphanedSegments.length} orphaned VideoWatchSegment records`); totalCleaned += orphanedSegments.length; } // 3. Clean orphaned VideoUnlock records const allUnlocks = await prisma.videoUnlock.findMany({ select: { id: true, videoId: true, userId: true } }); const orphanedUnlocks = allUnlocks.filter(u => !existingVideoIds.has(u.videoId)); if (orphanedUnlocks.length > 0) { console.log(`๐Ÿงน Found ${orphanedUnlocks.length} orphaned VideoUnlock records`); await prisma.videoUnlock.deleteMany({ where: { id: { in: orphanedUnlocks.map(u => u.id) } } }); console.log(`โœ… Deleted ${orphanedUnlocks.length} orphaned VideoUnlock records`); totalCleaned += orphanedUnlocks.length; } // 4. Clean orphaned VideoLike records const allLikes = await prisma.videoLike.findMany({ select: { id: true, videoId: true, userId: true } }); const orphanedLikes = allLikes.filter(l => !existingVideoIds.has(l.videoId)); if (orphanedLikes.length > 0) { console.log(`๐Ÿงน Found ${orphanedLikes.length} orphaned VideoLike records`); await prisma.videoLike.deleteMany({ where: { id: { in: orphanedLikes.map(l => l.id) } } }); console.log(`โœ… Deleted ${orphanedLikes.length} orphaned VideoLike records`); totalCleaned += orphanedLikes.length; } // 5. Clean orphaned Comment records const allComments = await prisma.comment.findMany({ select: { id: true, videoId: true, userId: true } }); const orphanedComments = allComments.filter(c => !existingVideoIds.has(c.videoId)); if (orphanedComments.length > 0) { console.log(`๐Ÿงน Found ${orphanedComments.length} orphaned Comment records`); await prisma.comment.deleteMany({ where: { id: { in: orphanedComments.map(c => c.id) } } }); console.log(`โœ… Deleted ${orphanedComments.length} orphaned Comment records`); totalCleaned += orphanedComments.length; } // 6. Clean orphaned VideoCourse records const allVideoCourses = await prisma.videoCourse.findMany({ select: { id: true, videoId: true, courseId: true } }); const orphanedVideoCourses = allVideoCourses.filter(vc => !existingVideoIds.has(vc.videoId)); if (orphanedVideoCourses.length > 0) { console.log(`๐Ÿงน Found ${orphanedVideoCourses.length} orphaned VideoCourse records`); await prisma.videoCourse.deleteMany({ where: { id: { in: orphanedVideoCourses.map(vc => vc.id) } } }); console.log(`โœ… Deleted ${orphanedVideoCourses.length} orphaned VideoCourse records`); totalCleaned += orphanedVideoCourses.length; } // 7. Report summary if (totalCleaned === 0) { console.log('โœจ No orphaned video references found! Database is clean.'); } else { console.log(`๐ŸŽ‰ Cleanup complete! Removed ${totalCleaned} total orphaned records.`); } } catch (error) { console.error('โŒ Error during cleanup:', error); } finally { await prisma.$disconnect(); } } // Run the cleanup cleanupOrphanedData() .then(() => { console.log('๐Ÿ”ง Cleanup script completed'); process.exit(0); }) .catch((error) => { console.error('๐Ÿ’ฅ Script failed:', error); process.exit(1); });