clean_sl_history_data.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {
  2. db,
  3. getCollectionInternal,
  4. } from '../../app/src/infrastructure/mongodb.js'
  5. import { ensureMongoTimeout } from '../helpers/env_variable_helper.mjs'
  6. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  7. // Ensure default mongo query timeout has been increased 1h
  8. if (!process.env.MONGO_SOCKET_TIMEOUT) {
  9. ensureMongoTimeout(360000)
  10. }
  11. async function main() {
  12. await checkAllProjectsAreMigrated()
  13. await setAllowDowngradeToFalse()
  14. await deleteHistoryCollections()
  15. console.log('Legacy history data cleaned up successfully')
  16. process.exit(0)
  17. }
  18. async function checkAllProjectsAreMigrated() {
  19. console.log('checking all projects are migrated to Full Project History')
  20. const count = await db.projects.countDocuments({
  21. 'overleaf.history.display': { $ne: true },
  22. })
  23. if (count === 0) {
  24. console.log('All projects are migrated to Full Project History')
  25. } else {
  26. console.error(
  27. `There are ${count} projects that are not migrated to Full Project History` +
  28. ` please complete the migration before running this script again.`
  29. )
  30. process.exit(1)
  31. }
  32. }
  33. async function setAllowDowngradeToFalse() {
  34. console.log('unsetting `allowDowngrade` flag in all projects')
  35. await db.projects.updateMany(
  36. {
  37. 'overleaf.history.id': { $exists: true },
  38. 'overleaf.history.allowDowngrade': true,
  39. },
  40. { $unset: { 'overleaf.history.allowDowngrade': 1 } }
  41. )
  42. console.log('unsetting `allowDowngrade` flag in all projects - Done')
  43. }
  44. async function deleteHistoryCollections() {
  45. await gracefullyDropCollection(db.docHistory)
  46. await gracefullyDropCollection(db.docHistoryIndex)
  47. const projectHistoryMetaData = await getCollectionInternal(
  48. 'projectHistoryMetaData'
  49. )
  50. await gracefullyDropCollection(projectHistoryMetaData)
  51. }
  52. async function gracefullyDropCollection(collection) {
  53. const collectionName = collection.collectionName
  54. console.log(`removing \`${collectionName}\` data`)
  55. try {
  56. await collection.drop()
  57. } catch (err) {
  58. if (err.code === 26) {
  59. // collection already deleted
  60. console.log(`removing \`${collectionName}\` data - Already removed`)
  61. } else {
  62. throw err
  63. }
  64. }
  65. console.log(`removing \`${collectionName}\` data - Done`)
  66. }
  67. try {
  68. await scriptRunner(main)
  69. } catch (err) {
  70. console.error(err)
  71. process.exit(1)
  72. }