From 0a7ceda807d8eb53ede80526c80541c39f782c9c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 18 Jul 2022 17:38:58 +0100 Subject: [PATCH 1/2] Fixing an issue with automations being unable to access app databases due to new context stack up. --- packages/backend-core/src/context/index.ts | 1 + packages/server/src/threads/automation.js | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/backend-core/src/context/index.ts b/packages/backend-core/src/context/index.ts index e0db18dde6..487585ea5f 100644 --- a/packages/backend-core/src/context/index.ts +++ b/packages/backend-core/src/context/index.ts @@ -67,6 +67,7 @@ export const getTenantIDFromAppID = (appId: string) => { // used for automations, API endpoints should always be in context already export const doInTenant = (tenantId: string | null, task: any) => { + tenantId = tenantId || DEFAULT_TENANT_ID // the internal function is so that we can re-use an existing // context - don't want to close DB on a parent context async function internal(opts = { existing: false }) { diff --git a/packages/server/src/threads/automation.js b/packages/server/src/threads/automation.js index 2ba7175bfd..8880f0cbcb 100644 --- a/packages/server/src/threads/automation.js +++ b/packages/server/src/threads/automation.js @@ -3,9 +3,7 @@ const actions = require("../automations/actions") const automationUtils = require("../automations/automationUtils") const AutomationEmitter = require("../events/AutomationEmitter") const { processObject } = require("@budibase/string-templates") -const { DEFAULT_TENANT_ID } = require("@budibase/backend-core/constants") const { DocumentTypes } = require("../db/utils") -const { doInTenant } = require("@budibase/backend-core/tenancy") const { definitions: triggerDefs } = require("../automations/triggerInfo") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { AutomationErrors, LoopStepTypes } = require("../constants") @@ -134,7 +132,6 @@ class Orchestrator { async execute() { let automation = this._automation - const app = await this.getApp() let stopped = false let loopStep = null @@ -264,14 +261,11 @@ class Orchestrator { inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs) try { // appId is always passed - let tenantId = app.tenantId || DEFAULT_TENANT_ID - const outputs = await doInTenant(tenantId, () => { - return stepFn({ - inputs: inputs, - appId: this._appId, - emitter: this._emitter, - context: this._context, - }) + const outputs = await stepFn({ + inputs: inputs, + appId: this._appId, + emitter: this._emitter, + context: this._context, }) this._context.steps[stepCount] = outputs // if filter causes us to stop execution don't break the loop, set a var From d2987fcf397e49116d1005f216b606b6ac10c1be Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 18 Jul 2022 18:18:01 +0100 Subject: [PATCH 2/2] PR comments. --- packages/backend-core/src/context/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/context/index.ts b/packages/backend-core/src/context/index.ts index 487585ea5f..1e430f01de 100644 --- a/packages/backend-core/src/context/index.ts +++ b/packages/backend-core/src/context/index.ts @@ -67,7 +67,10 @@ export const getTenantIDFromAppID = (appId: string) => { // used for automations, API endpoints should always be in context already export const doInTenant = (tenantId: string | null, task: any) => { - tenantId = tenantId || DEFAULT_TENANT_ID + // make sure default always selected in single tenancy + if (!env.MULTI_TENANCY) { + tenantId = tenantId || DEFAULT_TENANT_ID + } // the internal function is so that we can re-use an existing // context - don't want to close DB on a parent context async function internal(opts = { existing: false }) {