remove_feature_from_all_users.mjs 1.3 KB

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