pr_10442.patch 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --- services/web/scripts/convert_archived_state.js
  2. +++ services/web/scripts/convert_archived_state.js
  3. @@ -6,62 +6,77 @@
  4. const { promiseMapWithLimit } = require('../app/src/util/promises')
  5. // $ node scripts/convert_archived_state.js FIRST,SECOND
  6. -const STAGE = process.argv.pop()
  7. -async function main() {
  8. - if (STAGE.includes('FIRST')) {
  9. - await batchedUpdate(
  10. - 'projects',
  11. - { archived: false },
  12. - {
  13. - $set: { archived: [] },
  14. - }
  15. - )
  16. +async function main(STAGE) {
  17. + for (const FIELD of ['archived', 'trashed']) {
  18. + if (STAGE.includes('FIRST')) {
  19. + await batchedUpdate(
  20. + 'projects',
  21. + { [FIELD]: false },
  22. + {
  23. + $set: { [FIELD]: [] },
  24. + }
  25. + )
  26. - console.error('Done, with first part')
  27. - }
  28. + console.error('Done, with first part for field:', FIELD)
  29. + }
  30. - if (STAGE.includes('SECOND')) {
  31. - await batchedUpdate('projects', { archived: true }, performUpdate, {
  32. - _id: 1,
  33. - owner_ref: 1,
  34. - collaberator_refs: 1,
  35. - readOnly_refs: 1,
  36. - tokenAccessReadAndWrite_refs: 1,
  37. - tokenAccessReadOnly_refs: 1,
  38. - })
  39. + if (STAGE.includes('SECOND')) {
  40. + await batchedUpdate(
  41. + 'projects',
  42. + { [FIELD]: true },
  43. + async function performUpdate(collection, nextBatch) {
  44. + await promiseMapWithLimit(
  45. + WRITE_CONCURRENCY,
  46. + nextBatch,
  47. + async project => {
  48. + try {
  49. + await upgradeFieldToArray({ collection, project, FIELD })
  50. + } catch (err) {
  51. + console.error(project._id, err)
  52. + throw err
  53. + }
  54. + }
  55. + )
  56. + },
  57. + {
  58. + _id: 1,
  59. + owner_ref: 1,
  60. + collaberator_refs: 1,
  61. + readOnly_refs: 1,
  62. + tokenAccessReadAndWrite_refs: 1,
  63. + tokenAccessReadOnly_refs: 1,
  64. + }
  65. + )
  66. - console.error('Done, with second part')
  67. + console.error('Done, with second part for field:', FIELD)
  68. + }
  69. }
  70. }
  71. -main()
  72. - .then(() => {
  73. - process.exit(0)
  74. - })
  75. - .catch(error => {
  76. - console.error({ error })
  77. - process.exit(1)
  78. - })
  79. -
  80. -async function performUpdate(collection, nextBatch) {
  81. - await promiseMapWithLimit(WRITE_CONCURRENCY, nextBatch, project =>
  82. - setArchived(collection, project)
  83. - )
  84. +module.exports = main
  85. +
  86. +if (require.main === module) {
  87. + main(process.argv.pop())
  88. + .then(() => {
  89. + process.exit(0)
  90. + })
  91. + .catch(error => {
  92. + console.error({ error })
  93. + process.exit(1)
  94. + })
  95. }
  96. -async function setArchived(collection, project) {
  97. - const archived = calculateArchivedArray(project)
  98. -
  99. +async function upgradeFieldToArray({ collection, project, FIELD }) {
  100. return collection.updateOne(
  101. { _id: project._id },
  102. {
  103. - $set: { archived },
  104. + $set: { [FIELD]: getAllUserIds(project) },
  105. }
  106. )
  107. }
  108. -function calculateArchivedArray(project) {
  109. +function getAllUserIds(project) {
  110. return _.unionWith(
  111. [project.owner_ref],
  112. project.collaberator_refs,
  113. --- /dev/null
  114. +++ services/web/migrations/20221111111111_ce_sp_convert_archived_state.js
  115. @@ -0,0 +1,9 @@
  116. +const runScript = require('../scripts/convert_archived_state')
  117. +
  118. +exports.tags = ['server-ce', 'server-pro']
  119. +
  120. +exports.migrate = async () => {
  121. + await runScript('FIRST,SECOND')
  122. +}
  123. +
  124. +exports.rollback = async () => {}