From 65f20a4d56f044f8a070574b9b7075eac9a0d7a7 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Thu, 2 May 2024 08:42:48 -0400 Subject: [PATCH] Merge pull request #18186 from overleaf/em-migration-dependencies Add a migration helper checking dependent migrations GitOrigin-RevId: 96aa6238b20115206554faaa4c2aefc537bbe7e8 --- services/web/migrations/lib/helpers.js | 19 +++++++++++++++++++ services/web/migrations/lib/template.js | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/services/web/migrations/lib/helpers.js b/services/web/migrations/lib/helpers.js index 7a79ab9a8a..dbe76579e5 100644 --- a/services/web/migrations/lib/helpers.js +++ b/services/web/migrations/lib/helpers.js @@ -1,3 +1,5 @@ +// @ts-check + const { db, getCollectionNames, @@ -42,8 +44,25 @@ async function dropCollection(collectionName) { await collection.drop() } +/** + * Asserts that a dependent migration has run. Throws an error otherwise. + * + * @param {string} migrationName + */ +async function assertDependency(migrationName) { + await waitForDb() + const migrations = await getCollectionInternal('migrations') + const migration = await migrations.findOne({ name: migrationName }) + if (migration == null) { + throw new Error( + `Bad migration order: ${migrationName} should run before this migration` + ) + } +} + module.exports = { addIndexesToCollection, dropIndexesFromCollection, dropCollection, + assertDependency, } diff --git a/services/web/migrations/lib/template.js b/services/web/migrations/lib/template.js index 3505b5ddb6..1c2e3c39f1 100644 --- a/services/web/migrations/lib/template.js +++ b/services/web/migrations/lib/template.js @@ -6,6 +6,11 @@ exports.tags = ['server-ce', 'server-pro', 'saas'] exports.migrate = async client => { const { db } = client + // Are there migrations that need to run before this migration? + // Use the following helper to enforce the dependency: + // + // await Helpers.assertDependency('20200101000000_another_migration') + // await Helpers.addIndexesToCollection(db.wombats, [{ name: 1 }]) }