Merge pull request #7685 from Budibase/fix/sept-various-fixes

Fix/sept various fixes
This commit is contained in:
Michael Drury 2022-09-12 12:16:31 +02:00 committed by GitHub
commit d79a9bfecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 275 additions and 94 deletions

View File

@ -1,4 +1,5 @@
import { dangerousGetDB, closeDB } from "." import { dangerousGetDB, closeDB } from "."
import { DocumentType } from "./constants"
class Replication { class Replication {
source: any source: any
@ -53,6 +54,14 @@ class Replication {
return this.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 * Rollback the target DB back to the state of the source DB
*/ */
@ -60,6 +69,7 @@ class Replication {
await this.target.destroy() await this.target.destroy()
// Recreate the DB again // Recreate the DB again
this.target = dangerousGetDB(this.target.name) this.target = dangerousGetDB(this.target.name)
// take the opportunity to remove deleted tombstones
await this.replicate() await this.replicate()
} }

View File

@ -254,7 +254,16 @@ export async function getAllApps({ dev, all, idsOnly, efficient }: any = {}) {
return false return false
}) })
if (idsOnly) { if (idsOnly) {
return appDbNames const devAppIds = appDbNames.filter(appId => isDevAppID(appId))
const prodAppIds = appDbNames.filter(appId => !isDevAppID(appId))
switch (dev) {
case true:
return devAppIds
case false:
return prodAppIds
default:
return appDbNames
}
} }
const appPromises = appDbNames.map((app: any) => const appPromises = appDbNames.map((app: any) =>
// skip setup otherwise databases could be re-created // skip setup otherwise databases could be re-created

View File

@ -10,6 +10,7 @@
export let noHorizPadding = false export let noHorizPadding = false
export let quiet = false export let quiet = false
export let emphasized = false export let emphasized = false
export let onTop = false
export let size = "M" export let size = "M"
let thisSelected = undefined let thisSelected = undefined
@ -75,6 +76,7 @@
bind:this={container} bind:this={container}
class:spectrum-Tabs--quiet={quiet} class:spectrum-Tabs--quiet={quiet}
class:noHorizPadding class:noHorizPadding
class:onTop
class:spectrum-Tabs--vertical={vertical} class:spectrum-Tabs--vertical={vertical}
class:spectrum-Tabs--horizontal={!vertical} class:spectrum-Tabs--horizontal={!vertical}
class="spectrum-Tabs spectrum-Tabs--size{size}" class="spectrum-Tabs spectrum-Tabs--size{size}"
@ -122,4 +124,7 @@
.noPadding { .noPadding {
margin: 0; margin: 0;
} }
.onTop {
z-index: 100;
}
</style> </style>

View File

@ -1,6 +1,7 @@
<script> <script>
import { Button, Select, Input, Label } from "@budibase/bbui" import { Button, Select, Input, Label } from "@budibase/bbui"
import { createEventDispatcher } from "svelte" import { onMount, createEventDispatcher } from "svelte"
import { flags } from "stores/backend"
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
export let value export let value
@ -29,11 +30,16 @@
label: "Every Night at Midnight", label: "Every Night at Midnight",
value: "0 0 * * *", value: "0 0 * * *",
}, },
{
label: "Every Budibase Reboot",
value: "@reboot",
},
] ]
onMount(() => {
if (!$flags.cloud) {
CRON_EXPRESSIONS.push({
label: "Every Budibase Reboot",
value: "@reboot",
})
}
})
</script> </script>
<div class="block-field"> <div class="block-field">

View File

@ -23,6 +23,7 @@
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let bindingDrawer let bindingDrawer
let valid = true let valid = true
let currentVal = value
$: readableValue = runtimeToReadableBinding(bindings, value) $: readableValue = runtimeToReadableBinding(bindings, value)
$: tempValue = readableValue $: tempValue = readableValue
@ -30,11 +31,17 @@
const saveBinding = () => { const saveBinding = () => {
onChange(tempValue) onChange(tempValue)
onBlur()
bindingDrawer.hide() bindingDrawer.hide()
} }
const onChange = value => { const onChange = value => {
dispatch("change", readableToRuntimeBinding(bindings, value)) currentVal = readableToRuntimeBinding(bindings, value)
dispatch("change", currentVal)
}
const onBlur = () => {
dispatch("blur", currentVal)
} }
</script> </script>
@ -45,6 +52,7 @@
readonly={isJS} readonly={isJS}
value={isJS ? "(JavaScript function)" : readableValue} value={isJS ? "(JavaScript function)" : readableValue}
on:change={event => onChange(event.detail)} on:change={event => onChange(event.detail)}
on:blur={onBlur}
{placeholder} {placeholder}
{updateOnChange} {updateOnChange}
/> />

