Updating migration to check if the SQL definitions have changed, if they haven't don't write them again.
This commit is contained in:
parent
3c4cf69463
commit
244fbe42b1
|
@ -2,23 +2,6 @@ import { context } 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"
|
||||||
|
|
||||||
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", () => {
|
describe("migration integrity", () => {
|
||||||
// These test is checking that each migration is "idempotent".
|
// 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
|
// 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()
|
const db = context.getAppDB()
|
||||||
for (const migration of migrations.MIGRATIONS) {
|
for (const migration of migrations.MIGRATIONS) {
|
||||||
await migration.func()
|
await migration.func()
|
||||||
const preResp = await db.allDocs({ include_docs: true })
|
const docs = await db.allDocs({ include_docs: true })
|
||||||
|
|
||||||
await migration.func()
|
await migration.func()
|
||||||
const postResp = await db.allDocs({ include_docs: true })
|
const latestDocs = await db.allDocs({ include_docs: true })
|
||||||
|
|
||||||
const preDocs = removeChangeableKeys(
|
expect(docs).toEqual(latestDocs)
|
||||||
preResp.rows.map(row => row.doc as Document)
|
|
||||||
)
|
|
||||||
const postDocs = removeChangeableKeys(
|
|
||||||
postResp.rows.map(row => row.doc as Document)
|
|
||||||
)
|
|
||||||
expect(preDocs).toEqual(postDocs)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {
|
||||||
CONSTANT_INTERNAL_ROW_COLS,
|
CONSTANT_INTERNAL_ROW_COLS,
|
||||||
generateJunctionTableID,
|
generateJunctionTableID,
|
||||||
} from "../../../../db/utils"
|
} from "../../../../db/utils"
|
||||||
|
import { isEqual } from "lodash"
|
||||||
|
|
||||||
const FieldTypeMap: Record<FieldType, SQLiteType> = {
|
const FieldTypeMap: Record<FieldType, SQLiteType> = {
|
||||||
[FieldType.BOOLEAN]: SQLiteType.NUMERIC,
|
[FieldType.BOOLEAN]: SQLiteType.NUMERIC,
|
||||||
|
@ -107,16 +108,18 @@ async function buildBaseDefinition(): Promise<PreSaveSQLiteDefinition> {
|
||||||
|
|
||||||
export async function syncDefinition(): Promise<void> {
|
export async function syncDefinition(): Promise<void> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
let rev: string | undefined
|
let existing: SQLiteDefinition | undefined
|
||||||
if (await db.exists(SQLITE_DESIGN_DOC_ID)) {
|
if (await db.exists(SQLITE_DESIGN_DOC_ID)) {
|
||||||
const existing = await db.get(SQLITE_DESIGN_DOC_ID)
|
existing = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||||
rev = existing._rev
|
|
||||||
}
|
}
|
||||||
const definition = await buildBaseDefinition()
|
const definition = await buildBaseDefinition()
|
||||||
if (rev) {
|
if (existing) {
|
||||||
definition._rev = rev
|
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) {
|
export async function addTable(table: Table) {
|
||||||
|
|
Loading…
Reference in New Issue