This commit is contained in:
34
clean_orphaned_assets.ts
Normal file
34
clean_orphaned_assets.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user