View File

@ -107,7 +107,7 @@
placeholder={keyPlaceholder} placeholder={keyPlaceholder}
readonly={readOnly} readonly={readOnly}
bind:value={field.name} bind:value={field.name}
on:change={changed} on:blur={changed}
/> />
{#if options} {#if options}
<Select bind:value={field.value} on:change={changed} {options} /> <Select bind:value={field.value} on:change={changed} {options} />
@ -115,7 +115,10 @@
<DrawerBindableInput <DrawerBindableInput
{bindings} {bindings}
placeholder="Value" placeholder="Value"
on:change={e => (field.value = e.detail)} on:blur={e => {
field.value = e.detail
changed()
}}
disabled={readOnly} disabled={readOnly}
value={field.value} value={field.value}
allowJS={false} allowJS={false}
@ -127,7 +130,7 @@
placeholder={valuePlaceholder} placeholder={valuePlaceholder}
readonly={readOnly} readonly={readOnly}
bind:value={field.value} bind:value={field.value}
on:change={changed} on:blur={changed}
/> />
{/if} {/if}
{#if toggle} {#if toggle}

View File

@ -1,16 +1,24 @@
<script> <script>
import { ModalContent, Toggle } from "@budibase/bbui" import { ModalContent, Toggle, Body } from "@budibase/bbui"
export let app export let app
export let published
let excludeRows = false let excludeRows = false
$: title = published ? "Export published app" : "Export latest app"
$: confirmText = published ? "Export published" : "Export latest"
const exportApp = () => { const exportApp = () => {
const id = app.deployed ? app.prodId : app.devId const id = published ? app.prodId : app.devId
const appName = encodeURIComponent(app.name) const appName = encodeURIComponent(app.name)
window.location = `/api/backups/export?appId=${id}&appname=${appName}&excludeRows=${excludeRows}` window.location = `/api/backups/export?appId=${id}&appname=${appName}&excludeRows=${excludeRows}`
} }
</script> </script>
<ModalContent title={"Export"} confirmText={"Export"} onConfirm={exportApp}> <ModalContent {title} {confirmText} onConfirm={exportApp}>
<Body
>Apps can be exported with or without data that is within internal tables -
select this below.</Body
>
<Toggle text="Exclude Rows" bind:value={excludeRows} /> <Toggle text="Exclude Rows" bind:value={excludeRows} />
</ModalContent> </ModalContent>

View File

@ -46,7 +46,7 @@ export function buildQueryString(obj) {
if (str !== "") { if (str !== "") {
str += "&" str += "&"
} }
str += `${key}=${value || ""}` str += `${key}=${encodeURIComponent(value || "")}`
} }
} }
return str return str

View File

