Migrations in a queue

This commit is contained in:
Adria Navarro 2023-11-29 12:34:50 +01:00
parent a4fd4ef635
commit 3ee59b0e96
3 changed files with 16 additions and 15 deletions

View File

@ -1,3 +1,8 @@
// This file should never be manually modified, use `yarn add-app-migration` in order to add a new one
export const MIGRATIONS: Record<string, { migration: () => Promise<void> }> = {}
export const MIGRATIONS: {
migrationId: string
migrationFunc: () => Promise<void>
}[] = [
// Migrations will be executed sorted by migrationId
]

View File

@ -11,10 +11,6 @@ import environment from "../environment"
const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION)
appMigrationQueue.process(processMessage)
export async function runMigration(migrationId: string) {
await MIGRATIONS[migrationId].migration()
}
// TODO
export const PROCESS_MIGRATION_TIMEOUT =
environment.APP_MIGRATION_TIMEOUT || 60000
@ -34,8 +30,8 @@ async function processMessage(job: Job) {
await context.doInAppContext(appId, async () => {
const currentVersion = await getAppMigrationMetadata(appId)
const pendingMigrations = Object.keys(MIGRATIONS).filter(
m => m > currentVersion
const pendingMigrations = MIGRATIONS.filter(
m => m.migrationId > currentVersion
)
let index = 0
@ -45,8 +41,11 @@ async function processMessage(job: Job) {
migration,
appId,
})
await runMigration(migration)
await updateAppMigrationMetadata({ appId, version: migration })
await migration.migrationFunc()
await updateAppMigrationMetadata({
appId,
version: migration.migrationId,
})
}
})
}

View File

@ -1,22 +1,19 @@
import { context } from "@budibase/backend-core"
import * as setup from "../../api/routes/tests/utilities"
import { MIGRATIONS } from "../migrations"
import { runMigration } from "../queue"
describe("migration", () => {
it("each migration can rerun safely", async () => {
const config = setup.getConfig()
await config.init()
const migrations = Object.keys(MIGRATIONS)
await config.doInContext(config.getAppId(), async () => {
const db = context.getAppDB()
for (const migration of migrations) {
await runMigration(migration)
for (const migration of MIGRATIONS) {
await migration.migrationFunc()
const docs = await db.allDocs({ include_docs: true })
await runMigration(migration)
await migration.migrationFunc()
const latestDocs = await db.allDocs({ include_docs: true })
expect(docs).toEqual(latestDocs)