diff --git a/packages/server/src/appMigrations/index.ts b/packages/server/src/appMigrations/index.ts index b1a2b742bc..a4ffe64604 100644 --- a/packages/server/src/appMigrations/index.ts +++ b/packages/server/src/appMigrations/index.ts @@ -4,7 +4,12 @@ import { MIGRATIONS } from "./migrations" export * from "./appMigrationMetadata" -export const latestMigration = MIGRATIONS.map(m => m.migrationId) +export type AppMigration = { + id: string + func: () => Promise +} + +export const latestMigration = MIGRATIONS.map(m => m.id) .sort() .reverse()[0] diff --git a/packages/server/src/appMigrations/migrations.ts b/packages/server/src/appMigrations/migrations.ts index 893881e716..02b93a781b 100644 --- a/packages/server/src/appMigrations/migrations.ts +++ b/packages/server/src/appMigrations/migrations.ts @@ -1,8 +1,7 @@ // This file should never be manually modified, use `yarn add-app-migration` in order to add a new one -export const MIGRATIONS: { - migrationId: string - migrationFunc: () => Promise -}[] = [ +import { AppMigration } from "." + +export const MIGRATIONS: AppMigration[] = [ // Migrations will be executed sorted by migrationId ] diff --git a/packages/server/src/appMigrations/migrationsProcessor.ts b/packages/server/src/appMigrations/migrationsProcessor.ts index d639fcd2c1..8d119f3864 100644 --- a/packages/server/src/appMigrations/migrationsProcessor.ts +++ b/packages/server/src/appMigrations/migrationsProcessor.ts @@ -5,13 +5,11 @@ import { getAppMigrationVersion, updateAppMigrationMetadata, } from "./appMigrationMetadata" +import { AppMigration } from "." export async function processMigrations( appId: string, - migrations: { - migrationId: string - migrationFunc: () => Promise - }[] + migrations: AppMigration[] ) { console.log(`Processing app migration for "${appId}"`) @@ -26,31 +24,31 @@ export async function processMigrations( let currentVersion = await getAppMigrationVersion(appId) const pendingMigrations = migrations - .filter(m => m.migrationId > currentVersion) - .sort((a, b) => a.migrationId.localeCompare(b.migrationId)) + .filter(m => m.id > currentVersion) + .sort((a, b) => a.id.localeCompare(b.id)) - const migrationIds = migrations.map(m => m.migrationId).sort() + const migrationIds = migrations.map(m => m.id).sort() let index = 0 - for (const { migrationId, migrationFunc } of pendingMigrations) { + for (const { id, func } 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` + if (expectedMigration !== id) { + throw `Migration ${id} could not run, update for "${id}" is running but ${expectedMigration} is expected` } const counter = `(${++index}/${pendingMigrations.length})` - console.info(`Running migration ${migrationId}... ${counter}`, { - migrationId, + console.info(`Running migration ${id}... ${counter}`, { + migrationId: id, appId, }) - await migrationFunc() + await func() await updateAppMigrationMetadata({ appId, - version: migrationId, + version: id, }) - currentVersion = migrationId + currentVersion = id } }) } diff --git a/packages/server/src/appMigrations/tests/migrations.spec.ts b/packages/server/src/appMigrations/tests/migrations.spec.ts index a5ceb05d69..b761837dd4 100644 --- a/packages/server/src/appMigrations/tests/migrations.spec.ts +++ b/packages/server/src/appMigrations/tests/migrations.spec.ts @@ -10,10 +10,10 @@ describe("migration", () => { await config.doInContext(config.getAppId(), async () => { const db = context.getAppDB() for (const migration of MIGRATIONS) { - await migration.migrationFunc() + await migration.func() const docs = await db.allDocs({ include_docs: true }) - await migration.migrationFunc() + await migration.func() const latestDocs = await db.allDocs({ include_docs: true }) expect(docs).toEqual(latestDocs) diff --git a/packages/server/src/appMigrations/tests/migrationsProcessor.spec.ts b/packages/server/src/appMigrations/tests/migrationsProcessor.spec.ts index 460524cb9c..189f6c068b 100644 --- a/packages/server/src/appMigrations/tests/migrationsProcessor.spec.ts +++ b/packages/server/src/appMigrations/tests/migrationsProcessor.spec.ts @@ -2,16 +2,14 @@ import * as setup from "../../api/routes/tests/utilities" import { processMigrations } from "../migrationsProcessor" import { getAppMigrationVersion } from "../appMigrationMetadata" import { context } from "@budibase/backend-core" +import { AppMigration } from ".." describe("migrationsProcessor", () => { it("running migrations will update the latest applied migration", async () => { - const testMigrations: { - migrationId: string - migrationFunc: () => Promise - }[] = [ - { migrationId: "123", migrationFunc: async () => {} }, - { migrationId: "124", migrationFunc: async () => {} }, - { migrationId: "125", migrationFunc: async () => {} }, + const testMigrations: AppMigration[] = [ + { id: "123", func: async () => {} }, + { id: "124", func: async () => {} }, + { id: "125", func: async () => {} }, ] const config = setup.getConfig() @@ -29,13 +27,10 @@ describe("migrationsProcessor", () => { }) it("no context can be initialised within a migration", async () => { - const testMigrations: { - migrationId: string - migrationFunc: () => Promise - }[] = [ + const testMigrations: AppMigration[] = [ { - migrationId: "123", - migrationFunc: async () => { + id: "123", + func: async () => { await context.doInAppMigrationContext("any", () => {}) }, },