@budibase/server 1.0.121 → 1.0.123

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/builder/assets/{index.be685d3b.js → index.8d4a5b33.js} +1 -1
  2. package/builder/index.html +1 -1
  3. package/coverage/clover.xml +246 -213
  4. package/coverage/coverage-final.json +5 -5
  5. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  6. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  7. package/coverage/lcov-report/api/controllers/application.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/automation.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/datasource.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/index.html +1 -1
  16. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  31. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  33. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/table/index.html +19 -19
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/table/internal.js.html +83 -11
  40. package/coverage/lcov-report/api/controllers/table/utils.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  46. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  47. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  49. package/coverage/lcov-report/api/index.html +1 -1
  50. package/coverage/lcov-report/api/index.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/index.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  80. package/coverage/lcov-report/api/routes/utils/validators.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  83. package/coverage/lcov-report/automations/actions.js.html +1 -1
  84. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  85. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  86. package/coverage/lcov-report/automations/index.html +1 -1
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/index.html +1 -1
  97. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  99. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  101. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  104. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  105. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  107. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  108. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  110. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  111. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  112. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  113. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  114. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  115. package/coverage/lcov-report/automations/utils.js.html +1 -1
  116. package/coverage/lcov-report/constants/index.html +5 -5
  117. package/coverage/lcov-report/constants/index.js.html +28 -4
  118. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  119. package/coverage/lcov-report/constants/screens.js.html +1 -1
  120. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  121. package/coverage/lcov-report/db/index.html +1 -1
  122. package/coverage/lcov-report/db/index.js.html +1 -1
  123. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +9 -6
  124. package/coverage/lcov-report/db/linkedRows/index.html +10 -10
  125. package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
  126. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  127. package/coverage/lcov-report/db/newid.js.html +1 -1
  128. package/coverage/lcov-report/db/utils.js.html +1 -1
  129. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  130. package/coverage/lcov-report/events/index.html +1 -1
  131. package/coverage/lcov-report/events/index.js.html +1 -1
  132. package/coverage/lcov-report/events/utils.js.html +1 -1
  133. package/coverage/lcov-report/index.html +34 -34
  134. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  135. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  136. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  137. package/coverage/lcov-report/middleware/authorized.js.html +1 -1
  138. package/coverage/lcov-report/middleware/builder.js.html +1 -1
  139. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  140. package/coverage/lcov-report/middleware/index.html +1 -1
  141. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  142. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  143. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  144. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  145. package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
  146. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  147. package/coverage/lcov-report/threads/automation.js.html +1 -1
  148. package/coverage/lcov-report/threads/index.html +1 -1
  149. package/coverage/lcov-report/threads/index.js.html +1 -1
  150. package/coverage/lcov-report/threads/query.js.html +1 -1
  151. package/coverage/lcov-report/threads/utils.js.html +1 -1
  152. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  153. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  154. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  155. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  156. package/coverage/lcov-report/utilities/global.js.html +1 -1
  157. package/coverage/lcov-report/utilities/index.html +1 -1
  158. package/coverage/lcov-report/utilities/index.js.html +1 -1
  159. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  160. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  161. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  162. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  163. package/coverage/lcov-report/utilities/rowProcessor/index.html +25 -25
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +31 -22
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +94 -10
  166. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  167. package/coverage/lcov-report/utilities/security.js.html +1 -1
  168. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  169. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +1 -1
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  172. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  173. package/coverage/lcov-report/utilities/users.js.html +1 -1
  174. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  175. package/coverage/lcov.info +457 -392
  176. package/dist/api/controllers/table/internal.js +22 -1
  177. package/dist/constants/index.js +7 -0
  178. package/dist/db/linkedRows/LinkController.js +1 -0
  179. package/dist/package.json +4 -4
  180. package/dist/tsconfig.tsbuildinfo +1 -1
  181. package/dist/utilities/rowProcessor/index.js +11 -6
  182. package/dist/utilities/rowProcessor/utils.js +27 -1
  183. package/package.json +5 -5
  184. package/src/api/controllers/table/internal.js +25 -1
  185. package/src/api/routes/tests/routing.spec.js +0 -1
  186. package/src/constants/index.js +8 -0
  187. package/src/db/linkedRows/LinkController.js +1 -0
  188. package/src/utilities/rowProcessor/index.js +12 -9
  189. package/src/utilities/rowProcessor/utils.js +29 -1
@@ -12,11 +12,12 @@ const linkRows = require("../../db/linkedRows");
12
12
  const { cloneDeep } = require("lodash/fp");
13
13
  const { FieldTypes, AutoFieldSubTypes } = require("../../constants");
