Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table/column deletion checks and disabling fetched external table deletion #3477

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/bbui/src/Form/Core/TextField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
export let readonly = false
export let updateOnChange = true
export let quiet = false
export let dataCy

const dispatch = createEventDispatcher()
let focus = false
Expand Down Expand Up @@ -78,6 +79,7 @@
{disabled}
{readonly}
{id}
data-cy={dataCy}
value={value || ""}
placeholder={placeholder || ""}
on:click
Expand Down
2 changes: 2 additions & 0 deletions packages/bbui/src/Form/Input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
export let error = null
export let updateOnChange = true
export let quiet = false
export let dataCy

const dispatch = createEventDispatcher()
const onChange = e => {
Expand All @@ -23,6 +24,7 @@

<Field {label} {labelPosition} {error}>
<TextField
{dataCy}
{updateOnChange}
{error}
{disabled}
Expand Down
2 changes: 2 additions & 0 deletions packages/builder/cypress/integration/createTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ context("Create a Table", () => {
cy.get(".spectrum-Table-editIcon > use").click()
cy.contains("Delete").click()
cy.wait(50)
cy.get(`[data-cy="delete-column-confirm"]`).type("nameupdated")
cy.contains("Delete Column").click()
cy.contains("nameupdated").should("not.exist")
})
Expand All @@ -66,6 +67,7 @@ context("Create a Table", () => {
cy.get(".actions .spectrum-Icon").click({ force: true })
})
cy.get(".spectrum-Menu > :nth-child(2)").click()
cy.get(`[data-cy="delete-table-confirm"]`).type("dog")
cy.contains("Delete Table").click()
cy.contains("dog").should("not.exist")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
let indexes = [...($tables.selected.indexes || [])]
let confirmDeleteDialog
let deletion
let deleteColName

$: checkConstraints(field)
$: required = !!field?.constraints?.presence || primaryDisplay
Expand Down Expand Up @@ -179,6 +180,7 @@

function hideDeleteDialog() {
confirmDeleteDialog.hide()
deleteColName = ""
deletion = false
}

Expand Down Expand Up @@ -408,9 +410,20 @@
</ModalContent>
<ConfirmDialog
bind:this={confirmDeleteDialog}
body={`Are you sure you wish to delete this column? Your data will be deleted and this action cannot be undone.`}
okText="Delete Column"
onOk={deleteColumn}
onCancel={hideDeleteDialog}
title="Confirm Deletion"
/>
disabled={deleteColName !== field.name}
>
<p>
Are you sure you wish to delete the column <b>{field.name}?</b>
Your data will be deleted and this action cannot be undone - enter the column
name to confirm.
</p>
<Input
dataCy="delete-column-confirm"
bind:value={deleteColName}
placeholder={field.name}
/>
</ConfirmDialog>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
let originalName = table.name
let templateScreens
let willBeDeleted
let deleteTableName

$: external = table?.type === "external"
$: allowDeletion = !external || table?.created

function showDeleteModal() {
templateScreens = $allScreens.filter(
Expand All @@ -36,18 +38,26 @@

async function deleteTable() {
const wasSelectedTable = $tables.selected
await tables.delete(table)
store.actions.screens.delete(templateScreens)
await tables.fetch()
notifications.success("Table deleted")
if (table.type === "external") {
await datasources.fetch()
}
if (wasSelectedTable && wasSelectedTable._id === table._id) {
$goto("./table")
try {
await tables.delete(table)
await store.actions.screens.delete(templateScreens)
await tables.fetch()
if (table.type === "external") {
await datasources.fetch()
}
notifications.success("Table deleted")
if (wasSelectedTable && wasSelectedTable._id === table._id) {
$goto("./table")
}
} catch (err) {
notifications.error(err)
}
}

function hideDeleteDialog() {
deleteTableName = ""
}

async function save() {
await tables.save(table)
notifications.success("Table renamed successfully")
Expand All @@ -67,7 +77,9 @@
<Icon s hoverable name="MoreSmallList" />
</div>
<MenuItem icon="Edit" on:click={editorModal.show}>Edit</MenuItem>
<MenuItem icon="Delete" on:click={showDeleteModal}>Delete</MenuItem>
{#if allowDeletion}
<MenuItem icon="Delete" on:click={showDeleteModal}>Delete</MenuItem>
{/if}
</ActionMenu>

<Modal bind:this={editorModal}>
Expand All @@ -90,19 +102,31 @@
bind:this={confirmDeleteDialog}
okText="Delete Table"
onOk={deleteTable}
onCancel={hideDeleteDialog}
title="Confirm Deletion"
disabled={deleteTableName !== table.name}
>
Are you sure you wish to delete the table
<i>{table.name}?</i>
The following will also be deleted:
<p>
Are you sure you wish to delete the table
<b>{table.name}?</b>
The following will also be deleted:
</p>
<b>
<div class="delete-items">
{#each willBeDeleted as item}
<div>{item}</div>
{/each}
</div>
</b>
This action cannot be undone.
<p>
This action cannot be undone - to continue please enter the table name below
to confirm.
</p>
<Input
bind:value={deleteTableName}
placeholder={table.name}
dataCy="delete-table-confirm"
/>
</ConfirmDialog>

<style>
Expand Down
6 changes: 5 additions & 1 deletion packages/builder/src/pages/builder/portal/apps/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@
Are you sure you want to delete the app <b>{selectedApp?.name}</b>?

<p>Please enter the app name below to confirm.</p>
<Input bind:value={appName} data-cy="delete-app-confirmation" />
<Input
bind:value={appName}
data-cy="delete-app-confirmation"
placeholder={selectedApp?.name}
/>
</ConfirmDialog>
<ConfirmDialog
bind:this={unpublishModal}
Expand Down
11 changes: 8 additions & 3 deletions packages/builder/src/stores/backend/tables.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable, get } from "svelte/store"
import { views, queries, datasources } from "./"
import { get, writable } from "svelte/store"
import { datasources, queries, views } from "./"
import { cloneDeep } from "lodash/fp"
import api from "builderStore/api"
import { SWITCHABLE_TYPES } from "../../constants/backend"
Expand Down Expand Up @@ -97,7 +97,12 @@ export function createTablesStore() {
})
},
delete: async table => {
await api.delete(`/api/tables/${table._id}/${table._rev}`)
const response = await api.delete(
`/api/tables/${table._id}/${table._rev}`
)
if (response.status !== 200) {
throw (await response.json()).message
}
update(state => ({
...state,
list: state.list.filter(existing => existing._id !== table._id),
Expand Down
1 change: 1 addition & 0 deletions packages/server/scripts/dev/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function init() {
SELF_HOSTED: 1,
DISABLE_ACCOUNT_PORTAL: "",
MULTI_TENANCY: "",
DISABLE_THREADING: 1,
}
let envFile = ""
Object.keys(envFileJson).forEach(key => {
Expand Down
9 changes: 8 additions & 1 deletion packages/server/src/api/controllers/table/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ function isRelationshipSetup(column) {
exports.save = async function (ctx) {
const appId = ctx.appId
const table = ctx.request.body
// can't do this
// can't do this right now
delete table.dataImport
const datasourceId = getDatasourceId(ctx.request.body)
// table doesn't exist already, note that it is created
if (!table._id) {
table.created = true
}
let tableToSave = {
type: "table",
_id: buildExternalTableId(datasourceId, table.name),
Expand Down Expand Up @@ -265,6 +269,9 @@ exports.save = async function (ctx) {
exports.destroy = async function (ctx) {
const appId = ctx.appId
const tableToDelete = await getTable(appId, ctx.params.tableId)
if (!tableToDelete || !tableToDelete.created) {
ctx.throw(400, "Cannot delete tables which weren't created in Budibase.")
}
const datasourceId = getDatasourceId(tableToDelete)

const db = new CouchDB(appId)
Expand Down
15 changes: 15 additions & 0 deletions packages/server/src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if (!LOADED && isDev() && !isTest()) {
LOADED = true
}

let inThread = false

module.exports = {
// important
PORT: process.env.PORT,
Expand Down Expand Up @@ -62,6 +64,7 @@ module.exports = {
USERID_API_KEY: process.env.USERID_API_KEY,
DEPLOYMENT_CREDENTIALS_URL: process.env.DEPLOYMENT_CREDENTIALS_URL,
ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS,
DISABLE_THREADING: process.env.DISABLE_THREADING,
_set(key, value) {
process.env[key] = value
module.exports[key] = value
Expand All @@ -72,6 +75,18 @@ module.exports = {
isProd: () => {
return !isDev()
},
// used to check if already in a thread, don't thread further
setInThread: () => {
inThread = true
},
isInThread: () => {
return inThread
},
}

// threading can cause memory issues with node-ts in development
if (isDev() && module.exports.DISABLE_THREADING == null) {
module.exports._set("DISABLE_THREADING", "1")
}

// clean up any environment variable edge cases
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/threads/automation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// when thread starts, make sure it is recorded
const env = require("../environment")
env.setInThread()
const actions = require("../automations/actions")
const automationUtils = require("../automations/automationUtils")
const AutomationEmitter = require("../events/AutomationEmitter")
Expand All @@ -6,7 +9,6 @@ const { DEFAULT_TENANT_ID } = require("@budibase/auth").constants
const CouchDB = require("../db")
const { DocumentTypes, isDevAppID } = require("../db/utils")
const { doInTenant } = require("@budibase/auth/tenancy")
const env = require("../environment")
const usage = require("../utilities/usageQuota")
const { definitions: triggerDefs } = require("../automations/triggerInfo")

Expand Down
12 changes: 9 additions & 3 deletions packages/server/src/threads/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ function typeToFile(type) {
class Thread {
constructor(type, opts = { timeoutMs: null, count: 1 }) {
this.type = type
if (!env.isTest()) {
this.count = opts.count ? opts.count : 1
this.disableThreading =
env.isTest() ||
env.DISABLE_THREADING ||
this.count === 0 ||
env.isInThread()
if (!this.disableThreading) {
const workerOpts = {
autoStart: true,
maxConcurrentWorkers: opts.count ? opts.count : 1,
maxConcurrentWorkers: this.count,
}
if (opts.timeoutMs) {
workerOpts.maxCallTime = opts.timeoutMs
Expand All @@ -40,7 +46,7 @@ class Thread {
return new Promise((resolve, reject) => {
let fncToCall
// if in test then don't use threading
if (env.isTest()) {
if (this.disableThreading) {
fncToCall = require(typeToFile(this.type))
} else {
fncToCall = this.workers
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/threads/query.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// when thread starts, make sure it is recorded
const env = require("../environment")
env.setInThread()
const ScriptRunner = require("../utilities/scriptRunner")
const { integrations } = require("../integrations")

Expand Down