Updating migrations to disable all migrations after the first disabled migration.
This commit is contained in:
parent
338d9f8476
commit
0a5a788440
|
@ -361,7 +361,7 @@ async function performAppCreate(ctx: UserCtx<CreateAppRequest, App>) {
|
||||||
// Initialise the app migration version as the latest one
|
// Initialise the app migration version as the latest one
|
||||||
await appMigrations.updateAppMigrationMetadata({
|
await appMigrations.updateAppMigrationMetadata({
|
||||||
appId,
|
appId,
|
||||||
version: appMigrations.getLatestMigrationId(),
|
version: appMigrations.getLatestEnabledMigrationId(),
|
||||||
})
|
})
|
||||||
|
|
||||||
await cache.app.invalidateAppMetadata(appId, newApplication)
|
await cache.app.invalidateAppMetadata(appId, newApplication)
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { migrate as migrationImpl, MIGRATIONS } from "../../migrations"
|
||||||
import { Ctx } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
getAppMigrationVersion,
|
getAppMigrationVersion,
|
||||||
getLatestMigrationId,
|
getLatestEnabledMigrationId,
|
||||||
} from "../../appMigrations"
|
} from "../../appMigrations"
|
||||||
|
|
||||||
export async function migrate(ctx: Ctx) {
|
export async function migrate(ctx: Ctx) {
|
||||||
|
@ -27,7 +27,7 @@ export async function getMigrationStatus(ctx: Ctx) {
|
||||||
|
|
||||||
const latestAppliedMigration = await getAppMigrationVersion(appId)
|
const latestAppliedMigration = await getAppMigrationVersion(appId)
|
||||||
|
|
||||||
const migrated = latestAppliedMigration === getLatestMigrationId()
|
const migrated = latestAppliedMigration === getLatestEnabledMigrationId()
|
||||||
|
|
||||||
ctx.body = { migrated }
|
ctx.body = { migrated }
|
||||||
ctx.status = 200
|
ctx.status = 200
|
||||||
|
|
|
@ -31,7 +31,7 @@ import {
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
getAppMigrationVersion,
|
getAppMigrationVersion,
|
||||||
getLatestMigrationId,
|
getLatestEnabledMigrationId,
|
||||||
} from "../../../appMigrations"
|
} from "../../../appMigrations"
|
||||||
|
|
||||||
import send from "koa-send"
|
import send from "koa-send"
|
||||||
|
@ -133,7 +133,7 @@ const requiresMigration = async (ctx: Ctx) => {
|
||||||
ctx.throw("AppId could not be found")
|
ctx.throw("AppId could not be found")
|
||||||
}
|
}
|
||||||
|
|
||||||
const latestMigration = getLatestMigrationId()
|
const latestMigration = getLatestEnabledMigrationId()
|
||||||
if (!latestMigration) {
|
if (!latestMigration) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,16 +14,24 @@ export type AppMigration = {
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
// all migrations must be enabled for migrations to run
|
export function getLatestEnabledMigrationId() {
|
||||||
export const migrationsEnabled = (): boolean =>
|
const enabledMigrations: AppMigration[] = []
|
||||||
MIGRATIONS.find(m => m.disabled) == null
|
for (let migration of MIGRATIONS) {
|
||||||
|
// if a migration is disabled, all migrations after it are disabled
|
||||||
export const getLatestMigrationId = () =>
|
if (migration.disabled) {
|
||||||
MIGRATIONS.map(m => m.id)
|
break
|
||||||
|
}
|
||||||
|
enabledMigrations.push(migration)
|
||||||
|
}
|
||||||
|
return enabledMigrations
|
||||||
|
.map(m => m.id)
|
||||||
.sort()
|
.sort()
|
||||||
.reverse()[0]
|
.reverse()[0]
|
||||||
|
}
|
||||||
|
|
||||||
const getTimestamp = (versionId: string) => versionId?.split("_")[0] || ""
|
function getTimestamp(versionId: string) {
|
||||||
|
return versionId?.split("_")[0] || ""
|
||||||
|
}
|
||||||
|
|
||||||
export async function checkMissingMigrations(
|
export async function checkMissingMigrations(
|
||||||
ctx: UserCtx,
|
ctx: UserCtx,
|
||||||
|
@ -31,7 +39,7 @@ export async function checkMissingMigrations(
|
||||||
appId: string
|
appId: string
|
||||||
) {
|
) {
|
||||||
const currentVersion = await getAppMigrationVersion(appId)
|
const currentVersion = await getAppMigrationVersion(appId)
|
||||||
const latestMigration = getLatestMigrationId()
|
const latestMigration = getLatestEnabledMigrationId()
|
||||||
|
|
||||||
if (getTimestamp(currentVersion) < getTimestamp(latestMigration)) {
|
if (getTimestamp(currentVersion) < getTimestamp(latestMigration)) {
|
||||||
await queue.add(
|
await queue.add(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Header } from "@budibase/backend-core"
|
import { Header } from "@budibase/backend-core"
|
||||||
import * as setup from "../../api/routes/tests/utilities"
|
import * as setup from "../../api/routes/tests/utilities"
|
||||||
import * as migrations from "../migrations"
|
import * as migrations from "../migrations"
|
||||||
import { migrationsEnabled } from "../index"
|
import { getLatestEnabledMigrationId } from "../index"
|
||||||
import { getAppMigrationVersion } from "../appMigrationMetadata"
|
import { getAppMigrationVersion } from "../appMigrationMetadata"
|
||||||
|
|
||||||
jest.mock<typeof migrations>("../migrations", () => ({
|
jest.mock<typeof migrations>("../migrations", () => ({
|
||||||
|
@ -55,12 +55,24 @@ describe("migrations", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should disable migrations if any migration is disabled", () => {
|
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({
|
migrations.MIGRATIONS.push({
|
||||||
id: "20231211105814_new-test",
|
id: "20231211105814_new-test",
|
||||||
func: async () => {},
|
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")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { UserCtx } from "@budibase/types"
|
import { UserCtx } from "@budibase/types"
|
||||||
import { checkMissingMigrations, migrationsEnabled } from "../appMigrations"
|
import { checkMissingMigrations } from "../appMigrations"
|
||||||
import { env } from "@budibase/backend-core"
|
import { env } from "@budibase/backend-core"
|
||||||
|
|
||||||
export default async (ctx: UserCtx, next: any) => {
|
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
|
// 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
|
// 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()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue