clean_sl_history_data.js 2.1 KB

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