@ -28,25 +28,25 @@
import { onMount } from "svelte" import { onMount } from "svelte"
import restUtils from "helpers/data/utils" import restUtils from "helpers/data/utils"
import { import {
RestBodyTypes as bodyTypes,
SchemaTypeOptions,
PaginationLocations, PaginationLocations,
PaginationTypes, PaginationTypes,
RawRestBodyTypes,
RestBodyTypes as bodyTypes,
SchemaTypeOptions,
} from "constants/backend" } from "constants/backend"
import JSONPreview from "components/integration/JSONPreview.svelte" import JSONPreview from "components/integration/JSONPreview.svelte"
import AccessLevelSelect from "components/integration/AccessLevelSelect.svelte" import AccessLevelSelect from "components/integration/AccessLevelSelect.svelte"
import DynamicVariableModal from "../../_components/DynamicVariableModal.svelte" import DynamicVariableModal from "../../_components/DynamicVariableModal.svelte"
import Placeholder from "assets/bb-spaceship.svg" import Placeholder from "assets/bb-spaceship.svg"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { RawRestBodyTypes } from "constants/backend"
import { import {
getRestBindings, getRestBindings,
toBindingsArray,
runtimeToReadableBinding,
readableToRuntimeBinding, readableToRuntimeBinding,
runtimeToReadableMap,
readableToRuntimeMap, readableToRuntimeMap,
runtimeToReadableBinding,
runtimeToReadableMap,
toBindingsArray,
} from "builderStore/dataBinding" } from "builderStore/dataBinding"
let query, datasource let query, datasource
@ -95,7 +95,7 @@
$: runtimeUrlQueries = readableToRuntimeMap(mergedBindings, breakQs) $: runtimeUrlQueries = readableToRuntimeMap(mergedBindings, breakQs)
function getSelectedQuery() { function getSelectedQuery() {
const cloneQuery = cloneDeep( return cloneDeep(
$queries.list.find(q => q._id === $queries.selected) || { $queries.list.find(q => q._id === $queries.selected) || {
datasourceId: $params.selectedDatasource, datasourceId: $params.selectedDatasource,
parameters: [], parameters: [],
@ -107,7 +107,6 @@
queryVerb: "read", queryVerb: "read",
} }
) )
return cloneQuery
} }
function checkQueryName(inputUrl = null) { function checkQueryName(inputUrl = null) {
@ -121,14 +120,15 @@
if (!base) { if (!base) {
return base return base
} }
const qs = restUtils.buildQueryString( let qs = restUtils.buildQueryString(
runtimeToReadableMap(mergedBindings, qsObj) runtimeToReadableMap(mergedBindings, qsObj)
) )
let newUrl = base let newUrl = base
if (base.includes("?")) { if (base.includes("?")) {
newUrl = base.split("?")[0] const split = base.split("?")
newUrl = split[0]
} }
return qs.length > 0 ? `${newUrl}?${qs}` : newUrl return qs.length === 0 ? newUrl : `${newUrl}?${qs}`
} }
function buildQuery() { function buildQuery() {
@ -314,6 +314,25 @@
} }
} }
const paramsChanged = evt => {
breakQs = {}
for (let param of evt.detail) {
breakQs[param.name] = param.value
}
}
const urlChanged = evt => {
breakQs = {}
const qs = evt.target.value.split("?")[1]
if (qs && qs.length > 0) {
const parts = qs.split("&")
for (let part of parts) {
const [key, value] = part.split("=")
breakQs[key] = value
}
}
}
onMount(async () => { onMount(async () => {
query = getSelectedQuery() query = getSelectedQuery()
@ -426,7 +445,11 @@
/> />
</div> </div>
<div class="url"> <div class="url">
<Input bind:value={url} placeholder="http://www.api.com/endpoint" /> <Input
on:blur={urlChanged}
bind:value={url}
placeholder="http://www.api.com/endpoint"
/>
</div> </div>
<Button primary disabled={!url} on:click={runQuery}>Send</Button> <Button primary disabled={!url} on:click={runQuery}>Send</Button>
<Button <Button
@ -456,13 +479,16 @@
/> />
</Tab> </Tab>
<Tab title="Params"> <Tab title="Params">
<KeyValueBuilder {#key breakQs}
bind:object={breakQs} <KeyValueBuilder
name="param" on:change={paramsChanged}
headings object={breakQs}
bindings={mergedBindings} name="param"
bindingDrawerLeft="260px" headings
/> bindings={mergedBindings}
bindingDrawerLeft="260px"
/>
{/key}
</Tab> </Tab>
<Tab title="Headers"> <Tab title="Headers">
<KeyValueBuilder <KeyValueBuilder

View File

@ -15,7 +15,6 @@
import Spinner from "components/common/Spinner.svelte" import Spinner from "components/common/Spinner.svelte"
import CreateAppModal from "components/start/CreateAppModal.svelte" import CreateAppModal from "components/start/CreateAppModal.svelte"
import UpdateAppModal from "components/start/UpdateAppModal.svelte" import UpdateAppModal from "components/start/UpdateAppModal.svelte"
import ExportAppModal from "components/start/ExportAppModal.svelte"
import { store, automationStore } from "builderStore" import { store, automationStore } from "builderStore"
import { API } from "api" import { API } from "api"
@ -33,7 +32,6 @@
let selectedApp let selectedApp
let creationModal let creationModal
let updatingModal let updatingModal
let exportModal
let creatingApp = false let creatingApp = false
let loaded = $apps?.length || $templates?.length let loaded = $apps?.length || $templates?.length
let searchTerm = "" let searchTerm = ""
@ -407,10 +405,6 @@
<UpdateAppModal app={selectedApp} /> <UpdateAppModal app={selectedApp} />
</Modal> </Modal>
<Modal bind:this={exportModal} padding={false} width="600px">
<ExportAppModal app={selectedApp} />
</Modal>
<style> <style>
.appTable { .appTable {
border-top: var(--border-light); border-top: var(--border-light);

View File

@ -16,6 +16,7 @@
MenuItem, MenuItem,
Icon, Icon,
Helpers, Helpers,
Modal,
} from "@budibase/bbui" } from "@budibase/bbui"
import OverviewTab from "../_components/OverviewTab.svelte" import OverviewTab from "../_components/OverviewTab.svelte"
import SettingsTab from "../_components/SettingsTab.svelte" import SettingsTab from "../_components/SettingsTab.svelte"
@ -29,6 +30,7 @@
import EditableIcon from "components/common/EditableIcon.svelte" import EditableIcon from "components/common/EditableIcon.svelte"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import HistoryTab from "components/portal/overview/automation/HistoryTab.svelte" import HistoryTab from "components/portal/overview/automation/HistoryTab.svelte"
import ExportAppModal from "components/start/ExportAppModal.svelte"
import { checkIncomingDeploymentStatus } from "components/deploy/utils" import { checkIncomingDeploymentStatus } from "components/deploy/utils"
import { onDestroy, onMount } from "svelte" import { onDestroy, onMount } from "svelte"
@ -38,7 +40,9 @@
let loaded = false let loaded = false
let deletionModal let deletionModal
let unpublishModal let unpublishModal
let exportModal
let appName = "" let appName = ""
let published
// App // App
$: filteredApps = $apps.filter(app => app.devId === application) $: filteredApps = $apps.filter(app => app.devId === application)
@ -140,11 +144,9 @@
notifications.success("App ID copied to clipboard.") notifications.success("App ID copied to clipboard.")
} }
const exportApp = (app, opts = { published: false }) => { const exportApp = opts => {
const appName = encodeURIComponent(app.name) published = opts.published
const id = opts?.published ? app.prodId : app.devId exportModal.show()
// always export the development version
window.location = `/api/backups/export?appId=${id}&appname=${appName}`
} }
const unpublishApp = app => { const unpublishApp = app => {
@ -206,6 +208,10 @@
}) })
</script> </script>
<Modal bind:this={exportModal} padding={false} width="600px">
<ExportAppModal app={selectedApp} {published} />
</Modal>
<span class="overview-wrap"> <span class="overview-wrap">
<Page wide noPadding> <Page wide noPadding>
{#await promise} {#await promise}
@ -269,14 +275,14 @@
<Icon hoverable name="More" /> <Icon hoverable name="More" />
</span> </span>
<MenuItem <MenuItem
on:click={() => exportApp(selectedApp, { published: false })} on:click={() => exportApp({ published: false })}
icon="DownloadFromCloud" icon="DownloadFromCloud"
> >
Export latest Export latest
</MenuItem> </MenuItem>
{#if isPublished} {#if isPublished}
<MenuItem <MenuItem
on:click={() => exportApp(selectedApp, { published: true })} on:click={() => exportApp({ published: true })}
icon="DownloadFromCloudOutline" icon="DownloadFromCloudOutline"
> >
Export published Export published

View File

@ -553,11 +553,7 @@ export const sync = async (ctx: any, next: any) => {
}) })
let error let error
try { try {
await replication.replicate({ await replication.replicate(replication.appReplicateOpts())
filter: function (doc: any) {
return doc._id !== DocumentType.APP_METADATA
},
})
} catch (err) { } catch (err) {
error = err error = err
} finally { } finally {

View File

@ -1,11 +1,12 @@
const { streamBackup } = require("../../utilities/fileSystem") const { streamBackup } = require("../../utilities/fileSystem")
const { events, context } = require("@budibase/backend-core") const { events, context } = require("@budibase/backend-core")
const { DocumentType } = require("../../db/utils") const { DocumentType } = require("../../db/utils")
const { isQsTrue } = require("../../utilities")
exports.exportAppDump = async function (ctx) { exports.exportAppDump = async function (ctx) {
let { appId, excludeRows } = ctx.query let { appId, excludeRows } = ctx.query
const appName = decodeURI(ctx.query.appname) const appName = decodeURI(ctx.query.appname)
excludeRows = excludeRows === "true" excludeRows = isQsTrue(excludeRows)
const backupIdentifier = `${appName}-export-${new Date().getTime()}.txt` const backupIdentifier = `${appName}-export-${new Date().getTime()}.txt`
ctx.attachment(backupIdentifier) ctx.attachment(backupIdentifier)
ctx.body = await streamBackup(appId, excludeRows) ctx.body = await streamBackup(appId, excludeRows)

View File

@ -15,6 +15,7 @@ import {
getAppId, getAppId,
getAppDB, getAppDB,
getProdAppDB, getProdAppDB,
getDevAppDB,
} from "@budibase/backend-core/context" } from "@budibase/backend-core/context"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { events } from "@budibase/backend-core" import { events } from "@budibase/backend-core"
@ -110,17 +111,29 @@ async function deployApp(deployment: any) {
target: productionAppId, target: productionAppId,
} }
replication = new Replication(config) replication = new Replication(config)
const devDb = getDevAppDB()
console.log("Compacting development DB")
await devDb.compact()
console.log("Replication object created") console.log("Replication object created")
await replication.replicate() await replication.replicate(replication.appReplicateOpts())
console.log("replication complete.. replacing app meta doc") 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 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) {
delete appDoc._rev
}
// switch to production app ID
deployment.appUrl = appDoc.url deployment.appUrl = appDoc.url
appDoc.appId = productionAppId appDoc.appId = productionAppId
appDoc.instance._id = productionAppId appDoc.instance._id = productionAppId
// remove automation errors if they exist
delete appDoc.automationErrors
await db.put(appDoc) await db.put(appDoc)
await appCache.invalidateAppMetadata(productionAppId) await appCache.invalidateAppMetadata(productionAppId)
console.log("New app doc written successfully.") console.log("New app doc written successfully.")

View File

@ -534,7 +534,7 @@ module External {
}) })
// this is the response from knex if no rows found // this is the response from knex if no rows found
const rows = !response[0].read ? response : [] const rows = !response[0].read ? response : []
const storeTo = isMany ? field.throughFrom || linkPrimaryKey : manyKey const storeTo = isMany ? field.throughFrom || linkPrimaryKey : fieldName
related[storeTo] = { rows, isMany, tableId } related[storeTo] = { rows, isMany, tableId }
} }
return related return related

