From 70edd7b2c03fd34f4726d1671ea9cd6b100d5665 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 22 Jun 2023 13:45:55 +0200 Subject: [PATCH] Don't batch prop updates in migration to save memory Should run slower, but within memory constraints --- app/imports/migrations/server/dbv2/dbv2.js | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/app/imports/migrations/server/dbv2/dbv2.js b/app/imports/migrations/server/dbv2/dbv2.js index 9df7b2ce..8d311b78 100644 --- a/app/imports/migrations/server/dbv2/dbv2.js +++ b/app/imports/migrations/server/dbv2/dbv2.js @@ -32,12 +32,15 @@ Migrations.add({ }); function migrateCollection(collection, migrateDoc) { - const bulk = collection.rawCollection().initializeUnorderedBulkOp(); - collection.find({}).forEach(doc => migrateDoc(bulk, doc, collection)); - bulk.execute(); + collection.find({}).forEach((doc, index) => { + if (index % 1000 === 0) { + console.log(`Migrating document #${index}`); + } + migrateDoc(doc, collection) + }); } -export function migratePropUp(bulk, prop, collection) { +export function migratePropUp(prop, collection) { let update; if (prop.type === 'slotFiller') { update = update || { $set: {} }; @@ -67,13 +70,17 @@ export function migratePropUp(bulk, prop, collection) { // Replace dollar sign with tilde in calculated fields update = dollarSignToTilde(prop, update); - // Add the update to the bulk op + // update the document, respecting the schema if (update) { - bulk.find({ _id: prop._id }).updateOne(update); + try { + collection.update({ _id: prop._id }, update, { selector: { type: prop.type } }); + } catch (e) { + console.warn('Doc Migration failed: ', prop._id, e); + } } } -export function migratePropDown(bulk, prop) { +export function migratePropDown(prop, collection) { const update = { $unset: { slotFillImage: 1, @@ -88,7 +95,13 @@ export function migratePropDown(bulk, prop) { tags: union(prop.libraryTags, prop.tags) } } - bulk.find({ _id: prop._id }).updateOne(update); + if (update) { + try { + collection.update({ _id: prop._id }, update, { selector: { type: prop.type } }); + } catch (e) { + console.warn('Doc Migration failed: ', prop._id, e); + } + } } function countSubscribers() {