14
14
  const { attachmentsRelativeURL } = require("../index");
15
- const { processFormulas } = require("./utils");
15
+ const { processFormulas, fixAutoColumnSubType } = require("./utils");
16
16
  const { deleteFiles } = require("../../utilities/fileSystem/utilities");
17
17
  const { ObjectStoreBuckets } = require("../../constants");
18
18
  const { isProdAppID, getProdAppID, dbExists, } = require("@budibase/backend-core/db");
19
19
  const { getAppId } = require("@budibase/backend-core/context");
20
+ const { InternalTables } = require("../../db/utils");
20
21
  const BASE_AUTO_ID = 1;
21
22
  /**
22
23
  * A map of how we convert various properties in rows to each other based on the row type.
@@ -135,19 +136,22 @@ function getRemovedAttachmentKeys(oldRow, row, attachmentKey) {
135
136
  */
136
137
  function processAutoColumn(user, table, row, opts = { reprocessing: false, noAutoRelationships: false }) {
137
138
  let noUser = !user || !user.userId;
139
+ let isUserTable = table._id === InternalTables.USER_METADATA;
138
140
  let now = new Date().toISOString();
139
141
  // if a row doesn't have a revision then it doesn't exist yet
140
142
  const creating = !row._rev;
143
+ // check its not user table, or whether any of the processing options have been disabled
144
+ const shouldUpdateUserFields = !isUserTable && !opts.reprocessing && !opts.noAutoRelationships && !noUser;
141
145
  for (let [key, schema] of Object.entries(table.schema)) {
142
146
  if (!schema.autocolumn) {
143
147
  continue;
144
148
  }
149
+ if (!schema.subtype) {
150
+ schema = fixAutoColumnSubType(schema);
151
+ }
145
152
  switch (schema.subtype) {
146
153
  case AutoFieldSubTypes.CREATED_BY:
147
- if (creating &&
148
- !opts.reprocessing &&
149
- !opts.noAutoRelationships &&
150
- !noUser) {
154
+ if (creating && shouldUpdateUserFields) {
151
155
  row[key] = [user.userId];
152
156
  }
153
157
  break;
@@ -157,7 +161,7 @@ function processAutoColumn(user, table, row, opts = { reprocessing: false, noAut
157
161
  }
158
162
  break;
159
163
  case AutoFieldSubTypes.UPDATED_BY:
160
- if (!opts.reprocessing && !opts.noAutoRelationships && !noUser) {
164
+ if (shouldUpdateUserFields) {
161
165
  row[key] = [user.userId];
162
166
  }
163
167
  break;
@@ -175,6 +179,7 @@ function processAutoColumn(user, table, row, opts = { reprocessing: false, noAut
175
179
  return { table, row };
176
180
  }
177
181
  exports.processAutoColumn = processAutoColumn;
182
+ exports.fixAutoColumnSubType = fixAutoColumnSubType;
178
183
  exports.processFormulas = processFormulas;
179
184
  /**
180
185
  * This will coerce a value to the correct types based on the type transform map
@@ -1,6 +1,32 @@
1
1
  "use strict";
2
- const { FieldTypes, FormulaTypes } = require("../../constants");
2
+ const { FieldTypes, FormulaTypes, AutoFieldDefaultNames, AutoFieldSubTypes, } = require("../../constants");
3
3
  const { processStringSync } = require("@budibase/string-templates");
4
+ /**
5
+ * If the subtype has been lost for any reason this works out what
6
+ * subtype the auto column should be.
7
+ */
8
+ exports.fixAutoColumnSubType = column => {
9
+ if (!column.autocolumn || !column.name || column.subtype) {
10
+ return column;
11
+ }
12
+ // the columns which get auto generated
13
+ if (column.name.endsWith(AutoFieldDefaultNames.CREATED_BY)) {
14
+ column.subtype = AutoFieldSubTypes.CREATED_BY;
15
+ }
16
+ else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_BY)) {
17
+ column.subtype = AutoFieldSubTypes.UPDATED_BY;
18
+ }
19
+ else if (column.name.endsWith(AutoFieldDefaultNames.CREATED_AT)) {
20
+ column.subtype = AutoFieldSubTypes.CREATED_AT;
21
+ }
22
+ else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_AT)) {
23
+ column.subtype = AutoFieldSubTypes.UPDATED_AT;
24
+ }
25
+ else if (column.name.endsWith(AutoFieldDefaultNames.AUTO_ID)) {
26
+ column.subtype = AutoFieldSubTypes.AUTO_ID;
27
+ }
28
+ return column;
29
+ };
4
30
  /**
5
31
  * Looks through the rows provided and finds formulas - which it then processes.
6
32
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.121",
4
+ "version": "1.0.123",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -68,9 +68,9 @@
68
68
  "license": "GPL-3.0",
69
69
  "dependencies": {
70
70
  "@apidevtools/swagger-parser": "^10.0.3",
71
- "@budibase/backend-core": "^1.0.121",
72
- "@budibase/client": "^1.0.121",
73
- "@budibase/string-templates": "^1.0.121",
71
+ "@budibase/backend-core": "^1.0.123",
72
+ "@budibase/client": "^1.0.123",
73
+ "@budibase/string-templates": "^1.0.123",
74
74
  "@bull-board/api": "^3.7.0",
75
75
  "@bull-board/koa": "^3.7.0",
76
76
  "@elastic/elasticsearch": "7.10.0",
@@ -174,5 +174,5 @@
174
174
  "optionalDependencies": {
175
175
  "oracledb": "^5.3.0"
176
176
  },
177
- "gitHead": "6f3769181fbcfdffd1928266fa08871779c7a694"
177
+ "gitHead": "db99c31b53d0dbbb788335126e45d1cc2f39fbf0"
178
178
  }
@@ -10,9 +10,30 @@ const {
10
10
  const usageQuota = require("../../../utilities/usageQuota")
11
11
  const { getAppDB } = require("@budibase/backend-core/context")
12
12
  const env = require("../../../environment")
13
- const { cleanupAttachments } = require("../../../utilities/rowProcessor")
13
+ const {
14
+ cleanupAttachments,
15
+ fixAutoColumnSubType,
16
+ } = require("../../../utilities/rowProcessor")
14
17
  const { runStaticFormulaChecks } = require("./bulkFormula")
15
18
 
19
+ function checkAutoColumns(table, oldTable) {
20
+ if (!table.schema) {
21
+ return table
22
+ }
23
+ for (let [key, schema] of Object.entries(table.schema)) {
24
+ if (!schema.autocolumn || schema.subtype) {
25
+ continue
26
+ }
27
+ const oldSchema = oldTable && oldTable.schema[key]
28
+ if (oldSchema && oldSchema.subtype) {
29
+ table.schema[key].subtype = oldSchema.subtype
30
+ } else {
31
+ table.schema[key] = fixAutoColumnSubType(schema)
32
+ }
33
+ }
34
+ return table
35
+ }
36
+
16
37
  exports.save = async function (ctx) {
17
38
  const db = getAppDB()
18
39
  const { dataImport, ...rest } = ctx.request.body
@@ -29,9 +50,12 @@ exports.save = async function (ctx) {
29
50
  oldTable = await db.get(ctx.request.body._id)
30
51
  }
31
52
 
53
+ // check all types are correct
32
54
  if (hasTypeChanged(tableToSave, oldTable)) {
33
55
  ctx.throw(400, "A column type has changed.")
34
56
  }
57
+ // check that subtypes have been maintained
58
+ tableToSave = checkAutoColumns(tableToSave, oldTable)
35
59
 
36
60
  // saving a table is a complex operation, involving many different steps, this
37
61
  // has been broken out into a utility to make it more obvious/easier to manipulate
@@ -2,7 +2,6 @@ const setup = require("./utilities")
2
2
  const { basicScreen } = setup.structures
3
3
  const { checkBuilderEndpoint, runInProd } = require("./utilities/TestFunctions")
4
4
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
5
- const { doInAppContext } = require("@budibase/backend-core/context")
6
5
 
7
6
  const route = "/test"
8
7
 
@@ -162,6 +162,14 @@ exports.AutoFieldSubTypes = {
162
162
  AUTO_ID: "autoID",
163
163
  }
164
164
 
165
+ exports.AutoFieldDefaultNames = {
166
+ CREATED_BY: "Created By",
167
+ CREATED_AT: "Created At",
168
+ UPDATED_BY: "Updated By",
169
+ UPDATED_AT: "Updated At",
170
+ AUTO_ID: "Auto ID",
171
+ }
172
+
165
173
  exports.OBJ_STORE_DIRECTORY = "/prod-budi-app-assets"
166
174
  exports.BaseQueryVerbs = {
167
175
  CREATE: "create",
@@ -376,6 +376,7 @@ class LinkController {
376
376
 
377
377
  if (field.autocolumn) {
378
378
  linkedField.autocolumn = field.autocolumn
379
+ linkedField.subtype = field.subtype
379
380
  }
380
381
 
381
382
  // check the linked table to make sure we aren't overwriting an existing column
@@ -2,7 +2,7 @@ const linkRows = require("../../db/linkedRows")
2
2
  const { cloneDeep } = require("lodash/fp")
3
3
  const { FieldTypes, AutoFieldSubTypes } = require("../../constants")
4
4
  const { attachmentsRelativeURL } = require("../index")
5
- const { processFormulas } = require("./utils")
5
+ const { processFormulas, fixAutoColumnSubType } = require("./utils")
6
6
  const { deleteFiles } = require("../../utilities/fileSystem/utilities")
7
7
  const { ObjectStoreBuckets } = require("../../constants")
8
8
  const {
@@ -11,6 +11,7 @@ const {
11
11
  dbExists,
12
12
  } = require("@budibase/backend-core/db")
13
13
  const { getAppId } = require("@budibase/backend-core/context")
14
+ const { InternalTables } = require("../../db/utils")
14
15
 
15
16
  const BASE_AUTO_ID = 1
16
17
 
@@ -137,21 +138,23 @@ function processAutoColumn(
137
138
  opts = { reprocessing: false, noAutoRelationships: false }
138
139
  ) {
139
140
  let noUser = !user || !user.userId
141
+ let isUserTable = table._id === InternalTables.USER_METADATA
140
142
  let now = new Date().toISOString()
141
143
  // if a row doesn't have a revision then it doesn't exist yet
142
144
  const creating = !row._rev
145
+ // check its not user table, or whether any of the processing options have been disabled
146
+ const shouldUpdateUserFields =
147
+ !isUserTable && !opts.reprocessing && !opts.noAutoRelationships && !noUser
143
148
  for (let [key, schema] of Object.entries(table.schema)) {
144
149
  if (!schema.autocolumn) {
145
150
  continue
146
151
  }
152
+ if (!schema.subtype) {
153
+ schema = fixAutoColumnSubType(schema)
154
+ }
147
155
  switch (schema.subtype) {
148
156
  case AutoFieldSubTypes.CREATED_BY:
149
- if (
150
- creating &&
151
- !opts.reprocessing &&
152
- !opts.noAutoRelationships &&
153
- !noUser
154
- ) {
157
+ if (creating && shouldUpdateUserFields) {
155
158
  row[key] = [user.userId]
156
159
  }
157
160
  break
@@ -161,7 +164,7 @@ function processAutoColumn(
161
164
  }
162
165
  break
163
166
  case AutoFieldSubTypes.UPDATED_BY:
164
- if (!opts.reprocessing && !opts.noAutoRelationships && !noUser) {
167
+ if (shouldUpdateUserFields) {
165
168
  row[key] = [user.userId]
166
169
  }
167
170
  break
@@ -179,7 +182,7 @@ function processAutoColumn(
179
182
  return { table, row }
180
183
  }
181
184
  exports.processAutoColumn = processAutoColumn
182
-
185
+ exports.fixAutoColumnSubType = fixAutoColumnSubType
183
186
  exports.processFormulas = processFormulas
184
187
 
185
188
  /**
@@ -1,6 +1,34 @@
1
- const { FieldTypes, FormulaTypes } = require("../../constants")
1
+ const {
2
+ FieldTypes,
3
+ FormulaTypes,
4
+ AutoFieldDefaultNames,
5
+ AutoFieldSubTypes,
6
+ } = require("../../constants")
2
7
  const { processStringSync } = require("@budibase/string-templates")
3
8
 
9
+ /**
10
+ * If the subtype has been lost for any reason this works out what
11
+ * subtype the auto column should be.
12
+ */
13
+ exports.fixAutoColumnSubType = column => {
14
+ if (!column.autocolumn || !column.name || column.subtype) {
15
+ return column
16
+ }
17
+ // the columns which get auto generated
18
+ if (column.name.endsWith(AutoFieldDefaultNames.CREATED_BY)) {
19
+ column.subtype = AutoFieldSubTypes.CREATED_BY
20
+ } else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_BY)) {
21
+ column.subtype = AutoFieldSubTypes.UPDATED_BY
22
+ } else if (column.name.endsWith(AutoFieldDefaultNames.CREATED_AT)) {
23
+ column.subtype = AutoFieldSubTypes.CREATED_AT
24
+ } else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_AT)) {
25
+ column.subtype = AutoFieldSubTypes.UPDATED_AT
26
+ } else if (column.name.endsWith(AutoFieldDefaultNames.AUTO_ID)) {
27
+ column.subtype = AutoFieldSubTypes.AUTO_ID
28
+ }
29
+ return column
30
+ }
31
+
4
32
  /**
5
33
  * Looks through the rows provided and finds formulas - which it then processes.
6
34
  */