remove_feature_from_all_users.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {
  2. db,
  3. READ_PREFERENCE_SECONDARY,
  4. } from '../app/src/infrastructure/mongodb.mjs'
  5. import parseArgs from 'minimist'
  6. import { scriptRunner } from './lib/ScriptRunner.mjs'
  7. async function _removeFeatureFromAllUsers(feature, commit) {
  8. let removals = 0
  9. const query = {}
  10. query[`features.${feature}`] = true
  11. const usersWithFeature = db.users.find(query, {
  12. readPreference: READ_PREFERENCE_SECONDARY,
  13. })
  14. const update = {}
  15. update[`features.${feature}`] = false
  16. while (await usersWithFeature.hasNext()) {
  17. const user = await usersWithFeature.next()
  18. if (commit) {
  19. await db.users.findOneAndUpdate({ _id: user._id }, { $set: update })
  20. }
  21. removals++
  22. }
  23. console.log(`removed ${feature} from ${removals} users`)
  24. if (!commit) {
  25. console.log(
  26. 'this was a dry run, pass --commit to remove features from users'
  27. )
  28. }
  29. }
  30. async function main() {
  31. const argv = parseArgs(process.argv.slice(2), {
  32. string: ['feature'],
  33. boolean: ['commit'],
  34. unknown: function (arg) {
  35. console.error('unrecognised argument', arg)
  36. process.exit(1)
  37. },
  38. })
  39. const feature = argv.feature
  40. const commit = argv.commit || false
  41. await _removeFeatureFromAllUsers(feature, commit)
  42. }
  43. try {
  44. await scriptRunner(main)
  45. console.log('Done')
  46. process.exit(0)
  47. } catch (error) {
  48. console.error(error)
  49. process.exit(1)
  50. }