import { PrismaClient } from '@prisma/client'; import * as fs from 'fs'; import * as path from 'path'; import { fileURLToPath } from 'url'; import dotenv from 'dotenv'; dotenv.config(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const prisma = new PrismaClient(); const STORAGE_ROOT = process.env.STORAGE_PATH || path.join(__dirname, '..', 'storage'); async function cleanOrphanedAssets(projectId?: string) { const where = projectId ? { projectId } : {}; const assets = await prisma.asset.findMany({ where }); let cleaned = 0; for (const a of assets) { const fullPath = path.join(STORAGE_ROOT, a.path); if (!fs.existsSync(fullPath)) { console.log('šŸ—‘ļø Deleting orphaned record:', a.type, a.path); await prisma.asset.delete({ where: { id: a.id } }); cleaned++; } } console.log(`\nāœ… Cleaned ${cleaned} orphaned asset records.`); await prisma.$disconnect(); } // Run with optional project ID const projectId = process.argv[2]; cleanOrphanedAssets(projectId);