-
Notifications
You must be signed in to change notification settings - Fork 1
[752] eat(scripts): add MongoDB data migration script for testimonials and table widgets #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd06c79
install mongodb and yargs
VitalyyP e7c141f
create migrate-field-type.js
VitalyyP e986114
fix function expression
VitalyyP 565321e
fix
VitalyyP 2f3d6e9
decomposite and add tests
VitalyyP fe38ca8
Refactor migrate-field-type script for improved testability
VitalyyP 93b28d8
Refactor areaData check to use optional chaining
VitalyyP ae4bcfc
Fix feedback update condition in testimonials migration util
VitalyyP 2389e3b
Merge branch 'main' into 752-scripts/data-migration
yuramax c5c76cb
Merge branch 'main' into 752-scripts/data-migration
killev dff3e9a
Merge branch 'main' into 752-scripts/data-migration
killev 2235178
Merge branch 'main' into 752-scripts/data-migration
killev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| const { MongoClient } = require('mongodb'); | ||
| const yargs = require('yargs/yargs'); | ||
| const { hideBin } = require('yargs/helpers'); | ||
|
|
||
| const { argv } = yargs(hideBin(process.argv)) | ||
| .option('mongoUri', { | ||
| describe: 'MongoDB connection URI', | ||
| type: 'string', | ||
| demandOption: true, | ||
| }) | ||
| .option('dbName', { | ||
| describe: 'MongoDB database name', | ||
| type: 'string', | ||
| demandOption: true, | ||
| }) | ||
| .help() | ||
| .alias('help', 'h'); | ||
|
|
||
| const MONGODB_URI = argv.mongoUri; | ||
| const DB_NAME = argv.dbName; | ||
|
|
||
| const stripHtml = function (html) { | ||
| if (typeof html !== 'string') return ''; | ||
| let temp = html; | ||
| while (temp.includes('<p') || temp.includes('<span')) { | ||
| const startP = temp.indexOf('<p'); | ||
| const startSpan = temp.indexOf('<span'); | ||
| let start = -1; | ||
| if (startP !== -1 && (startSpan === -1 || startP < startSpan)) { | ||
| start = startP; | ||
| } else if (startSpan !== -1) { | ||
| start = startSpan; | ||
| } | ||
| if (start === -1) break; | ||
| const end = temp.indexOf('>', start); | ||
| if (end === -1) break; | ||
| temp = temp.slice(0, start) + temp.slice(end + 1); | ||
| } | ||
| temp = temp.replaceAll('</p>', '').replaceAll('</span>', ''); | ||
| return temp; | ||
| }; | ||
|
|
||
| const areaToString = function (areaData) { | ||
| if (typeof areaData === 'string') return stripHtml(areaData); | ||
| if (areaData && areaData.items && areaData.items.length > 0) { | ||
| return areaData.items | ||
| .map(function (item) { | ||
| if (typeof item.content === 'string') { | ||
| return stripHtml(item.content); | ||
| } | ||
| return ''; | ||
| }) | ||
| .join(' ') | ||
| .trim(); | ||
| } | ||
| return ''; | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
|
||
|
|
||
| const getCollection = async function () { | ||
| const client = new MongoClient(MONGODB_URI); | ||
| await client.connect(); | ||
| const db = client.db(DB_NAME); | ||
| const collection = db.collection('aposDocs'); | ||
| return { client, collection }; | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
|
||
|
|
||
| const updateTestimonialFeedback = async function (collection, doc, idKey) { | ||
| if ( | ||
| typeof doc.feedback !== 'string' && | ||
| doc.feedback && | ||
| typeof doc.feedback === 'object' | ||
| ) { | ||
| const fixedFeedback = areaToString(doc.feedback); | ||
| if (typeof fixedFeedback === 'string' && fixedFeedback !== doc.feedback) { | ||
| await collection.updateOne( | ||
| { [idKey]: doc[idKey] }, | ||
| { $set: { feedback: fixedFeedback } }, | ||
| ); | ||
| return 1; | ||
| } | ||
| } | ||
| return 0; | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
VitalyyP marked this conversation as resolved.
|
||
|
|
||
| const migrateTestimonialFeedbackToString = async function () { | ||
| const { client, collection } = await getCollection(); | ||
| try { | ||
| const documents = await collection.find({ type: 'testimonials' }).toArray(); | ||
| const idKey = '_id'; | ||
| const updatePromises = documents.map(function (doc) { | ||
| return updateTestimonialFeedback(collection, doc, idKey); | ||
| }); | ||
| const results = await Promise.all(updatePromises); | ||
| return results.reduce(function (sum, value) { | ||
| return sum + value; | ||
| }, 0); | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
|
||
|
|
||
| const updateTableRowsDescriptions = async function (collection, doc, idKey) { | ||
| let changed = false; | ||
| if (doc.main && Array.isArray(doc.main.items)) { | ||
| const newItems = doc.main.items.map(function (widget) { | ||
| if (widget.type === 'table' && Array.isArray(widget.rows)) { | ||
| const newRows = widget.rows.map(function (row) { | ||
| if ( | ||
| row && | ||
| typeof row.description === 'object' && | ||
| row.description !== null && | ||
| row.description.items | ||
| ) { | ||
| changed = true; | ||
| return { ...row, description: areaToString(row.description) }; | ||
| } | ||
| return row; | ||
| }); | ||
| return { ...widget, rows: newRows }; | ||
| } | ||
| return widget; | ||
| }); | ||
| if (changed) { | ||
| await collection.updateOne( | ||
| { [idKey]: doc[idKey] }, | ||
| { $set: { 'main.items': newItems } }, | ||
| ); | ||
| return 1; | ||
| } | ||
| } | ||
| return 0; | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
|
||
|
|
||
| const migrateTableDescriptions = async function () { | ||
| const { client, collection } = await getCollection(); | ||
| try { | ||
| const docs = await collection | ||
| .find({ 'main.items.type': 'table' }) | ||
| .toArray(); | ||
| const idKey = '_id'; | ||
| const updatePromises = docs.map(function (doc) { | ||
| return updateTableRowsDescriptions(collection, doc, idKey); | ||
| }); | ||
| const results = await Promise.all(updatePromises); | ||
| return results.reduce(function (sum, value) { | ||
| return sum + value; | ||
| }, 0); | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| }; | ||
|
VitalyyP marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (require.main === module) { | ||
| (async function () { | ||
| try { | ||
| const testimonials = await migrateTestimonialFeedbackToString(); | ||
| process.stdout.write(`Updated testimonials: ${testimonials}\n`); | ||
| const tables = await migrateTableDescriptions(); | ||
| process.stdout.write(`Updated table rows: ${tables}\n`); | ||
| } catch (error) { | ||
| process.stdout.write(`Migration error: ${error}\n`); | ||
| throw error; | ||
| } | ||
| })(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.