Use migrations context
This commit is contained in:
parent
e0d8e4c671
commit
7b05c7eb18
|
@ -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<void>
|
||||||
|
}[]
|
||||||
|
) {
|
||||||
|
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`)
|
||||||
|
}
|
|
@ -1,61 +1,15 @@
|
||||||
import { context, locks, queue } from "@budibase/backend-core"
|
import { queue } from "@budibase/backend-core"
|
||||||
import { LockName, LockType } from "@budibase/types"
|
|
||||||
import { Job } from "bull"
|
import { Job } from "bull"
|
||||||
import { MIGRATIONS } from "./migrations"
|
import { MIGRATIONS } from "./migrations"
|
||||||
import {
|
import { processMigrations } from "./migrationsProcessor"
|
||||||
getAppMigrationVersion,
|
|
||||||
updateAppMigrationMetadata,
|
|
||||||
} from "./appMigrationMetadata"
|
|
||||||
|
|
||||||
const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION)
|
const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION)
|
||||||
appMigrationQueue.process(processMessage)
|
appMigrationQueue.process(processMessage)
|
||||||
|
|
||||||
async function processMessage(job: Job) {
|
async function processMessage(job: Job) {
|
||||||
const { appId } = job.data
|
const { appId } = job.data
|
||||||
console.log(`Processing app migration for "${appId}"`)
|
|
||||||
|
|
||||||
await locks.doWithLock(
|
await processMigrations(appId, MIGRATIONS)
|
||||||
{
|
|
||||||
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`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default appMigrationQueue
|
export default appMigrationQueue
|
||||||
|
|
Loading…
Reference in New Issue