From 244fbe42b145666897e3a7db59b91b61ba3bc4b0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 6 Jun 2024 16:04:23 +0100 Subject: [PATCH] Updating migration to check if the SQL definitions have changed, if they haven't don't write them again. --- .../tests/migrations.integrity.spec.ts | 29 ++----------------- .../server/src/sdk/app/tables/internal/sqs.ts | 15 ++++++---- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/packages/server/src/appMigrations/tests/migrations.integrity.spec.ts b/packages/server/src/appMigrations/tests/migrations.integrity.spec.ts index 2de27ef9d4..145a06d7f5 100644 --- a/packages/server/src/appMigrations/tests/migrations.integrity.spec.ts +++ b/packages/server/src/appMigrations/tests/migrations.integrity.spec.ts @@ -2,23 +2,6 @@ import { context } from "@budibase/backend-core" import * as setup from "../../api/routes/tests/utilities" import * as migrations from "../migrations" -function removeChangeableKeys(documents: Document[]) { - const changeableKeys = ["createdAt", "updatedAt", "_rev", "rev"] - function iterate(obj: Record) { - for (let key of Object.keys(obj)) { - if (typeof obj[key] === "object") { - iterate(obj[key]) - } else if (changeableKeys.indexOf(key) !== -1) { - delete obj[key] - } - } - } - for (let doc of documents) { - iterate(doc) - } - return documents -} - describe("migration integrity", () => { // These test is checking that each migration is "idempotent". // We should be able to rerun any migration, with any rerun not modifiying anything. The code should be aware that the migration already ran @@ -30,18 +13,12 @@ describe("migration integrity", () => { const db = context.getAppDB() for (const migration of migrations.MIGRATIONS) { await migration.func() - const preResp = await db.allDocs({ include_docs: true }) + const docs = await db.allDocs({ include_docs: true }) await migration.func() - const postResp = await db.allDocs({ include_docs: true }) + const latestDocs = await db.allDocs({ include_docs: true }) - const preDocs = removeChangeableKeys( - preResp.rows.map(row => row.doc as Document) - ) - const postDocs = removeChangeableKeys( - postResp.rows.map(row => row.doc as Document) - ) - expect(preDocs).toEqual(postDocs) + expect(docs).toEqual(latestDocs) } }) }) diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 52a0cfbda0..6e0e6e7496 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -14,6 +14,7 @@ import { CONSTANT_INTERNAL_ROW_COLS, generateJunctionTableID, } from "../../../../db/utils" +import { isEqual } from "lodash" const FieldTypeMap: Record = { [FieldType.BOOLEAN]: SQLiteType.NUMERIC, @@ -107,16 +108,18 @@ async function buildBaseDefinition(): Promise { export async function syncDefinition(): Promise { const db = context.getAppDB() - let rev: string | undefined + let existing: SQLiteDefinition | undefined if (await db.exists(SQLITE_DESIGN_DOC_ID)) { - const existing = await db.get(SQLITE_DESIGN_DOC_ID) - rev = existing._rev + existing = await db.get(SQLITE_DESIGN_DOC_ID) } const definition = await buildBaseDefinition() - if (rev) { - definition._rev = rev + if (existing) { + definition._rev = existing._rev + } + // only write if something has changed + if (!existing || !isEqual(existing.sql, definition.sql)) { + await db.put(definition) } - await db.put(definition) } export async function addTable(table: Table) {