From 20bd57e9bd005fb7ada0eb54f1239a485044db00 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:34:54 -0500 Subject: [PATCH] Merge pull request #16314 from overleaf/em-migrate-docops-versions Migrate doc versions from docOps to docs GitOrigin-RevId: 34b0130e9f0849b8e16f1febc38c78b55d51a4c1 --- ...0_move_doc_versions_from_docops_to_docs.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 services/web/migrations/20231219081700_move_doc_versions_from_docops_to_docs.js diff --git a/services/web/migrations/20231219081700_move_doc_versions_from_docops_to_docs.js b/services/web/migrations/20231219081700_move_doc_versions_from_docops_to_docs.js new file mode 100644 index 0000000000..e4a37093db --- /dev/null +++ b/services/web/migrations/20231219081700_move_doc_versions_from_docops_to_docs.js @@ -0,0 +1,45 @@ +const { ReadPreference } = require('mongodb') + +const BATCH_SIZE = parseInt(process.env.BATCH_SIZE || '1000', 10) + +exports.tags = ['server-ce', 'server-pro', 'saas'] + +exports.migrate = async ({ db }) => { + const records = db.docOps.find( + {}, + { readPreference: ReadPreference.secondaryPreferred } + ) + + let docsProcessed = 0 + let batch = [] + for await (const record of records) { + const docId = record.doc_id + const version = record.version + batch.push({ + updateOne: { + filter: { + _id: docId, + version: { $exists: false }, + }, + update: { $set: { version } }, + }, + }) + if (batch.length >= BATCH_SIZE) { + await db.docs.bulkWrite(batch, { ordered: false }) + batch = [] + } + docsProcessed += 1 + if (docsProcessed % 100000 === 0) { + console.log(`${docsProcessed} docs processed`) + } + } + if (batch.length > 0) { + await db.docs.bulkWrite(batch, { ordered: false }) + } +} + +exports.rollback = async ({ db }) => { + // Nothing to do on rollback. We don't want to remove versions from the docs + // collection because they might be more current than the ones in the docOps + // collection. +}