-
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 5 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,112 @@ | ||
| const { MongoClient } = require('mongodb'); | ||
| const yargs = require('yargs/yargs'); | ||
| const { hideBin } = require('yargs/helpers'); | ||
| const { | ||
| stripHtml, | ||
| areaToString, | ||
| updateTestimonialFeedback, | ||
| updateTableRowsDescriptions, | ||
| } = require('./migrate-field-type.utils'); | ||
|
|
||
| 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 getCollection = async () => { | ||
| const client = new MongoClient(MONGODB_URI); | ||
| await client.connect(); | ||
| const db = client.db(DB_NAME); | ||
| const collection = db.collection('aposDocs'); | ||
| return { client, collection }; | ||
| }; | ||
|
|
||
| const processBatches = async ( | ||
| batches, | ||
| idKey, | ||
| collection, | ||
| updateFn = updateTestimonialFeedback, | ||
| ) => { | ||
| let updatedCount = 0; | ||
| const allPromises = []; | ||
| for (const batch of batches) { | ||
| allPromises.push(...batch.map((doc) => updateFn(collection, doc, idKey))); | ||
| } | ||
| const results = await Promise.all(allPromises); | ||
| updatedCount = results.reduce((sum, value) => sum + value, 0); | ||
| return updatedCount; | ||
| }; | ||
|
|
||
| const migrateTestimonialFeedbackToString = async () => { | ||
| const { client, collection } = await getCollection(); | ||
| try { | ||
| const documents = await collection.find({ type: 'testimonials' }).toArray(); | ||
| const idKey = '_id'; | ||
| const batchSize = 10; | ||
| const batches = []; | ||
| for (let i = 0; i < documents.length; i += batchSize) { | ||
| batches.push(documents.slice(i, i + batchSize)); | ||
| } | ||
| const updatedCount = await processBatches(batches, idKey, collection); | ||
| return updatedCount; | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| }; | ||
|
|
||
| const migrateTableDescriptions = async () => { | ||
| const { client, collection } = await getCollection(); | ||
| try { | ||
| const docs = await collection | ||
| .find({ 'main.items.type': 'table' }) | ||
| .toArray(); | ||
| const idKey = '_id'; | ||
| const batchSize = 10; | ||
| const batches = []; | ||
| for (let i = 0; i < docs.length; i += batchSize) { | ||
| batches.push(docs.slice(i, i + batchSize)); | ||
| } | ||
| const updatedCount = await processBatches( | ||
| batches, | ||
| idKey, | ||
| collection, | ||
| updateTableRowsDescriptions, | ||
| ); | ||
| return updatedCount; | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| }; | ||
|
|
||
| if (require.main === module) { | ||
| (async () => { | ||
| 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; | ||
| } | ||
| })(); | ||
| } | ||
|
|
||
| module.exports = { | ||
| stripHtml, | ||
| areaToString, | ||
| updateTestimonialFeedback, | ||
| updateTableRowsDescriptions, | ||
| }; | ||
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,107 @@ | ||
| const { | ||
| stripHtml, | ||
| areaToString, | ||
| updateTestimonialFeedback, | ||
| updateTableRowsDescriptions, | ||
| } = require('./migrate-field-type.utils'); | ||
|
|
||
| describe('stripHtml', () => { | ||
| test('removes <p> and <span> tags and their closing tags', () => { | ||
| expect(stripHtml('<p>Text</p>')).toBe('Text'); | ||
| expect(stripHtml('<span>Text</span>')).toBe('Text'); | ||
| expect(stripHtml('<p><span>Nested</span></p>')).toBe('Nested'); | ||
| expect(stripHtml('No tags')).toBe('No tags'); | ||
| }); | ||
|
|
||
| test('returns empty string for non-string input', () => { | ||
| expect(stripHtml(null)).toBe(''); | ||
| expect(stripHtml(undefined)).toBe(''); | ||
| expect(stripHtml(123)).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('areaToString', () => { | ||
| test('returns string as is (after stripHtml)', () => { | ||
| expect(areaToString('<p>abc</p>')).toBe('abc'); | ||
| }); | ||
|
|
||
| test('joins items content and strips HTML', () => { | ||
| const area = { | ||
| items: [{ content: '<p>foo</p>' }, { content: '<span>bar</span>' }], | ||
| }; | ||
| expect(areaToString(area)).toBe('foo bar'); | ||
| }); | ||
|
|
||
| test('returns empty string for empty or invalid area', () => { | ||
| expect(areaToString({ items: [] })).toBe(''); | ||
| expect(areaToString({})).toBe(''); | ||
| expect(areaToString(null)).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateTestimonialFeedback', () => { | ||
| test('updates feedback if it is an area object', async () => { | ||
| const collection = { updateOne: jest.fn().mockResolvedValue({}) }; | ||
| const doc = { _id: 1, feedback: { items: [{ content: '<p>abc</p>' }] } }; | ||
| const result = await updateTestimonialFeedback(collection, doc, '_id'); | ||
| expect(result).toBe(1); | ||
| expect(collection.updateOne).toHaveBeenCalledWith( | ||
| { _id: 1 }, | ||
| { $set: { feedback: 'abc' } }, | ||
| ); | ||
| }); | ||
|
|
||
| test('does not update if feedback is already string', async () => { | ||
| const collection = { updateOne: jest.fn() }; | ||
| const doc = { _id: 2, feedback: 'already string' }; | ||
| const result = await updateTestimonialFeedback(collection, doc, '_id'); | ||
| expect(result).toBe(0); | ||
| expect(collection.updateOne).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateTableRowsDescriptions', () => { | ||
| test('updates table row descriptions if they are area objects', async () => { | ||
| const collection = { updateOne: jest.fn().mockResolvedValue({}) }; | ||
| const doc = { | ||
| _id: 1, | ||
| main: { | ||
| items: [ | ||
| { | ||
| type: 'table', | ||
| rows: [ | ||
| { description: { items: [{ content: '<p>foo</p>' }] } }, | ||
| { description: 'bar' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| const result = await updateTableRowsDescriptions(collection, doc, '_id'); | ||
| expect(result).toBe(1); | ||
| expect(collection.updateOne).toHaveBeenCalledWith( | ||
| { _id: 1 }, | ||
| { | ||
| $set: { | ||
| 'main.items': [ | ||
| { | ||
| type: 'table', | ||
| rows: [{ description: 'foo' }, { description: 'bar' }], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('does not update if no area descriptions found', async () => { | ||
| const collection = { updateOne: jest.fn() }; | ||
| const doc = { | ||
| _id: 2, | ||
| main: { items: [{ type: 'table', rows: [{ description: 'plain' }] }] }, | ||
| }; | ||
| const result = await updateTableRowsDescriptions(collection, doc, '_id'); | ||
| expect(result).toBe(0); | ||
| expect(collection.updateOne).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.