From 617863ddd1e846ba8dce991579b90a90e344c0fa Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 7 Dec 2022 14:32:29 +0000 Subject: [PATCH 1/4] Fix for #7865 - if you deleted a cron automation immediately after creating/publishing it there was a scenario where prod automations would be out of sync with dev automations and it wouldn't really delete. To get around this, we do an automation sync back to dev DB - meaning that the cron ID is known and can be disabled. --- packages/server/src/api/controllers/deploy/index.ts | 6 ++++-- packages/server/src/sdk/app/applications/sync.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/deploy/index.ts b/packages/server/src/api/controllers/deploy/index.ts index a87e7fbd7d..4daf1a1d53 100644 --- a/packages/server/src/api/controllers/deploy/index.ts +++ b/packages/server/src/api/controllers/deploy/index.ts @@ -8,6 +8,7 @@ import { } from "../../../automations/utils" import { backups } from "@budibase/pro" import { AppBackupTrigger } from "@budibase/types" +import sdk from "../../../sdk" // the max time we can wait for an invalidation to complete before considering it failed const MAX_PENDING_TIME_MS = 30 * 60000 @@ -86,6 +87,9 @@ async function initDeployedApp(prodAppId: any) { } await Promise.all(promises) console.log("Enabled cron triggers for deployed app..") + // sync the automations back to the dev DB - since there is now cron + // information attached + await sdk.applications.syncApp(dbCore.getDevAppID(prodAppId), { automationOnly: true }) } async function deployApp(deployment: any, userId: string) { @@ -112,8 +116,6 @@ async function deployApp(deployment: any, userId: string) { } replication = new dbCore.Replication(config) const devDb = context.getDevAppDB() - console.log("Compacting development DB") - await devDb.compact() console.log("Replication object created") await replication.replicate(replication.appReplicateOpts()) console.log("replication complete.. replacing app meta doc") diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index 5261eabb3d..dd32002713 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -2,7 +2,7 @@ import env from "../../../environment" import { db as dbCore, context } from "@budibase/backend-core" import sdk from "../../" -export async function syncApp(appId: string) { +export async function syncApp(appId: string, opts?: { automationOnly?: boolean }) { if (env.DISABLE_AUTO_PROD_APP_SYNC) { return { message: @@ -33,7 +33,11 @@ export async function syncApp(appId: string) { }) let error try { - await replication.replicate(replication.appReplicateOpts()) + const replOpts = replication.appReplicateOpts() + if (opts?.automationOnly) { + replOpts.filter = (doc: any) => doc._id.startsWith(dbCore.DocumentType.AUTOMATION) + } + await replication.replicate() } catch (err) { error = err } finally { From be0b15428bb056711d5eec595ae5b3b115ac848e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 7 Dec 2022 14:43:27 +0000 Subject: [PATCH 2/4] Linting. --- packages/server/src/api/controllers/deploy/index.ts | 4 +++- packages/server/src/sdk/app/applications/sync.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/deploy/index.ts b/packages/server/src/api/controllers/deploy/index.ts index 4daf1a1d53..9b80af889c 100644 --- a/packages/server/src/api/controllers/deploy/index.ts +++ b/packages/server/src/api/controllers/deploy/index.ts @@ -89,7 +89,9 @@ async function initDeployedApp(prodAppId: any) { console.log("Enabled cron triggers for deployed app..") // sync the automations back to the dev DB - since there is now cron // information attached - await sdk.applications.syncApp(dbCore.getDevAppID(prodAppId), { automationOnly: true }) + await sdk.applications.syncApp(dbCore.getDevAppID(prodAppId), { + automationOnly: true, + }) } async function deployApp(deployment: any, userId: string) { diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index dd32002713..477fa41532 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -2,7 +2,10 @@ import env from "../../../environment" import { db as dbCore, context } from "@budibase/backend-core" import sdk from "../../" -export async function syncApp(appId: string, opts?: { automationOnly?: boolean }) { +export async function syncApp( + appId: string, + opts?: { automationOnly?: boolean } +) { if (env.DISABLE_AUTO_PROD_APP_SYNC) { return { message: @@ -35,7 +38,8 @@ export async function syncApp(appId: string, opts?: { automationOnly?: boolean } try { const replOpts = replication.appReplicateOpts() if (opts?.automationOnly) { - replOpts.filter = (doc: any) => doc._id.startsWith(dbCore.DocumentType.AUTOMATION) + replOpts.filter = (doc: any) => + doc._id.startsWith(dbCore.DocumentType.AUTOMATION) } await replication.replicate() } catch (err) { From b3e5483eca9f287a463ef6327c4836c7bd1f7793 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 7 Dec 2022 18:09:17 +0000 Subject: [PATCH 3/4] Adding back compaction. --- packages/server/src/api/controllers/deploy/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/server/src/api/controllers/deploy/index.ts b/packages/server/src/api/controllers/deploy/index.ts index 9b80af889c..209aedfb62 100644 --- a/packages/server/src/api/controllers/deploy/index.ts +++ b/packages/server/src/api/controllers/deploy/index.ts @@ -118,6 +118,8 @@ async function deployApp(deployment: any, userId: string) { } replication = new dbCore.Replication(config) const devDb = context.getDevAppDB() + console.log("Compacting development DB") + await devDb.compact() console.log("Replication object created") await replication.replicate(replication.appReplicateOpts()) console.log("replication complete.. replacing app meta doc") From a120ba0c8d9ac9a9953e4bc2e4d059fd75e8b271 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 7 Dec 2022 18:22:02 +0000 Subject: [PATCH 4/4] Fixing repl options. --- packages/server/src/sdk/app/applications/sync.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index 477fa41532..6fb3576ae6 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -41,7 +41,7 @@ export async function syncApp( replOpts.filter = (doc: any) => doc._id.startsWith(dbCore.DocumentType.AUTOMATION) } - await replication.replicate() + await replication.replicate(replOpts) } catch (err) { error = err } finally {