From 6b78e920e54566457a5620d05fe8795de38bda3c Mon Sep 17 00:00:00 2001 From: Mithun Kamath Date: Thu, 11 Mar 2021 10:40:23 +0530 Subject: [PATCH 1/2] #228 - Store reviews and review summations under submissions when migrating from db to es (#229) --- scripts/migrateFromDBToES.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/scripts/migrateFromDBToES.js b/scripts/migrateFromDBToES.js index 8d6ff6b1..b92bef6a 100644 --- a/scripts/migrateFromDBToES.js +++ b/scripts/migrateFromDBToES.js @@ -14,9 +14,10 @@ const esClient = helper.getEsClient() /* * Migrate records from DB to ES * @param tableName {String} DynamoDB table name + * @param customFunction {Function} custom function to handle record * @returns {Promise} */ -function * migrateRecords (tableName) { +function * migrateRecords (tableName, customFunction) { let body = [] let batchCounter = 1 const params = { @@ -27,7 +28,8 @@ function * migrateRecords (tableName) { const records = yield dbhelper.scanRecords(params) logger.debug(`Number of ${tableName}s currently fetched from DB - ` + records.Items.length) let i = 0 - for (const item of records.Items) { + for (const recordItem of records.Items) { + const item = customFunction(recordItem) // action body.push({ index: { @@ -69,12 +71,24 @@ function * migrateRecords (tableName) { co(function * () { const promises = [] - promises.push(migrateRecords('ReviewType')) - promises.push(migrateRecords('Submission')) - promises.push(migrateRecords('Review')) - promises.push(migrateRecords('ReviewSummation')) + const reviews = [] + const reviewSummations = [] + promises.push(migrateRecords('ReviewType', t => t)) + promises.push(migrateRecords('Review', t => { + reviews.push(t) + return t + })) + promises.push(migrateRecords('ReviewSummation', t => { + reviewSummations.push(t) + return t + })) // Process migration in parallel yield promises + yield migrateRecords('Submission', t => { + t.review = _.map(_.filter(reviews, ['submissionId', t.id]), r => _.omit(r, ['resource'])) + t.reviewSummation = _.map(_.filter(reviewSummations, ['submissionId', t.id]), r => _.omit(r, ['resource'])) + return t + }) }).catch((err) => { logger.logFullError(err) }) From d2627b6064960aae854ee8681f3ada10fca33f6c Mon Sep 17 00:00:00 2001 From: Sachin Maheshwari Date: Thu, 11 Mar 2021 16:13:40 +0530 Subject: [PATCH 2/2] DBtoES migration: to avoid community-app UI broken in case of empty array --- scripts/migrateFromDBToES.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/migrateFromDBToES.js b/scripts/migrateFromDBToES.js index b92bef6a..40be6fe1 100644 --- a/scripts/migrateFromDBToES.js +++ b/scripts/migrateFromDBToES.js @@ -87,6 +87,12 @@ co(function * () { yield migrateRecords('Submission', t => { t.review = _.map(_.filter(reviews, ['submissionId', t.id]), r => _.omit(r, ['resource'])) t.reviewSummation = _.map(_.filter(reviewSummations, ['submissionId', t.id]), r => _.omit(r, ['resource'])) + if (_.isEmpty(t.review)) { + t = _.omit(t, ['review']) + } + if (_.isEmpty(t.reviewSummation)) { + t = _.omit(t, ['reviewSummation']) + } return t }) }).catch((err) => {