diff --git a/migrations/1496771633_tags.js b/migrations/1496771633_tags.js index 2480ed6c6..b74e8b336 100644 --- a/migrations/1496771633_tags.js +++ b/migrations/1496771633_tags.js @@ -22,8 +22,7 @@ module.exports = { return; } - // Create a new batch operation. - let batch = CommentModel.collection.initializeUnorderedBulkOp(); + const updates = []; // Loop over the comments retrieved, updating the tag structure. for (let {id, tags} of comments) { @@ -73,11 +72,22 @@ module.exports = { created_at })); - // Execute the batch operation. - batch.find({id}).updateOne({$set: {tags}}); + updates.push({query: {id}, update: {$set: {tags}}}); } - // Execute the batch update operation. - await batch.execute(); + if (updates.length > 0) { + + // Create a new batch operation. + let batch = CommentModel.collection.initializeUnorderedBulkOp(); + + for (const {query, update} of updates) { + + // Execute the batch operation. + batch.find(query).updateOne(update); + } + + // Execute the batch update operation. + await batch.execute(); + } } }; diff --git a/migrations/1507310316_flags.js b/migrations/1507310316_flags.js index 28b9d00cd..6dbbf4e7a 100644 --- a/migrations/1507310316_flags.js +++ b/migrations/1507310316_flags.js @@ -14,9 +14,7 @@ const mapping = { module.exports = { async up() { - // Setup the batch operation. - const batch = ActionModel.collection.initializeUnorderedBulkOp(); - + const updates = []; for (const item_type in mapping) { const mappings = mapping[item_type]; @@ -32,19 +30,29 @@ module.exports = { // group_id: // } - batch.find({ + updates.push({query: { group_id: OLD_GROUP_ID, item_type, - }).update({ + }, update: { $set: { group_id: NEW_GROUP_ID, }, - }); + }}); } } - // Execute the batch update operation. - await batch.execute(); + if (updates.length > 0) { + + // Setup the batch operation. + const batch = ActionModel.collection.initializeUnorderedBulkOp(); + + for (const {query, update} of updates) { + batch.find(query).update(update); + } + + // Execute the batch update operation. + await batch.execute(); + } } }; diff --git a/migrations/1510174676_user_status.js b/migrations/1510174676_user_status.js index 4bd52639b..f9245d639 100644 --- a/migrations/1510174676_user_status.js +++ b/migrations/1510174676_user_status.js @@ -22,12 +22,10 @@ module.exports = { const created_at = Date.now(); - // Create a new batch operation. - let bulk = UserModel.collection.initializeUnorderedBulkOp(); - // Get the first batch of users. let cursor = await getUserBatch(); + const updates = []; while (await cursor.hasNext()) { const user = await cursor.next(); @@ -201,11 +199,21 @@ module.exports = { throw new Error(`${status} is an invalid status`); } - bulk.find({id}).updateOne(update); + updates.push({query: {id}, update}); } - // Execute the bulk update operation. - await bulk.execute(); + if (updates.length > 0) { + + // Create a new batch operation. + let bulk = UserModel.collection.initializeUnorderedBulkOp(); + + for (const {query, update} of updates) { + bulk.find(query).updateOne(update); + } + + // Execute the bulk update operation. + await bulk.execute(); + } } };