Fix autocolumns

This commit is contained in:
Adria Navarro 2023-09-08 17:42:54 +02:00
parent 2f5aadec4b
commit 4e69e51cca
5 changed files with 44 additions and 18 deletions

View File

@ -1,4 +1,5 @@
import { import {
AutoReason,
Datasource, Datasource,
FieldSchema, FieldSchema,
FieldType, FieldType,
@ -24,7 +25,7 @@ import {
isSQL, isSQL,
} from "../../../integrations/utils" } from "../../../integrations/utils"
import { getDatasourceAndQuery } from "../../../sdk/app/rows/utils" import { getDatasourceAndQuery } from "../../../sdk/app/rows/utils"
import { FieldTypes } from "../../../constants" import { AutoFieldSubTypes, FieldTypes } from "../../../constants"
import { processObjectSync } from "@budibase/string-templates" import { processObjectSync } from "@budibase/string-templates"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { processDates, processFormulas } from "../../../utilities/rowProcessor" import { processDates, processFormulas } from "../../../utilities/rowProcessor"
@ -259,6 +260,15 @@ function isOneSide(field: FieldSchema) {
) )
} }
function isEditableColumn(column: FieldSchema) {
const isExternalAutoColumn =
column.autocolumn &&
column.autoReason !== AutoReason.FOREIGN_KEY &&
column.subtype !== AutoFieldSubTypes.AUTO_ID
const isFormula = column.type === FieldTypes.FORMULA
return !(isExternalAutoColumn || isFormula)
}
export class ExternalRequest { export class ExternalRequest {
private operation: Operation private operation: Operation
private tableId: string private tableId: string
@ -295,11 +305,7 @@ export class ExternalRequest {
manyRelationships: ManyRelationship[] = [] manyRelationships: ManyRelationship[] = []
for (let [key, field] of Object.entries(table.schema)) { for (let [key, field] of Object.entries(table.schema)) {
// if set already, or not set just skip it // if set already, or not set just skip it
if ( if (row[key] == null || newRow[key] || !isEditableColumn(field)) {
row[key] == null ||
newRow[key] ||
!sdk.tables.isEditableColumn(field)
) {
continue continue
} }
// if its an empty string then it means return the column to null (if possible) // if its an empty string then it means return the column to null (if possible)

View File

@ -18,6 +18,8 @@ import {
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "./utils" import * as utils from "./utils"
import { dataFilters } from "@budibase/shared-core" import { dataFilters } from "@budibase/shared-core"
import { inputProcessing } from "../../../utilities/rowProcessor"
import { cloneDeep, isEqual } from "lodash"
export async function handleRequest( export async function handleRequest(
operation: Operation, operation: Operation,
@ -88,10 +90,24 @@ export async function save(ctx: UserCtx) {
if (!validateResult.valid) { if (!validateResult.valid) {
throw { validation: validateResult.errors } throw { validation: validateResult.errors }
} }
const table = await sdk.tables.getTable(tableId)
const { table: updatedTable, row } = inputProcessing(
ctx.user,
cloneDeep(table),
inputs
)
const response = await handleRequest(Operation.CREATE, tableId, { const response = await handleRequest(Operation.CREATE, tableId, {
row: inputs, row,
}) })
const responseRow = response as { row: Row } const responseRow = response as { row: Row }
if (!isEqual(table, updatedTable)) {
await sdk.tables.saveTable(updatedTable)
}
const rowId = responseRow.row._id const rowId = responseRow.row._id
if (rowId) { if (rowId) {
const row = await sdk.rows.external.getRow(tableId, rowId, { const row = await sdk.rows.external.getRow(tableId, rowId, {

View File

@ -177,8 +177,8 @@ describe.each([
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const newTable = await config.createTable({ const newTable = await config.createTable({
...table,
name: "TestTableAuto", name: "TestTableAuto",
type: "table",
schema: { schema: {
...table.schema, ...table.schema,
"Row ID": { "Row ID": {
@ -189,7 +189,7 @@ describe.each([
autocolumn: true, autocolumn: true,
constraints: { constraints: {
type: "number", type: "number",
presence: false, presence: true,
numericality: { numericality: {
greaterThanOrEqualTo: "", greaterThanOrEqualTo: "",
lessThanOrEqualTo: "", lessThanOrEqualTo: "",

View File

@ -12,7 +12,7 @@ import {
TableViewsResponse, TableViewsResponse,
} from "@budibase/types" } from "@budibase/types"
import datasources from "../datasources" import datasources from "../datasources"
import { isEditableColumn, populateExternalTableSchemas } from "./validation" import { populateExternalTableSchemas } from "./validation"
import sdk from "../../../sdk" import sdk from "../../../sdk"
async function getAllInternalTables(db?: Database): Promise<Table[]> { async function getAllInternalTables(db?: Database): Promise<Table[]> {
@ -73,12 +73,23 @@ function enrichViewSchemas(table: Table): TableResponse {
} }
} }
async function saveTable(table: Table) {
const db = context.getAppDB()
if (isExternalTable(table._id!)) {
const datasource = await sdk.datasources.get(table.sourceId!)
datasource.entities![table.name] = table
await db.put(datasource)
} else {
await db.put(table)
}
}
export default { export default {
getAllInternalTables, getAllInternalTables,
getAllExternalTables, getAllExternalTables,
getExternalTable, getExternalTable,
getTable, getTable,
populateExternalTableSchemas, populateExternalTableSchemas,
isEditableColumn,
enrichViewSchemas, enrichViewSchemas,
saveTable,
} }

View File

@ -55,13 +55,6 @@ function checkForeignKeysAreAutoColumns(datasource: Datasource) {
return datasource return datasource
} }
export function isEditableColumn(column: FieldSchema) {
const isAutoColumn =
column.autocolumn && column.autoReason !== AutoReason.FOREIGN_KEY
const isFormula = column.type === FieldTypes.FORMULA
return !(isAutoColumn || isFormula)
}
export function populateExternalTableSchemas(datasource: Datasource) { export function populateExternalTableSchemas(datasource: Datasource) {
return checkForeignKeysAreAutoColumns(datasource) return checkForeignKeysAreAutoColumns(datasource)
} }