clean_sl_history_data.mjs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {
  2. db,
  3. getCollectionInternal,
  4. } from '../../app/src/infrastructure/mongodb.mjs'
  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. const docHistory = await getCollectionInternal('docHistory')
  46. const docHistoryIndex = await getCollectionInternal('docHistoryIndex')
  47. await gracefullyDropCollection(docHistory)
  48. await gracefullyDropCollection(docHistoryIndex)
  49. const projectHistoryMetaData = await getCollectionInternal(
  50. 'projectHistoryMetaData'
  51. )
  52. await gracefullyDropCollection(projectHistoryMetaData)
  53. }
  54. async function gracefullyDropCollection(collection) {
  55. const collectionName = collection.collectionName
  56. console.log(`removing \`${collectionName}\` data`)
  57. try {
  58. await collection.drop()
  59. } catch (err) {
  60. if (err.code === 26) {
  61. // collection already deleted
  62. console.log(`removing \`${collectionName}\` data - Already removed`)
  63. } else {
  64. throw err
  65. }
  66. }
  67. console.log(`removing \`${collectionName}\` data - Done`)
  68. }
  69. try {
  70. await scriptRunner(main)
  71. } catch (err) {
  72. console.error(err)
  73. process.exit(1)
  74. }