|
|
/**
|
|
|
* 清空全部邮件及相关解<E585B3>?导入/附件记录(保留邮箱账号等设置)<E7BDAE>?
|
|
|
* 用法: pnpm exec tsx scripts/purge-all-mails.ts
|
|
|
*/
|
|
|
import fs from "fs/promises";
|
|
|
import path from "path";
|
|
|
import { prisma } from "@/services/db";
|
|
|
|
|
|
async function rmMailDataDir() {
|
|
|
const root = path.join(process.cwd(), "data", "mails");
|
|
|
try {
|
|
|
await fs.rm(root, { recursive: true, force: true });
|
|
|
await fs.mkdir(root, { recursive: true });
|
|
|
console.log("cleared data/mails/");
|
|
|
} catch (e) {
|
|
|
console.warn("data/mails cleanup skipped", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async function main() {
|
|
|
const before = await prisma.mailMessage.count();
|
|
|
console.log("mails before:", before);
|
|
|
|
|
|
// FK-safe order
|
|
|
await prisma.importCompensation.deleteMany({});
|
|
|
await prisma.containerImport.deleteMany({});
|
|
|
await prisma.containerActiveLock.deleteMany({});
|
|
|
await prisma.parseResult.deleteMany({});
|
|
|
await prisma.mailAttachment.deleteMany({});
|
|
|
await prisma.imapPullLog.deleteMany({});
|
|
|
|
|
|
const deleted = await prisma.mailMessage.deleteMany({});
|
|
|
console.log("deleted mail_message:", deleted.count);
|
|
|
|
|
|
await rmMailDataDir();
|
|
|
|
|
|
const after = await prisma.mailMessage.count();
|
|
|
console.log("mails after:", after);
|
|
|
}
|
|
|
|
|
|
main()
|
|
|
.catch((e) => {
|
|
|
console.error(e);
|
|
|
process.exit(1);
|
|
|
})
|
|
|
.finally(async () => {
|
|
|
await prisma.$disconnect();
|
|
|
});
|