Skip to content

Commit e1aca98

Browse files
VitalyyPyuramaxkillev
authored
[752] eat(scripts): add MongoDB data migration script for testimonials and table widgets (#200)
- Add migrate-field-type.js script for MongoDB data migration - Add mongodb and yargs as dependencies --------- Co-authored-by: Yuriy <yuramax@gmail.com> Co-authored-by: Peter Ovchinnikov <peter@boarlabs.xyz>
1 parent b865de8 commit e1aca98

5 files changed

Lines changed: 436 additions & 18 deletions

File tree

website/package-lock.json

Lines changed: 9 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050
"jest-environment-jsdom": "^30.0.0-beta.3",
5151
"lodash": "^4.17.21",
5252
"lozad": "^1.16.0",
53+
"mongodb": "^6.17.0",
5354
"normalize.css": "^8.0.1",
5455
"postmark": "^4.0.5",
5556
"swiper": "^11.2.6",
57+
"yargs": "^17.7.2",
5658
"yup": "^1.6.1"
5759
},
5860
"devDependencies": {
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const { MongoClient } = require('mongodb');
2+
const {
3+
stripHtml,
4+
areaToString,
5+
updateTestimonialFeedback,
6+
updateTableRowsDescriptions,
7+
} = require('./migrate-field-type.utils');
8+
9+
const getCollection = async (
10+
mongoUri = 'mongodb://localhost:27017',
11+
dbName = 'test',
12+
) => {
13+
const client = new MongoClient(mongoUri);
14+
await client.connect();
15+
const db = client.db(dbName);
16+
const collection = db.collection('aposDocs');
17+
return { client, collection };
18+
};
19+
20+
const processBatches = async (
21+
batches,
22+
idKey,
23+
collection,
24+
updateFn = updateTestimonialFeedback,
25+
) => {
26+
let updatedCount = 0;
27+
const allPromises = [];
28+
for (const batch of batches) {
29+
allPromises.push(...batch.map((doc) => updateFn(collection, doc, idKey)));
30+
}
31+
const results = await Promise.all(allPromises);
32+
updatedCount = results.reduce((sum, value) => sum + value, 0);
33+
return updatedCount;
34+
};
35+
36+
const migrateTestimonialFeedbackToString = async (mongoUri, dbName) => {
37+
const { client, collection } = await getCollection(mongoUri, dbName);
38+
try {
39+
const documents = await collection.find({ type: 'testimonials' }).toArray();
40+
const idKey = '_id';
41+
const batchSize = 10;
42+
const batches = [];
43+
for (let i = 0; i < documents.length; i += batchSize) {
44+
batches.push(documents.slice(i, i + batchSize));
45+
}
46+
const updatedCount = await processBatches(batches, idKey, collection);
47+
return updatedCount;
48+
} finally {
49+
await client.close();
50+
}
51+
};
52+
53+
const migrateTableDescriptions = async (mongoUri, dbName) => {
54+
const { client, collection } = await getCollection(mongoUri, dbName);
55+
try {
56+
const docs = await collection
57+
.find({ 'main.items.type': 'table' })
58+
.toArray();
59+
const idKey = '_id';
60+
const batchSize = 10;
61+
const batches = [];
62+
for (let i = 0; i < docs.length; i += batchSize) {
63+
batches.push(docs.slice(i, i + batchSize));
64+
}
65+
const updatedCount = await processBatches(
66+
batches,
67+
idKey,
68+
collection,
69+
updateTableRowsDescriptions,
70+
);
71+
return updatedCount;
72+
} finally {
73+
await client.close();
74+
}
75+
};
76+
77+
if (require.main === module) {
78+
const yargs = require('yargs/yargs');
79+
const { hideBin } = require('yargs/helpers');
80+
const { argv } = yargs(hideBin(process.argv))
81+
.option('mongoUri', {
82+
describe: 'MongoDB connection URI',
83+
type: 'string',
84+
demandOption: true,
85+
})
86+
.option('dbName', {
87+
describe: 'MongoDB database name',
88+
type: 'string',
89+
demandOption: true,
90+
})
91+
.help()
92+
.alias('help', 'h');
93+
94+
const MONGODB_URI = argv.mongoUri;
95+
const DB_NAME = argv.dbName;
96+
97+
(async () => {
98+
try {
99+
const testimonials = await migrateTestimonialFeedbackToString(
100+
MONGODB_URI,
101+
DB_NAME,
102+
);
103+
process.stdout.write(`Updated testimonials: ${testimonials}\n`);
104+
const tables = await migrateTableDescriptions(MONGODB_URI, DB_NAME);
105+
process.stdout.write(`Updated table rows: ${tables}\n`);
106+
} catch (error) {
107+
process.stdout.write(`Migration error: ${error}\n`);
108+
throw error;
109+
}
110+
})();
111+
}
112+
113+
module.exports = {
114+
stripHtml,
115+
areaToString,
116+
updateTestimonialFeedback,
117+
updateTableRowsDescriptions,
118+
getCollection,
119+
processBatches,
120+
migrateTestimonialFeedbackToString,
121+
migrateTableDescriptions,
122+
};

0 commit comments

Comments
 (0)