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

View File

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