View File

@ -311,7 +311,7 @@ describe("/queries", () => {
"url": "string", "url": "string",
"value": "string" "value": "string"
}) })
expect(res.body.rows[0].url).toContain("doctype html") expect(res.body.rows[0].url).toContain("doctype%20html")
}) })
it("check that it automatically retries on fail with cached dynamics", async () => { it("check that it automatically retries on fail with cached dynamics", async () => {
@ -396,7 +396,7 @@ describe("/queries", () => {
"queryHdr": userDetails.firstName, "queryHdr": userDetails.firstName,
"secondHdr" : "1234" "secondHdr" : "1234"
}) })
expect(res.body.rows[0].url).toEqual("http://www.google.com?email=" + userDetails.email) expect(res.body.rows[0].url).toEqual("http://www.google.com?email=" + userDetails.email.replace("@", "%40"))
}) })
it("should bind the current user to query parameters", async () => { it("should bind the current user to query parameters", async () => {
@ -413,7 +413,7 @@ describe("/queries", () => {
"testParam" : "1234" "testParam" : "1234"
}) })
expect(res.body.rows[0].url).toEqual("http://www.google.com?test=" + userDetails.email + expect(res.body.rows[0].url).toEqual("http://www.google.com?test=" + userDetails.email.replace("@", "%40") +
"&testName=" + userDetails.firstName + "&testParam=1234") "&testName=" + userDetails.firstName + "&testParam=1234")
}) })

