From 6880e7bde9a293b81a1178635b7529f7d0ad44e4 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 26 Apr 2022 14:24:51 +0100 Subject: [PATCH 1/3] Fix for #5269 - the sub type was being lost in some scenarios related to the user table being updated - making sure it is consistently kept when dealing with either of the related tables. --- .../src/api/controllers/table/internal.js | 26 +++++++++++++++- packages/server/src/constants/index.js | 8 +++++ .../src/db/linkedRows/LinkController.js | 1 + .../src/utilities/rowProcessor/index.js | 21 +++++++------ .../src/utilities/rowProcessor/utils.js | 30 ++++++++++++++++++- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/table/internal.js b/packages/server/src/api/controllers/table/internal.js index 476e7a52af..412f644059 100644 --- a/packages/server/src/api/controllers/table/internal.js +++ b/packages/server/src/api/controllers/table/internal.js @@ -10,9 +10,30 @@ const { const usageQuota = require("../../../utilities/usageQuota") const { getAppDB } = require("@budibase/backend-core/context") const env = require("../../../environment") -const { cleanupAttachments } = require("../../../utilities/rowProcessor") +const { + cleanupAttachments, + fixAutoColumnSubType, +} = require("../../../utilities/rowProcessor") const { runStaticFormulaChecks } = require("./bulkFormula") +function checkAutoColumns(table, oldTable) { + if (!table.schema) { + return table + } + for (let [key, schema] of Object.entries(table.schema)) { + if (!schema.autocolumn || schema.subtype) { + continue + } + const oldSchema = oldTable.schema[key] + if (oldSchema && oldSchema.subtype) { + table.schema[key].subtype = oldSchema.subtype + } else { + table.schema[key] = fixAutoColumnSubType(schema) + } + } + return table +} + exports.save = async function (ctx) { const db = getAppDB() const { dataImport, ...rest } = ctx.request.body @@ -29,9 +50,12 @@ exports.save = async function (ctx) { oldTable = await db.get(ctx.request.body._id) } + // check all types are correct if (hasTypeChanged(tableToSave, oldTable)) { ctx.throw(400, "A column type has changed.") } + // check that subtypes have been maintained + tableToSave = checkAutoColumns(tableToSave, oldTable) // saving a table is a complex operation, involving many different steps, this // has been broken out into a utility to make it more obvious/easier to manipulate diff --git a/packages/server/src/constants/index.js b/packages/server/src/constants/index.js index cf89f1fe99..c08621cb8a 100644 --- a/packages/server/src/constants/index.js +++ b/packages/server/src/constants/index.js @@ -162,6 +162,14 @@ exports.AutoFieldSubTypes = { AUTO_ID: "autoID", } +exports.AutoFieldDefaultNames = { + CREATED_BY: "Created By", + CREATED_AT: "Created At", + UPDATED_BY: "Updated By", + UPDATED_AT: "Updated At", + AUTO_ID: "Auto ID", +} + exports.OBJ_STORE_DIRECTORY = "/prod-budi-app-assets" exports.BaseQueryVerbs = { CREATE: "create", diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 86c32bf94f..32782df162 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -376,6 +376,7 @@ class LinkController { if (field.autocolumn) { linkedField.autocolumn = field.autocolumn + linkedField.subtype = field.subtype } // check the linked table to make sure we aren't overwriting an existing column diff --git a/packages/server/src/utilities/rowProcessor/index.js b/packages/server/src/utilities/rowProcessor/index.js index 18e0b14de6..36a02eb9b1 100644 --- a/packages/server/src/utilities/rowProcessor/index.js +++ b/packages/server/src/utilities/rowProcessor/index.js @@ -2,7 +2,7 @@ const linkRows = require("../../db/linkedRows") const { cloneDeep } = require("lodash/fp") const { FieldTypes, AutoFieldSubTypes } = require("../../constants") const { attachmentsRelativeURL } = require("../index") -const { processFormulas } = require("./utils") +const { processFormulas, fixAutoColumnSubType } = require("./utils") const { deleteFiles } = require("../../utilities/fileSystem/utilities") const { ObjectStoreBuckets } = require("../../constants") const { @@ -11,6 +11,7 @@ const { dbExists, } = require("@budibase/backend-core/db") const { getAppId } = require("@budibase/backend-core/context") +const { InternalTables } = require("../../db/utils") const BASE_AUTO_ID = 1 @@ -137,21 +138,23 @@ function processAutoColumn( opts = { reprocessing: false, noAutoRelationships: false } ) { let noUser = !user || !user.userId + let isUserTable = table._id === InternalTables.USER_METADATA let now = new Date().toISOString() // if a row doesn't have a revision then it doesn't exist yet const creating = !row._rev + // check its not user table, or whether any of the processing options have been disabled + const shouldUpdateUserFields = + !isUserTable && !opts.reprocessing && !opts.noAutoRelationships && !noUser for (let [key, schema] of Object.entries(table.schema)) { if (!schema.autocolumn) { continue } + if (!schema.subtype) { + schema = fixAutoColumnSubType(schema) + } switch (schema.subtype) { case AutoFieldSubTypes.CREATED_BY: - if ( - creating && - !opts.reprocessing && - !opts.noAutoRelationships && - !noUser - ) { + if (creating && shouldUpdateUserFields) { row[key] = [user.userId] } break @@ -161,7 +164,7 @@ function processAutoColumn( } break case AutoFieldSubTypes.UPDATED_BY: - if (!opts.reprocessing && !opts.noAutoRelationships && !noUser) { + if (shouldUpdateUserFields) { row[key] = [user.userId] } break @@ -179,7 +182,7 @@ function processAutoColumn( return { table, row } } exports.processAutoColumn = processAutoColumn - +exports.fixAutoColumnSubType = fixAutoColumnSubType exports.processFormulas = processFormulas /** diff --git a/packages/server/src/utilities/rowProcessor/utils.js b/packages/server/src/utilities/rowProcessor/utils.js index 95b7828084..262ef40a3a 100644 --- a/packages/server/src/utilities/rowProcessor/utils.js +++ b/packages/server/src/utilities/rowProcessor/utils.js @@ -1,6 +1,34 @@ -const { FieldTypes, FormulaTypes } = require("../../constants") +const { + FieldTypes, + FormulaTypes, + AutoFieldDefaultNames, + AutoFieldSubTypes, +} = require("../../constants") const { processStringSync } = require("@budibase/string-templates") +/** + * If the subtype has been lost for any reason this works out what + * subtype the auto column should be. + */ +exports.fixAutoColumnSubType = column => { + if (!column.autocolumn || !column.name || column.subtype) { + return column + } + // the columns which get auto generated + if (column.name.endsWith(AutoFieldDefaultNames.CREATED_BY)) { + column.subtype = AutoFieldSubTypes.CREATED_BY + } else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_BY)) { + column.subtype = AutoFieldSubTypes.UPDATED_BY + } else if (column.name.endsWith(AutoFieldDefaultNames.CREATED_AT)) { + column.subtype = AutoFieldSubTypes.CREATED_AT + } else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_AT)) { + column.subtype = AutoFieldSubTypes.UPDATED_AT + } else if (column.name.endsWith(AutoFieldDefaultNames.AUTO_ID)) { + column.subtype = AutoFieldSubTypes.AUTO_ID + } + return column +} + /** * Looks through the rows provided and finds formulas - which it then processes. */ From bd72565e6f2c28471e1182d162208ce19402b9ce Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 26 Apr 2022 14:58:59 +0100 Subject: [PATCH 2/3] Fixing issue discovered by test case. --- packages/server/src/api/controllers/table/internal.js | 2 +- packages/server/src/api/routes/tests/routing.spec.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/table/internal.js b/packages/server/src/api/controllers/table/internal.js index 412f644059..7a43bdaf15 100644 --- a/packages/server/src/api/controllers/table/internal.js +++ b/packages/server/src/api/controllers/table/internal.js @@ -24,7 +24,7 @@ function checkAutoColumns(table, oldTable) { if (!schema.autocolumn || schema.subtype) { continue } - const oldSchema = oldTable.schema[key] + const oldSchema = oldTable && oldTable.schema[key] if (oldSchema && oldSchema.subtype) { table.schema[key].subtype = oldSchema.subtype } else { diff --git a/packages/server/src/api/routes/tests/routing.spec.js b/packages/server/src/api/routes/tests/routing.spec.js index d6d05c3322..1c3abf2457 100644 --- a/packages/server/src/api/routes/tests/routing.spec.js +++ b/packages/server/src/api/routes/tests/routing.spec.js @@ -2,7 +2,6 @@ const setup = require("./utilities") const { basicScreen } = setup.structures const { checkBuilderEndpoint, runInProd } = require("./utilities/TestFunctions") const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") -const { doInAppContext } = require("@budibase/backend-core/context") const route = "/test" From db99c31b53d0dbbb788335126e45d1cc2f39fbf0 Mon Sep 17 00:00:00 2001 From: Budibase Release Bot <> Date: Tue, 26 Apr 2022 17:11:18 +0000 Subject: [PATCH 3/3] v1.0.123 --- lerna.json | 2 +- packages/backend-core/package.json | 2 +- packages/bbui/package.json | 4 ++-- packages/builder/package.json | 10 +++++----- packages/cli/package.json | 2 +- packages/client/package.json | 8 ++++---- packages/frontend-core/package.json | 4 ++-- packages/server/package.json | 8 ++++---- packages/string-templates/package.json | 2 +- packages/worker/package.json | 6 +++--- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lerna.json b/lerna.json index 96440d2573..36e23d9ab3 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "1.0.122", + "version": "1.0.123", "npmClient": "yarn", "packages": [ "packages/*" diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index 322c81d402..117d74ed1d 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -1,6 +1,6 @@ { "name": "@budibase/backend-core", - "version": "1.0.122", + "version": "1.0.123", "description": "Budibase backend core libraries used in server and worker", "main": "src/index.js", "author": "Budibase", diff --git a/packages/bbui/package.json b/packages/bbui/package.json index 0a9a093dbb..c081e6bac3 100644 --- a/packages/bbui/package.json +++ b/packages/bbui/package.json @@ -1,7 +1,7 @@ { "name": "@budibase/bbui", "description": "A UI solution used in the different Budibase projects.", - "version": "1.0.122", + "version": "1.0.123", "license": "MPL-2.0", "svelte": "src/index.js", "module": "dist/bbui.es.js", @@ -38,7 +38,7 @@ ], "dependencies": { "@adobe/spectrum-css-workflow-icons": "^1.2.1", - "@budibase/string-templates": "^1.0.122", + "@budibase/string-templates": "^1.0.123", "@spectrum-css/actionbutton": "^1.0.1", "@spectrum-css/actiongroup": "^1.0.1", "@spectrum-css/avatar": "^3.0.2", diff --git a/packages/builder/package.json b/packages/builder/package.json index e13467a328..2c0a13a6d9 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -1,6 +1,6 @@ { "name": "@budibase/builder", - "version": "1.0.122", + "version": "1.0.123", "license": "GPL-3.0", "private": true, "scripts": { @@ -65,10 +65,10 @@ } }, "dependencies": { - "@budibase/bbui": "^1.0.122", - "@budibase/client": "^1.0.122", - "@budibase/frontend-core": "^1.0.122", - "@budibase/string-templates": "^1.0.122", + "@budibase/bbui": "^1.0.123", + "@budibase/client": "^1.0.123", + "@budibase/frontend-core": "^1.0.123", + "@budibase/string-templates": "^1.0.123", "@sentry/browser": "5.19.1", "@spectrum-css/page": "^3.0.1", "@spectrum-css/vars": "^3.0.1", diff --git a/packages/cli/package.json b/packages/cli/package.json index 056803a9fe..b564638f1a 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@budibase/cli", - "version": "1.0.122", + "version": "1.0.123", "description": "Budibase CLI, for developers, self hosting and migrations.", "main": "src/index.js", "bin": { diff --git a/packages/client/package.json b/packages/client/package.json index 251f53ea03..1720e53b1b 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,6 +1,6 @@ { "name": "@budibase/client", - "version": "1.0.122", + "version": "1.0.123", "license": "MPL-2.0", "module": "dist/budibase-client.js", "main": "dist/budibase-client.js", @@ -19,9 +19,9 @@ "dev:builder": "rollup -cw" }, "dependencies": { - "@budibase/bbui": "^1.0.122", - "@budibase/frontend-core": "^1.0.122", - "@budibase/string-templates": "^1.0.122", + "@budibase/bbui": "^1.0.123", + "@budibase/frontend-core": "^1.0.123", + "@budibase/string-templates": "^1.0.123", "@spectrum-css/button": "^3.0.3", "@spectrum-css/card": "^3.0.3", "@spectrum-css/divider": "^1.0.3", diff --git a/packages/frontend-core/package.json b/packages/frontend-core/package.json index 57e5f67f72..785d609462 100644 --- a/packages/frontend-core/package.json +++ b/packages/frontend-core/package.json @@ -1,12 +1,12 @@ { "name": "@budibase/frontend-core", - "version": "1.0.122", + "version": "1.0.123", "description": "Budibase frontend core libraries used in builder and client", "author": "Budibase", "license": "MPL-2.0", "svelte": "src/index.js", "dependencies": { - "@budibase/bbui": "^1.0.122", + "@budibase/bbui": "^1.0.123", "lodash": "^4.17.21", "svelte": "^3.46.2" } diff --git a/packages/server/package.json b/packages/server/package.json index 6e41a2506f..6f39cab8fb 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,7 +1,7 @@ { "name": "@budibase/server", "email": "hi@budibase.com", - "version": "1.0.122", + "version": "1.0.123", "description": "Budibase Web Server", "main": "src/index.ts", "repository": { @@ -68,9 +68,9 @@ "license": "GPL-3.0", "dependencies": { "@apidevtools/swagger-parser": "^10.0.3", - "@budibase/backend-core": "^1.0.122", - "@budibase/client": "^1.0.122", - "@budibase/string-templates": "^1.0.122", + "@budibase/backend-core": "^1.0.123", + "@budibase/client": "^1.0.123", + "@budibase/string-templates": "^1.0.123", "@bull-board/api": "^3.7.0", "@bull-board/koa": "^3.7.0", "@elastic/elasticsearch": "7.10.0", diff --git a/packages/string-templates/package.json b/packages/string-templates/package.json index 97ba8fe089..9ef70939cd 100644 --- a/packages/string-templates/package.json +++ b/packages/string-templates/package.json @@ -1,6 +1,6 @@ { "name": "@budibase/string-templates", - "version": "1.0.122", + "version": "1.0.123", "description": "Handlebars wrapper for Budibase templating.", "main": "src/index.cjs", "module": "dist/bundle.mjs", diff --git a/packages/worker/package.json b/packages/worker/package.json index f0c933d42d..46553bc40a 100644 --- a/packages/worker/package.json +++ b/packages/worker/package.json @@ -1,7 +1,7 @@ { "name": "@budibase/worker", "email": "hi@budibase.com", - "version": "1.0.122", + "version": "1.0.123", "description": "Budibase background service", "main": "src/index.ts", "repository": { @@ -31,8 +31,8 @@ "author": "Budibase", "license": "GPL-3.0", "dependencies": { - "@budibase/backend-core": "^1.0.122", - "@budibase/string-templates": "^1.0.122", + "@budibase/backend-core": "^1.0.123", + "@budibase/string-templates": "^1.0.123", "@koa/router": "^8.0.0", "@sentry/node": "^6.0.0", "@techpass/passport-openidconnect": "^0.3.0",