diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index 14b04ae9f8..1e7da2f9a2 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -193,7 +193,6 @@ const environment = { }, ROLLING_LOG_MAX_SIZE: process.env.ROLLING_LOG_MAX_SIZE || "10M", DISABLE_SCIM_CALLS: process.env.DISABLE_SCIM_CALLS, - SKIP_APP_MIGRATIONS: process.env.SKIP_APP_MIGRATIONS || false, } // clean up any environment variable edge cases diff --git a/packages/server/src/api/controllers/migrations.ts b/packages/server/src/api/controllers/migrations.ts index 9a23226837..97c0ae8712 100644 --- a/packages/server/src/api/controllers/migrations.ts +++ b/packages/server/src/api/controllers/migrations.ts @@ -27,7 +27,7 @@ export async function getMigrationStatus(ctx: Ctx) { const latestAppliedMigration = await getAppMigrationVersion(appId) - const migrated = latestAppliedMigration === getLatestEnabledMigrationId() + const migrated = latestAppliedMigration >= getLatestEnabledMigrationId() ctx.body = { migrated } ctx.status = 200 diff --git a/packages/server/src/appMigrations/index.ts b/packages/server/src/appMigrations/index.ts index a3e3858559..cac1e82716 100644 --- a/packages/server/src/appMigrations/index.ts +++ b/packages/server/src/appMigrations/index.ts @@ -14,9 +14,9 @@ export type AppMigration = { disabled?: boolean } -export function getLatestEnabledMigrationId() { +export function getLatestEnabledMigrationId(migrations?: AppMigration[]) { const enabledMigrations: AppMigration[] = [] - for (let migration of MIGRATIONS) { + for (let migration of migrations || MIGRATIONS) { // if a migration is disabled, all migrations after it are disabled if (migration.disabled) { break diff --git a/packages/server/src/appMigrations/migrations/20240604153647_initial_sqs.ts b/packages/server/src/appMigrations/migrations/20240604153647_initial_sqs.ts index d092f416ff..05174b8cf1 100644 --- a/packages/server/src/appMigrations/migrations/20240604153647_initial_sqs.ts +++ b/packages/server/src/appMigrations/migrations/20240604153647_initial_sqs.ts @@ -43,15 +43,7 @@ const migration = async () => { const tables = await sdk.tables.getAllInternalTables() // do these one by one - running in parallel could cause problems for (let table of tables) { - try { - await db.sql(`select * from ${table._id} limit 1`) - } catch (err) { - if (typeof err === "object") { - const dbErr = err as DBError - throw new Error(`Failed to run initial SQS search - ${dbErr.message}`) - } - throw err - } + await db.sql(`select * from ${table._id} limit 1`) } } } diff --git a/packages/server/src/appMigrations/tests/migrations.spec.ts b/packages/server/src/appMigrations/tests/migrations.spec.ts index 1ea54396f7..1da94f503f 100644 --- a/packages/server/src/appMigrations/tests/migrations.spec.ts +++ b/packages/server/src/appMigrations/tests/migrations.spec.ts @@ -1,7 +1,7 @@ import { Header } from "@budibase/backend-core" import * as setup from "../../api/routes/tests/utilities" import * as migrations from "../migrations" -import { getLatestEnabledMigrationId } from "../index" +import { AppMigration, getLatestEnabledMigrationId } from "../index" import { getAppMigrationVersion } from "../appMigrationMetadata" jest.mock("../migrations", () => ({ @@ -54,25 +54,28 @@ 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 () => {}, - }) + it("should disable all migrations after one that is disabled", () => { + const MIGRATION_ID1 = "20231211105810_new-test", + MIGRATION_ID2 = "20231211105812_new-test", + MIGRATION_ID3 = "20231211105814_new-test" + // create some migrations to test with + const migrations: AppMigration[] = [ + { + id: MIGRATION_ID1, + func: async () => {}, + }, + { + id: MIGRATION_ID2, + func: async () => {}, + }, + { + id: MIGRATION_ID3, + func: async () => {}, + }, + ] - expect(getLatestEnabledMigrationId()).toBe("20231211105814_new-test") - - migrations.MIGRATIONS[1].disabled = true - expect(getLatestEnabledMigrationId()).toBe("20231211105810_new-test") + expect(getLatestEnabledMigrationId(migrations)).toBe(MIGRATION_ID3) + migrations[1].disabled = true + expect(getLatestEnabledMigrationId(migrations)).toBe(MIGRATION_ID1) }) }) diff --git a/packages/server/src/middleware/appMigrations.ts b/packages/server/src/middleware/appMigrations.ts index a8ea886ab2..fdaeb3a03d 100644 --- a/packages/server/src/middleware/appMigrations.ts +++ b/packages/server/src/middleware/appMigrations.ts @@ -5,8 +5,8 @@ import { env } from "@budibase/backend-core" export default async (ctx: UserCtx, next: any) => { const { appId } = ctx - // 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 + // migrations can be disabled via environment variable if you + // need to completely disable migrations, e.g. for testing if (env.SKIP_APP_MIGRATIONS) { return next() } diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 6e0e6e7496..4819b9d8d5 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -109,8 +109,12 @@ async function buildBaseDefinition(): Promise { export async function syncDefinition(): Promise { const db = context.getAppDB() let existing: SQLiteDefinition | undefined - if (await db.exists(SQLITE_DESIGN_DOC_ID)) { + try { existing = await db.get(SQLITE_DESIGN_DOC_ID) + } catch (err: any) { + if (err.status !== 404) { + throw err + } } const definition = await buildBaseDefinition() if (existing) {