convert_archived_state.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const _ = require('lodash')
  2. const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
  3. const { batchedUpdate } = require('./helpers/batchedUpdate')
  4. const { promiseMapWithLimit } = require('../app/src/util/promises')
  5. async function main() {
  6. await batchedUpdate(
  7. 'projects',
  8. { archived: false },
  9. {
  10. $set: { archived: [] },
  11. $unset: { trashed: '' } // lest any v1 trashed bits be left behind
  12. }
  13. )
  14. console.log('Done, moving to archived projects')
  15. await batchedUpdate('projects', { archived: true }, performUpdate, {
  16. _id: 1,
  17. owner_ref: 1,
  18. collaberator_refs: 1,
  19. readOnly_refs: 1,
  20. tokenAccessReadAndWrite_refs: 1,
  21. tokenAccessReadOnly_refs: 1
  22. })
  23. }
  24. main()
  25. .then(() => {
  26. process.exit(0)
  27. })
  28. .catch(error => {
  29. console.error({ error })
  30. process.exit(1)
  31. })
  32. async function performUpdate(collection, nextBatch) {
  33. await promiseMapWithLimit(WRITE_CONCURRENCY, nextBatch, project =>
  34. setArchived(collection, project)
  35. )
  36. }
  37. async function setArchived(collection, project) {
  38. const archived = calculateArchivedArray(project)
  39. return collection.updateOne(
  40. { _id: project._id },
  41. {
  42. $set: { archived: archived },
  43. $unset: { trashed: '' } // lest any v1 trashed bits be left behind
  44. }
  45. )
  46. }
  47. function calculateArchivedArray(project) {
  48. return _.unionWith(
  49. [project.owner_ref],
  50. project.collaberator_refs,
  51. project.readOnly_refs,
  52. project.tokenAccessReadAndWrite_refs,
  53. project.tokenAccessReadOnly_refs,
  54. _objectIdEquals
  55. )
  56. }
  57. function _objectIdEquals(firstVal, secondVal) {
  58. // For use as a comparator for unionWith
  59. return firstVal.toString() === secondVal.toString()
  60. }