remove_brand_variation_ids.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Remove the brandVariationId attribute from project documents that have
  3. // that attribute, which value matches the one given.
  4. //
  5. // node scripts/remove_brand_variation_ids.js 3
  6. // gives a report of project documents that have brandVariationId attribute
  7. // with value, "3"
  8. //
  9. // node scripts/remove_brand_variation_ids.js 3 --commit true
  10. // actually removes the brandVariationId attribute from project documents
  11. // that have brandVariationId attribute with value, "3"
  12. //
  13. const { db } = require('../app/src/infrastructure/mongojs')
  14. const async = require('async')
  15. const minimist = require('minimist')
  16. const argv = minimist(process.argv.slice(2))
  17. const bvId = argv._[0]
  18. const commit = argv.commit !== undefined
  19. const maxParallel = 4
  20. console.log(
  21. (commit ? 'Remove' : 'Dry run for remove') +
  22. ' brandVariationId from projects that have { brandVariationId: ' +
  23. bvId +
  24. ' }'
  25. )
  26. var count = 0
  27. db.projects.find(
  28. { brandVariationId: bvId.toString() },
  29. { _id: 1, name: 1 },
  30. processRemovals
  31. )
  32. function processRemovals(err, projects) {
  33. if (err) throw err
  34. async.eachLimit(
  35. projects,
  36. maxParallel,
  37. function(project, cb) {
  38. count += 1
  39. console.log(
  40. (commit ? 'Removing' : 'Would remove') +
  41. ' brandVariationId on project ' +
  42. project._id +
  43. ', name: "' +
  44. project.name +
  45. '"'
  46. )
  47. if (commit) {
  48. db.projects.update(
  49. { _id: project._id },
  50. { $unset: { brandVariationId: '' } },
  51. cb
  52. )
  53. } else {
  54. async.setImmediate(cb)
  55. }
  56. },
  57. function(err) {
  58. if (err) {
  59. console.log('There was a problem: ', err)
  60. }
  61. console.log(
  62. 'BrandVariationId ' +
  63. (commit ? 'removed' : 'would be removed') +
  64. ' from ' +
  65. count +
  66. ' projects'
  67. )
  68. process.exit()
  69. }
  70. )
  71. }