Monorepo for Aesthetic.Computer
aesthetic.computer
1#!/usr/bin/env node
2// ants/migrate-news-codes.mjs
3// One-shot migration: prefix all news codes with "n"
4//
5// Updates:
6// news-posts.code: "abc" → "nabc"
7// news-comments.postCode: "abc" → "nabc"
8// news-votes.itemId (where itemType === "post"): "abc" → "nabc"
9//
10// Usage:
11// node ants/migrate-news-codes.mjs # dry run (default)
12// node ants/migrate-news-codes.mjs --apply # apply changes
13//
14// Required env vars: MONGODB_CONNECTION_STRING, MONGODB_NAME
15
16import { MongoClient } from "mongodb";
17
18const dryRun = !process.argv.includes("--apply");
19
20const uri = process.env.MONGODB_CONNECTION_STRING;
21const dbName = process.env.MONGODB_NAME;
22
23if (!uri || !dbName) {
24 console.error("Missing MONGODB_CONNECTION_STRING or MONGODB_NAME");
25 process.exit(1);
26}
27
28async function migrate() {
29 const client = new MongoClient(uri);
30 await client.connect();
31 const db = client.db(dbName);
32
33 console.log(`📰 News code migration (n-prefix)`);
34 console.log(` Mode: ${dryRun ? "DRY RUN" : "APPLY"}\n`);
35
36 // 1. Migrate news-posts.code
37 const posts = db.collection("news-posts");
38 const allPosts = await posts.find({ code: { $not: /^n/ } }).toArray();
39 console.log(` news-posts to migrate: ${allPosts.length}`);
40
41 for (const post of allPosts) {
42 const newCode = `n${post.code}`;
43 if (dryRun) {
44 console.log(` [dry] ${post.code} → ${newCode} "${post.title?.slice(0, 40)}"`);
45 } else {
46 await posts.updateOne({ _id: post._id }, { $set: { code: newCode } });
47 console.log(` ✓ ${post.code} → ${newCode}`);
48 }
49 }
50
51 // 2. Migrate news-comments.postCode
52 const comments = db.collection("news-comments");
53 const allComments = await comments.find({ postCode: { $not: /^n/ } }).toArray();
54 console.log(`\n news-comments to migrate: ${allComments.length}`);
55
56 for (const comment of allComments) {
57 const newPostCode = `n${comment.postCode}`;
58 if (dryRun) {
59 console.log(` [dry] postCode ${comment.postCode} → ${newPostCode}`);
60 } else {
61 await comments.updateOne({ _id: comment._id }, { $set: { postCode: newPostCode } });
62 console.log(` ✓ postCode ${comment.postCode} → ${newPostCode}`);
63 }
64 }
65
66 // 3. Migrate news-votes.itemId (post votes only)
67 const votes = db.collection("news-votes");
68 const postVotes = await votes.find({ itemType: "post", itemId: { $not: /^n/ } }).toArray();
69 console.log(`\n news-votes (post) to migrate: ${postVotes.length}`);
70
71 for (const vote of postVotes) {
72 const newItemId = `n${vote.itemId}`;
73 if (dryRun) {
74 console.log(` [dry] itemId ${vote.itemId} → ${newItemId}`);
75 } else {
76 await votes.updateOne({ _id: vote._id }, { $set: { itemId: newItemId } });
77 console.log(` ✓ itemId ${vote.itemId} → ${newItemId}`);
78 }
79 }
80
81 console.log(`\n${dryRun ? "🔍 Dry run complete. Use --apply to execute." : "✅ Migration complete."}`);
82
83 await client.close();
84}
85
86migrate().catch((err) => {
87 console.error("Migration failed:", err);
88 process.exit(1);
89});