Files
f0bm/debug/fix_deleted.mjs

57 lines
1.9 KiB
JavaScript

import db from "../src/inc/sql.mjs";
import { promises as fs } from "fs";
(async () => {
console.log("Starting migration...");
// 1. Ensure column exists
try {
await db`select is_deleted from items limit 1`;
console.log("Column 'is_deleted' already exists.");
} catch (err) {
if (err.message.includes('column "is_deleted" does not exist')) {
console.log("Column 'is_deleted' missing. Adding it now...");
await db`ALTER TABLE items ADD COLUMN is_deleted BOOLEAN DEFAULT FALSE`;
console.log("Column added successfully.");
} else {
console.error("Unexpected error checking column:", err);
process.exit(1);
}
}
const items = await db`select id, dest from items where active = false`;
console.log(`Found ${items.length} inactive items.`);
let trashCount = 0;
let pendingCount = 0;
let brokenCount = 0;
for (const item of items) {
try {
await fs.access(`./deleted/b/${item.dest}`);
// File exists in deleted, mark as is_deleted = true
await db`update items set is_deleted = true where id = ${item.id}`;
trashCount++;
} catch {
// Not in deleted, check public
try {
await fs.access(`./public/b/${item.dest}`);
// In public, is_deleted = false (default)
pendingCount++;
} catch {
// Not in either? Broken.
console.log(`Item ${item.id} (${item.dest}) missing from both locations. Deleting...`);
await db`delete from items where id = ${item.id}`;
brokenCount++;
}
}
}
console.log(`Migration complete.`);
console.log(`Trash (soft-deleted): ${trashCount}`);
console.log(`Pending: ${pendingCount}`);
console.log(`Broken: ${brokenCount}`);
process.exit(0);
})();