Skip to content

Commit 86a9fb0

Browse files
author
sachin-maheshwari
authored
Merge pull request #230 from topcoder-platform/develop
Hotfix: Shapeup-pure-v5-task, migration DB to ES
2 parents 70860cc + d2627b6 commit 86a9fb0

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

scripts/migrateFromDBToES.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const esClient = helper.getEsClient()
1414
/*
1515
* Migrate records from DB to ES
1616
* @param tableName {String} DynamoDB table name
17+
* @param customFunction {Function} custom function to handle record
1718
* @returns {Promise}
1819
*/
19-
function * migrateRecords (tableName) {
20+
function * migrateRecords (tableName, customFunction) {
2021
let body = []
2122
let batchCounter = 1
2223
const params = {
@@ -27,7 +28,8 @@ function * migrateRecords (tableName) {
2728
const records = yield dbhelper.scanRecords(params)
2829
logger.debug(`Number of ${tableName}s currently fetched from DB - ` + records.Items.length)
2930
let i = 0
30-
for (const item of records.Items) {
31+
for (const recordItem of records.Items) {
32+
const item = customFunction(recordItem)
3133
// action
3234
body.push({
3335
index: {
@@ -69,12 +71,30 @@ function * migrateRecords (tableName) {
6971

7072
co(function * () {
7173
const promises = []
72-
promises.push(migrateRecords('ReviewType'))
73-
promises.push(migrateRecords('Submission'))
74-
promises.push(migrateRecords('Review'))
75-
promises.push(migrateRecords('ReviewSummation'))
74+
const reviews = []
75+
const reviewSummations = []
76+
promises.push(migrateRecords('ReviewType', t => t))
77+
promises.push(migrateRecords('Review', t => {
78+
reviews.push(t)
79+
return t
80+
}))
81+
promises.push(migrateRecords('ReviewSummation', t => {
82+
reviewSummations.push(t)
83+
return t
84+
}))
7685
// Process migration in parallel
7786
yield promises
87+
yield migrateRecords('Submission', t => {
88+
t.review = _.map(_.filter(reviews, ['submissionId', t.id]), r => _.omit(r, ['resource']))
89+
t.reviewSummation = _.map(_.filter(reviewSummations, ['submissionId', t.id]), r => _.omit(r, ['resource']))
90+
if (_.isEmpty(t.review)) {
91+
t = _.omit(t, ['review'])
92+
}
93+
if (_.isEmpty(t.reviewSummation)) {
94+
t = _.omit(t, ['reviewSummation'])
95+
}
96+
return t
97+
})
7898
}).catch((err) => {
7999
logger.logFullError(err)
80100
})

0 commit comments

Comments
 (0)