20221111111111_ce_sp_convert_archived_state.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  2. import { promiseMapWithLimit } from '@overleaf/promise-utils'
  3. import { db, ObjectId } from './lib/mongodb.mjs'
  4. const tags = ['server-ce', 'server-pro']
  5. const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
  6. function getAllUserIds(project) {
  7. return Array.from(
  8. new Set(
  9. [
  10. project.owner_ref,
  11. ...(project.collaberator_refs || []),
  12. ...(project.readOnly_refs || []),
  13. ...(project.tokenAccessReadAndWrite_refs || []),
  14. ...(project.tokenAccessReadOnly_refs || []),
  15. ].map(id => id.toString())
  16. )
  17. ).map(id => new ObjectId(id))
  18. }
  19. async function migrateField(field) {
  20. await batchedUpdate(
  21. db.projects,
  22. { [field]: false },
  23. { $set: { [field]: [] } }
  24. )
  25. await batchedUpdate(
  26. db.projects,
  27. { [field]: true },
  28. async nextBatch => {
  29. await promiseMapWithLimit(WRITE_CONCURRENCY, nextBatch, async project => {
  30. await db.projects.updateOne(
  31. { _id: project._id },
  32. { $set: { [field]: getAllUserIds(project) } }
  33. )
  34. })
  35. },
  36. {
  37. _id: 1,
  38. owner_ref: 1,
  39. collaberator_refs: 1,
  40. readOnly_refs: 1,
  41. tokenAccessReadAndWrite_refs: 1,
  42. tokenAccessReadOnly_refs: 1,
  43. }
  44. )
  45. }
  46. const migrate = async () => {
  47. for (const field of ['archived', 'trashed']) {
  48. await migrateField(field)
  49. }
  50. }
  51. const rollback = async () => {}
  52. export default {
  53. tags,
  54. migrate,
  55. rollback,
  56. }