Files
digicraft-be/clean_orphaned_assets.ts
Fahri Can Seçer 80dcf4d04a
Some checks failed
Deploy Backend / deploy (push) Has been cancelled
main
2026-02-05 01:29:22 +03:00

35 lines
1.1 KiB
TypeScript

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);