import { PrismaClient } from "@prisma/client"; import { PrismaMariaDb } from "@prisma/adapter-mariadb"; import "dotenv/config"; const base = process.env.DATABASE_URL.replace("mysql://", "mariadb://"); const sep = base.includes("?") ? "&" : "?"; const connectionString = `${base}${sep}connection_limit=5&pool_timeout=10`; const adapter = new PrismaMariaDb(connectionString); const prisma = new PrismaClient({ adapter }); const INDUSTRY_SLUGS = [ "ecommerce", "ai-tech", "content", "finance", "cross-border", "real-estate", "food", "local-life", "health", "education", ]; const INDUSTRY_CHILD_SLUGS = [ "ec-platform", "ec-livestream", "ec-private", "ec-group", "ec-dtc", "ec-supply", "ai-tools-app", "ai-startup-forum", "ai-saas", "ai-hardware", "ai-llm", "ct-short-video", "ct-live", "ct-writing", "ct-mcn", "ct-podcast", "fi-stock", "fi-crypto", "fi-insurance", "fi-pe", "fi-forex", "cb-ecommerce", "cb-factory", "cb-logistics", "cb-payment", "cb-brand", "re-residential", "re-commercial", "re-renovation", "re-property", "fd-restaurant", "fd-tea", "fd-prepared", "fd-supply", "fd-delivery", "ll-beauty", "ll-fitness", "ll-pet", "ll-housekeeping", "ll-repair", "ll-edu-local", "hl-cosmetic", "hl-wellness", "hl-elderly", "hl-rehab", "hl-mental", "edu-skills", "edu-knowledge", "edu-abroad", "edu-corporate", "edu-k12", ]; const KEEP_SLUGS = new Set(["industry", ...INDUSTRY_SLUGS, ...INDUSTRY_CHILD_SLUGS]); async function main() { console.log("Cleaning up old forum categories..."); const allCategories = await prisma.forumCategory.findMany({ include: { _count: { select: { topics: true } } }, }); let deleted = 0; let kept = 0; for (const cat of allCategories) { if (KEEP_SLUGS.has(cat.slug)) { kept++; continue; } const topicsCount = cat._count.topics; console.log(` Deleting: ${cat.slug} "${cat.name}" (${topicsCount} topics)`); if (topicsCount > 0) { await prisma.forumTopic.deleteMany({ where: { categoryId: cat.id } }); } await prisma.forumCategory.updateMany({ where: { parentId: cat.id }, data: { parentId: null }, }); await prisma.forumCategory.delete({ where: { id: cat.id } }); deleted++; } console.log(`Done: ${deleted} deleted, ${kept} kept.`); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });