From e341959c28c178f8d86cc45e758445eaf64f29b6 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Wed, 26 Apr 2023 16:24:56 +0200 Subject: [PATCH] [web/scripts] Clean legacy history data (#12812) * [web/scripts] Clean legacy history data GitOrigin-RevId: ce91ca69a9f4e8b340e659d0b468852abe01e6fd --- .../scripts/history/clean_sl_history_data.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 services/web/scripts/history/clean_sl_history_data.js diff --git a/services/web/scripts/history/clean_sl_history_data.js b/services/web/scripts/history/clean_sl_history_data.js new file mode 100644 index 0000000000..1800692cbe --- /dev/null +++ b/services/web/scripts/history/clean_sl_history_data.js @@ -0,0 +1,60 @@ +const { waitForDb, db } = require('../../app/src/infrastructure/mongodb') + +async function main() { + await checkAllProjectsAreMigrated() + await setAllowDowngradeToFalse() + await deleteHistoryCollections() + console.log('Legacy history data cleaned up successfully') + process.exit(0) +} + +async function checkAllProjectsAreMigrated() { + console.log('checking all projects are migrated to Full Project History') + + const count = await db.projects.countDocuments({ + 'overleaf.history.display': { $ne: true }, + }) + + if (count === 0) { + console.log('All projects are migrated to Full Project History') + } else { + console.error( + `There are ${count} projects that are not migrated to Full Project History` + + ` please complete the migration before running this script again.` + ) + process.exit(1) + } +} + +async function setAllowDowngradeToFalse() { + console.log('unsetting `allowDowngrade` flag in all projects') + await db.projects.updateMany( + { + 'overleaf.history.id': { $exists: true }, + 'overleaf.history.allowDowngrade': true, + }, + { $unset: { 'overleaf.history.allowDowngrade': 1 } } + ) + console.log('unsetting `allowDowngrade` flag in all projects - Done') +} + +async function deleteHistoryCollections() { + console.log('removing `docHistory` data') + await db.docHistory.deleteMany({}) + console.log('removing `docHistory` data - Done') + + console.log('removing `docHistoryIndex` data') + await db.docHistoryIndex.deleteMany({}) + console.log('removing `docHistoryIndex` data - Done') + + console.log('removing `projectHistoryMetaData` data') + await db.projectHistoryMetaData.deleteMany({}) + console.log('removing `projectHistoryMetaData` data - Done') +} + +waitForDb() + .then(main) + .catch(err => { + console.error(err) + process.exit(1) + })