clean_sl_history_data.mjs 2.2 KB

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