From 65012e50374aa8ba2143836286c1adfbb9ed68ba Mon Sep 17 00:00:00 2001 From: Douglas Lovell Date: Fri, 1 Mar 2019 08:44:40 -0300 Subject: [PATCH] Merge branch 'dcl-i1507c' GitOrigin-RevId: fc12ab4d20918e83f7aa05ba5263c785d594c8dc --- .../web/scripts/remove_brand_variation_ids.js | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 services/web/scripts/remove_brand_variation_ids.js diff --git a/services/web/scripts/remove_brand_variation_ids.js b/services/web/scripts/remove_brand_variation_ids.js new file mode 100644 index 0000000000..b829e9de23 --- /dev/null +++ b/services/web/scripts/remove_brand_variation_ids.js @@ -0,0 +1,75 @@ +// +// Remove the brandVariationId attribute from project documents that have +// that attribute, which value matches the one given. +// +// node scripts/remove_brand_variation_ids.js 3 +// gives a report of project documents that have brandVariationId attribute +// with value, "3" +// +// node scripts/remove_brand_variation_ids.js 3 --commit true +// actually removes the brandVariationId attribute from project documents +// that have brandVariationId attribute with value, "3" +// +const { db } = require('../app/js/infrastructure/mongojs') +const async = require('async') +const minimist = require('minimist') + +const argv = minimist(process.argv.slice(2)) +const bvId = argv._[0] +const commit = argv.commit !== undefined + +console.log( + (commit ? 'Remove' : 'Dry run for remove') + + ' brandVariationId from projects that have { brandVariationId: ' + + bvId + + ' }' +) + +var count = 0 + +db.projects.find( + { brandVariationId: bvId.toString() }, + { _id: 1, name: 1 }, + processRemovals +) + +function processRemovals(err, projects) { + if (err) throw err + async.mapSeries( + projects, + function(project, cb) { + count += 1 + console.log( + (commit ? 'Removing' : 'Would remove') + + ' brandVariationId on project ' + + project._id + + ', name: "' + + project.name + + '"' + ) + if (commit) { + db.projects.update( + { _id: project._id }, + { $unset: { brandVariationId: '' } }, + cb + ) + } else { + cb() + } + }, + function(err) { + if (err) { + console.log('There was a problem: ', err) + } + console.log( + 'BrandVariationId ' + + (commit ? 'removed' : 'would be removed') + + ' from ' + + count + + ' projects' + ) + + process.exit() + } + ) +}