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

Updating migration to check if the SQL definitions have changed, if they haven't don't write them again.

This commit is contained in:
mike12345567 2024-06-06 16:04:23 +01:00
parent 3c4cf69463
commit 244fbe42b1
2 changed files with 12 additions and 32 deletions

View file

@ -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<string, any>) {
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)
}
})
})

View file

@ -14,6 +14,7 @@ import {
CONSTANT_INTERNAL_ROW_COLS,
generateJunctionTableID,
} from "../../../../db/utils"
import { isEqual } from "lodash"
const FieldTypeMap: Record<FieldType, SQLiteType> = {
[FieldType.BOOLEAN]: SQLiteType.NUMERIC,
@ -107,16 +108,18 @@ async function buildBaseDefinition(): Promise<PreSaveSQLiteDefinition> {
export async function syncDefinition(): Promise<void> {
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<SQLiteDefinition>(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) {