From 7b05c7eb182698b2fb0c1b25417e057566c62bbd Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 4 Dec 2023 16:39:01 +0100 Subject: [PATCH] Use migrations context --- .../src/appMigrations/migrationsProcessor.ts | 60 +++++++++++++++++++ packages/server/src/appMigrations/queue.ts | 52 +--------------- 2 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 packages/server/src/appMigrations/migrationsProcessor.ts diff --git a/packages/server/src/appMigrations/migrationsProcessor.ts b/packages/server/src/appMigrations/migrationsProcessor.ts new file mode 100644 index 0000000000..d639fcd2c1 --- /dev/null +++ b/packages/server/src/appMigrations/migrationsProcessor.ts @@ -0,0 +1,60 @@ +import { context, locks } from "@budibase/backend-core" +import { LockName, LockType } from "@budibase/types" + +import { + getAppMigrationVersion, + updateAppMigrationMetadata, +} from "./appMigrationMetadata" + +export async function processMigrations( + appId: string, + migrations: { + migrationId: string + migrationFunc: () => Promise + }[] +) { + console.log(`Processing app migration for "${appId}"`) + + await locks.doWithLock( + { + name: LockName.APP_MIGRATION, + type: LockType.AUTO_EXTEND, + resource: appId, + }, + async () => { + await context.doInAppMigrationContext(appId, async () => { + let currentVersion = await getAppMigrationVersion(appId) + + const pendingMigrations = migrations + .filter(m => m.migrationId > currentVersion) + .sort((a, b) => a.migrationId.localeCompare(b.migrationId)) + + const migrationIds = migrations.map(m => m.migrationId).sort() + + let index = 0 + for (const { migrationId, migrationFunc } of pendingMigrations) { + const expectedMigration = + migrationIds[migrationIds.indexOf(currentVersion) + 1] + + if (expectedMigration !== migrationId) { + throw `Migration ${migrationId} could not run, update for "${migrationId}" is running but ${expectedMigration} is expected` + } + + const counter = `(${++index}/${pendingMigrations.length})` + console.info(`Running migration ${migrationId}... ${counter}`, { + migrationId, + appId, + }) + await migrationFunc() + await updateAppMigrationMetadata({ + appId, + version: migrationId, + }) + currentVersion = migrationId + } + }) + } + ) + + console.log(`App migration for "${appId}" processed`) +} diff --git a/packages/server/src/appMigrations/queue.ts b/packages/server/src/appMigrations/queue.ts index d1bb9f3829..72bb2f9b12 100644 --- a/packages/server/src/appMigrations/queue.ts +++ b/packages/server/src/appMigrations/queue.ts @@ -1,61 +1,15 @@ -import { context, locks, queue } from "@budibase/backend-core" -import { LockName, LockType } from "@budibase/types" +import { queue } from "@budibase/backend-core" import { Job } from "bull" import { MIGRATIONS } from "./migrations" -import { - getAppMigrationVersion, - updateAppMigrationMetadata, -} from "./appMigrationMetadata" +import { processMigrations } from "./migrationsProcessor" const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION) appMigrationQueue.process(processMessage) async function processMessage(job: Job) { const { appId } = job.data - console.log(`Processing app migration for "${appId}"`) - await locks.doWithLock( - { - name: LockName.APP_MIGRATION, - type: LockType.AUTO_EXTEND, - resource: appId, - }, - async () => { - await context.doInAppContext(appId, async () => { - let currentVersion = await getAppMigrationVersion(appId) - - const pendingMigrations = MIGRATIONS.filter( - m => m.migrationId > currentVersion - ).sort((a, b) => a.migrationId.localeCompare(b.migrationId)) - - const migrationIds = MIGRATIONS.map(m => m.migrationId).sort() - - let index = 0 - for (const { migrationId, migrationFunc } of pendingMigrations) { - const expectedMigration = - migrationIds[migrationIds.indexOf(currentVersion) + 1] - - if (expectedMigration !== migrationId) { - throw `Migration ${migrationId} could not run, update for "${migrationId}" is running but ${expectedMigration} is expected` - } - - const counter = `(${++index}/${pendingMigrations.length})` - console.info(`Running migration ${migrationId}... ${counter}`, { - migrationId, - appId, - }) - await migrationFunc() - await updateAppMigrationMetadata({ - appId, - version: migrationId, - }) - currentVersion = migrationId - } - }) - } - ) - - console.log(`App migration for "${appId}" processed`) + await processMigrations(appId, MIGRATIONS) } export default appMigrationQueue