clean_sl_history_data.mjs 2.1 KB

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