diff --git a/packages/backend-core/src/db/Replication.ts b/packages/backend-core/src/db/Replication.ts index b46f6072be..e0bd3c7a43 100644 --- a/packages/backend-core/src/db/Replication.ts +++ b/packages/backend-core/src/db/Replication.ts @@ -1,4 +1,5 @@ import { dangerousGetDB, closeDB } from "." +import { DocumentType } from "./constants" class Replication { source: any @@ -53,6 +54,14 @@ class Replication { return this.replication } + appReplicateOpts() { + return { + filter: (doc: any) => { + return doc._id !== DocumentType.APP_METADATA + }, + } + } + /** * Rollback the target DB back to the state of the source DB */ @@ -60,6 +69,7 @@ class Replication { await this.target.destroy() // Recreate the DB again this.target = dangerousGetDB(this.target.name) + // take the opportunity to remove deleted tombstones await this.replicate() } diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index 926fe0ec52..ce3649f082 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -553,11 +553,7 @@ export const sync = async (ctx: any, next: any) => { }) let error try { - await replication.replicate({ - filter: function (doc: any) { - return doc._id !== DocumentType.APP_METADATA - }, - }) + await replication.replicate(replication.appReplicateOpts()) } catch (err) { error = err } finally { diff --git a/packages/server/src/api/controllers/deploy/index.ts b/packages/server/src/api/controllers/deploy/index.ts index d63e167dfb..86718294de 100644 --- a/packages/server/src/api/controllers/deploy/index.ts +++ b/packages/server/src/api/controllers/deploy/index.ts @@ -15,6 +15,7 @@ import { getAppId, getAppDB, getProdAppDB, + getDevAppDB, } from "@budibase/backend-core/context" import { quotas } from "@budibase/pro" import { events } from "@budibase/backend-core" @@ -110,17 +111,29 @@ async function deployApp(deployment: any) { target: productionAppId, } replication = new Replication(config) - + const devDb = getDevAppDB() + console.log("Compacting development DB") + await devDb.compact() console.log("Replication object created") - await replication.replicate() + await replication.replicate(replication.appReplicateOpts()) console.log("replication complete.. replacing app meta doc") + // app metadata is excluded as it is likely to be in conflict + // replicate the app metadata document manually const db = getProdAppDB() - const appDoc = await db.get(DocumentType.APP_METADATA) + const appDoc = await devDb.get(DocumentType.APP_METADATA) + try { + const prodAppDoc = await db.get(DocumentType.APP_METADATA) + appDoc._rev = prodAppDoc._rev + } catch (err) { + // ignore the error - doesn't exist + } + // switch to production app ID deployment.appUrl = appDoc.url - appDoc.appId = productionAppId appDoc.instance._id = productionAppId + // remove automation errors if they exist + delete appDoc.automationErrors await db.put(appDoc) await appCache.invalidateAppMetadata(productionAppId) console.log("New app doc written successfully.")