1
0
Fork 0
mirror of synced 2024-08-05 05:11:43 +12:00

Updating migrations to disable all migrations after the first disabled migration.

This commit is contained in:
mike12345567 2024-06-05 18:19:44 +01:00
parent 338d9f8476
commit 0a5a788440
6 changed files with 38 additions and 18 deletions

View file

@ -361,7 +361,7 @@ async function performAppCreate(ctx: UserCtx<CreateAppRequest, App>) {
// Initialise the app migration version as the latest one
await appMigrations.updateAppMigrationMetadata({
appId,
version: appMigrations.getLatestMigrationId(),
version: appMigrations.getLatestEnabledMigrationId(),
})
await cache.app.invalidateAppMetadata(appId, newApplication)

View file

@ -3,7 +3,7 @@ import { migrate as migrationImpl, MIGRATIONS } from "../../migrations"
import { Ctx } from "@budibase/types"
import {
getAppMigrationVersion,
getLatestMigrationId,
getLatestEnabledMigrationId,
} from "../../appMigrations"
export async function migrate(ctx: Ctx) {
@ -27,7 +27,7 @@ export async function getMigrationStatus(ctx: Ctx) {
const latestAppliedMigration = await getAppMigrationVersion(appId)
const migrated = latestAppliedMigration === getLatestMigrationId()
const migrated = latestAppliedMigration === getLatestEnabledMigrationId()
ctx.body = { migrated }
ctx.status = 200

View file

@ -31,7 +31,7 @@ import {
} from "@budibase/types"
import {
getAppMigrationVersion,
getLatestMigrationId,
getLatestEnabledMigrationId,
} from "../../../appMigrations"
import send from "koa-send"
@ -133,7 +133,7 @@ const requiresMigration = async (ctx: Ctx) => {
ctx.throw("AppId could not be found")
}
const latestMigration = getLatestMigrationId()
const latestMigration = getLatestEnabledMigrationId()
if (!latestMigration) {
return false
}

View file

@ -14,16 +14,24 @@ export type AppMigration = {
disabled?: boolean
}
// all migrations must be enabled for migrations to run
export const migrationsEnabled = (): boolean =>
MIGRATIONS.find(m => m.disabled) == null
export const getLatestMigrationId = () =>
MIGRATIONS.map(m => m.id)
export function getLatestEnabledMigrationId() {
const enabledMigrations: AppMigration[] = []
for (let migration of MIGRATIONS) {
// if a migration is disabled, all migrations after it are disabled
if (migration.disabled) {
break
}
enabledMigrations.push(migration)
}
return enabledMigrations
.map(m => m.id)
.sort()
.reverse()[0]
}
const getTimestamp = (versionId: string) => versionId?.split("_")[0] || ""
function getTimestamp(versionId: string) {
return versionId?.split("_")[0] || ""
}
export async function checkMissingMigrations(
ctx: UserCtx,
@ -31,7 +39,7 @@ export async function checkMissingMigrations(
appId: string
) {
const currentVersion = await getAppMigrationVersion(appId)
const latestMigration = getLatestMigrationId()
const latestMigration = getLatestEnabledMigrationId()
if (getTimestamp(currentVersion) < getTimestamp(latestMigration)) {
await queue.add(

View file

@ -1,7 +1,7 @@
import { Header } from "@budibase/backend-core"
import * as setup from "../../api/routes/tests/utilities"
import * as migrations from "../migrations"
import { migrationsEnabled } from "../index"
import { getLatestEnabledMigrationId } from "../index"
import { getAppMigrationVersion } from "../appMigrationMetadata"
jest.mock<typeof migrations>("../migrations", () => ({
@ -55,12 +55,24 @@ describe("migrations", () => {
})
it("should disable migrations if any migration is disabled", () => {
// remove all migrations
migrations.MIGRATIONS.splice(0, migrations.MIGRATIONS.length)
migrations.MIGRATIONS.push({
id: "20231211105810_new-test",
func: async () => {},
})
migrations.MIGRATIONS.push({
id: "20231211105812_new-test",
func: async () => {},
})
migrations.MIGRATIONS.push({
id: "20231211105814_new-test",
func: async () => {},
disabled: true,
})
expect(migrationsEnabled()).toBe(false)
expect(getLatestEnabledMigrationId()).toBe("20231211105814_new-test")
migrations.MIGRATIONS[1].disabled = true
expect(getLatestEnabledMigrationId()).toBe("20231211105810_new-test")
})
})

View file

@ -1,5 +1,5 @@
import { UserCtx } from "@budibase/types"
import { checkMissingMigrations, migrationsEnabled } from "../appMigrations"
import { checkMissingMigrations } from "../appMigrations"
import { env } from "@budibase/backend-core"
export default async (ctx: UserCtx, next: any) => {
@ -7,7 +7,7 @@ export default async (ctx: UserCtx, next: any) => {
// migrations can be disabled via environment variable, or can be disabled
// due to some of the migrations not being ready to run - disables all migrations
if (env.SKIP_APP_MIGRATIONS || !migrationsEnabled()) {
if (env.SKIP_APP_MIGRATIONS) {
return next()
}