You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 清空全部邮件及相关解<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();
});