View File

@ -1,16 +1,19 @@
const { processEvent } = require("./utils") const { processEvent } = require("./utils")
const { queue, shutdown } = require("./bullboard") const { queue, shutdown } = require("./bullboard")
const { TRIGGER_DEFINITIONS } = require("./triggers") const { TRIGGER_DEFINITIONS, rebootTrigger } = require("./triggers")
const { ACTION_DEFINITIONS } = require("./actions") const { ACTION_DEFINITIONS } = require("./actions")
/** /**
* This module is built purely to kick off the worker farm and manage the inputs/outputs * This module is built purely to kick off the worker farm and manage the inputs/outputs
*/ */
exports.init = function () { exports.init = async function () {
// this promise will not complete // this promise will not complete
return queue.process(async job => { const promise = queue.process(async job => {
await processEvent(job) await processEvent(job)
}) })
// on init we need to trigger any reboot automations
await rebootTrigger()
return promise
} }
exports.getQueues = () => { exports.getQueues = () => {

View File

@ -9,6 +9,7 @@ const { checkTestFlag } = require("../utilities/redis")
const utils = require("./utils") const utils = require("./utils")
const env = require("../environment") const env = require("../environment")
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
const { getAllApps } = require("@budibase/backend-core/db")
const TRIGGER_DEFINITIONS = definitions const TRIGGER_DEFINITIONS = definitions
const JOB_OPTS = { const JOB_OPTS = {
@ -16,24 +17,27 @@ const JOB_OPTS = {
removeOnFail: true, removeOnFail: true,
} }
async function getAllAutomations() {
const db = getAppDB()
let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true })
)
return automations.rows.map(row => row.doc)
}
async function queueRelevantRowAutomations(event, eventType) { async function queueRelevantRowAutomations(event, eventType) {
if (event.appId == null) { if (event.appId == null) {
throw `No appId specified for ${eventType} - check event emitters.` throw `No appId specified for ${eventType} - check event emitters.`
} }
doInAppContext(event.appId, async () => { doInAppContext(event.appId, async () => {
const db = getAppDB() let automations = await getAllAutomations()
let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true })
)
// filter down to the correct event type // filter down to the correct event type
automations = automations.rows automations = automations.filter(automation => {
.map(automation => automation.doc) const trigger = automation.definition.trigger
.filter(automation => { return trigger && trigger.event === eventType
const trigger = automation.definition.trigger })
return trigger && trigger.event === eventType
})
for (let automation of automations) { for (let automation of automations) {
let automationDef = automation.definition let automationDef = automation.definition
@ -110,4 +114,34 @@ exports.externalTrigger = async function (
} }
} }
exports.rebootTrigger = async () => {
// reboot cron option is only available on the main thread at
// startup and only usable in self host
if (env.isInThread() || !env.SELF_HOSTED) {
return
}
// iterate through all production apps, find the reboot crons
// and trigger events for them
const appIds = await getAllApps({ dev: false, idsOnly: true })
for (let prodAppId of appIds) {
await doInAppContext(prodAppId, async () => {
let automations = await getAllAutomations()
let rebootEvents = []
for (let automation of automations) {
if (utils.isRebootTrigger(automation)) {
const job = {
automation,
event: {
appId: prodAppId,
timestamp: Date.now(),
},
}
rebootEvents.push(queue.add(job, JOB_OPTS))
}
}
await Promise.all(rebootEvents)
})
}
}
exports.TRIGGER_DEFINITIONS = TRIGGER_DEFINITIONS exports.TRIGGER_DEFINITIONS = TRIGGER_DEFINITIONS

View File

@ -17,6 +17,7 @@ import { tenancy } from "@budibase/backend-core"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { Automation } from "@budibase/types" import { Automation } from "@budibase/types"
const REBOOT_CRON = "@reboot"
const WH_STEP_ID = definitions.WEBHOOK.stepId const WH_STEP_ID = definitions.WEBHOOK.stepId
const CRON_STEP_ID = definitions.CRON.stepId const CRON_STEP_ID = definitions.CRON.stepId
const Runner = new Thread(ThreadType.AUTOMATION) const Runner = new Thread(ThreadType.AUTOMATION)
@ -109,22 +110,33 @@ export async function clearMetadata() {
await db.bulkDocs(automationMetadata) await db.bulkDocs(automationMetadata)
} }
export function isCronTrigger(auto: Automation) {
return (
auto &&
auto.definition.trigger &&
auto.definition.trigger.stepId === CRON_STEP_ID
)
}
export function isRebootTrigger(auto: Automation) {
const trigger = auto ? auto.definition.trigger : null
return isCronTrigger(auto) && trigger?.inputs.cron === REBOOT_CRON
}
/** /**
* This function handles checking of any cron jobs that need to be enabled/updated. * This function handles checking of any cron jobs that need to be enabled/updated.
* @param {string} appId The ID of the app in which we are checking for webhooks * @param {string} appId The ID of the app in which we are checking for webhooks
* @param {object|undefined} automation The automation object to be updated. * @param {object|undefined} automation The automation object to be updated.
*/ */
export async function enableCronTrigger(appId: any, automation: any) { export async function enableCronTrigger(appId: any, automation: Automation) {
const trigger = automation ? automation.definition.trigger : null const trigger = automation ? automation.definition.trigger : null
function isCronTrigger(auto: any) {
return (
auto &&
auto.definition.trigger &&
auto.definition.trigger.stepId === CRON_STEP_ID
)
}
// need to create cron job // need to create cron job
if (isCronTrigger(automation) && trigger?.inputs.cron) { if (
isCronTrigger(automation) &&
!isRebootTrigger(automation) &&
trigger?.inputs.cron
) {
// make a job id rather than letting Bull decide, makes it easier to handle on way out // make a job id rather than letting Bull decide, makes it easier to handle on way out
const jobId = `${appId}_cron_${newid()}` const jobId = `${appId}_cron_${newid()}`
const job: any = await queue.add( const job: any = await queue.add(

View File

@ -14,6 +14,7 @@ import {
BearerAuthConfig, BearerAuthConfig,
} from "../definitions/datasource" } from "../definitions/datasource"
import { get } from "lodash" import { get } from "lodash"
import qs from "querystring"
const BodyTypes = { const BodyTypes = {
NONE: "none", NONE: "none",
@ -215,7 +216,8 @@ module RestModule {
} }
} }
const main = `${path}?${queryString}` // make sure the query string is fully encoded
const main = `${path}?${qs.encode(qs.decode(queryString))}`
let complete = main let complete = main
if (this.config.url && !main.startsWith("http")) { if (this.config.url && !main.startsWith("http")) {
complete = !this.config.url ? main : `${this.config.url}/${main}` complete = !this.config.url ? main : `${this.config.url}/${main}`

View File

@ -50,7 +50,7 @@ describe("REST Integration", () => {
name: "test", name: "test",
}), }),
} }
const response = await config.integration.create(query) await config.integration.create(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST", method: "POST",
body: '{"name":"test"}', body: '{"name":"test"}',
@ -295,7 +295,7 @@ describe("REST Integration", () => {
} }
await config.integration.read(query) await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith( expect(fetch).toHaveBeenCalledWith(
`${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}&`, `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}`,
{ {
headers: {}, headers: {},
method: "GET", method: "GET",
@ -420,7 +420,7 @@ describe("REST Integration", () => {
} }
const res = await config.integration.read(query) const res = await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith( expect(fetch).toHaveBeenCalledWith(
`${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}&`, `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}`,
{ {
headers: {}, headers: {},
method: "GET", method: "GET",
@ -528,5 +528,23 @@ describe("REST Integration", () => {
expect(sentData.get(sizeParam)).toEqual(sizeValue.toString()) expect(sentData.get(sizeParam)).toEqual(sizeValue.toString())
expect(res.pagination.cursor).toEqual(123) expect(res.pagination.cursor).toEqual(123)
}) })
it("should encode query string correctly", async () => {
const query = {
path: "api",
queryString: "test=1 2",
headers: HEADERS,
bodyType: "json",
requestBody: JSON.stringify({
name: "test",
}),
}
await config.integration.create(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1%202`, {
method: "POST",
body: '{"name":"test"}',
headers: HEADERS,
})
})
}) })
}) })

View File

@ -458,6 +458,9 @@ class Orchestrator {
export function execute(input: AutomationEvent, callback: WorkerCallback) { export function execute(input: AutomationEvent, callback: WorkerCallback) {
const appId = input.data.event.appId const appId = input.data.event.appId
if (!appId) {
throw new Error("Unable to execute, event doesn't contain app ID.")
}
doInAppContext(appId, async () => { doInAppContext(appId, async () => {
const automationOrchestrator = new Orchestrator( const automationOrchestrator = new Orchestrator(
input.data.automation, input.data.automation,
@ -475,6 +478,9 @@ export function execute(input: AutomationEvent, callback: WorkerCallback) {
export const removeStalled = async (input: AutomationEvent) => { export const removeStalled = async (input: AutomationEvent) => {
const appId = input.data.event.appId const appId = input.data.event.appId
if (!appId) {
throw new Error("Unable to execute, event doesn't contain app ID.")
}
await doInAppContext(appId, async () => { await doInAppContext(appId, async () => {
const automationOrchestrator = new Orchestrator( const automationOrchestrator = new Orchestrator(
input.data.automation, input.data.automation,

View File

@ -125,13 +125,13 @@ exports.defineFilter = excludeRows => {
* data or user relationships. * data or user relationships.
* @param {string} appId The app to backup * @param {string} appId The app to backup
* @param {object} config Config to send to export DB * @param {object} config Config to send to export DB
* @param {boolean} includeRows Flag to state whether the export should include data. * @param {boolean} excludeRows Flag to state whether the export should include data.
* @returns {*} either a string or a stream of the backup * @returns {*} either a string or a stream of the backup
*/ */
const backupAppData = async (appId, config, includeRows) => { const backupAppData = async (appId, config, excludeRows) => {
return await exports.exportDB(appId, { return await exports.exportDB(appId, {
...config, ...config,
filter: exports.defineFilter(includeRows), filter: exports.defineFilter(excludeRows),
}) })
} }
@ -148,11 +148,11 @@ exports.performBackup = async (appId, backupName) => {
/** /**
* Streams a backup of the database state for an app * Streams a backup of the database state for an app
* @param {string} appId The ID of the app which is to be backed up. * @param {string} appId The ID of the app which is to be backed up.
* @param {boolean} includeRows Flag to state whether the export should include data. * @param {boolean} excludeRows Flag to state whether the export should include data.
* @returns {*} a readable stream of the backup which is written in real time * @returns {*} a readable stream of the backup which is written in real time
*/ */
exports.streamBackup = async (appId, includeRows) => { exports.streamBackup = async (appId, excludeRows) => {
return await backupAppData(appId, { stream: true }, includeRows) return await backupAppData(appId, { stream: true }, excludeRows)
} }
/** /**

View File

@ -162,3 +162,11 @@ exports.convertBookmark = bookmark => {
} }
return bookmark return bookmark
} }
exports.isQsTrue = param => {
if (typeof param === "string") {
return param.toLowerCase() === "true"
} else {
return param === true
}
}

View File

@ -76,7 +76,7 @@ exports.processDates = (table, rows) => {
if (schema.type !== FieldTypes.DATETIME) { if (schema.type !== FieldTypes.DATETIME) {
continue continue
} }
if (!schema.ignoreTimezones) { if (!schema.timeOnly && !schema.ignoreTimezones) {
datesWithTZ.push(column) datesWithTZ.push(column)
} }
} }

View File

@ -72,7 +72,8 @@ exports.getUniqueRows = async appIds => {
// ensure uniqueness on a per app pair basis // ensure uniqueness on a per app pair basis
// this can't be done on all rows because app import results in // this can't be done on all rows because app import results in
// duplicate row ids across apps // duplicate row ids across apps
uniqueRows = uniqueRows.concat(...new Set(appRows)) // the array pre-concat is important to avoid stack overflow
uniqueRows = uniqueRows.concat([...new Set(appRows)])
} }
return uniqueRows return uniqueRows

View File

@ -272,6 +272,14 @@ describe("test the string helpers", () => {
) )
expect(output).toBe("Hi!") expect(output).toBe("Hi!")
}) })
it("should allow use of the ellipsis helper", async () => {
const output = await processString(
"{{ ellipsis \"adfasdfasdfasf\" 7 }}",
{},
)
expect(output).toBe("adfasdf…")
})
}) })
describe("test the comparison helpers", () => { describe("test the comparison helpers", () => {

View File

@ -25,6 +25,10 @@ export interface AutomationStep {
export interface AutomationTrigger { export interface AutomationTrigger {
id: string id: string
stepId: string stepId: string
inputs: {
[key: string]: any
}
cronJobId?: string
} }
export enum AutomationStatus { export enum AutomationStatus {