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 {
AutoReason,
Datasource,
FieldSchema,
FieldType,
@ -24,7 +25,7 @@ import {
isSQL,
} from "../../../integrations/utils"
import { getDatasourceAndQuery } from "../../../sdk/app/rows/utils"
import { FieldTypes } from "../../../constants"
import { AutoFieldSubTypes, FieldTypes } from "../../../constants"
import { processObjectSync } from "@budibase/string-templates"
import { cloneDeep } from "lodash/fp"
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 {
private operation: Operation
private tableId: string
@ -295,11 +305,7 @@ export class ExternalRequest {
manyRelationships: ManyRelationship[] = []
for (let [key, field] of Object.entries(table.schema)) {
// if set already, or not set just skip it
if (
row[key] == null ||
newRow[key] ||
!sdk.tables.isEditableColumn(field)
) {
if (row[key] == null || newRow[key] || !isEditableColumn(field)) {
continue
}
// 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 * as utils from "./utils"
import { dataFilters } from "@budibase/shared-core"
import { inputProcessing } from "../../../utilities/rowProcessor"
import { cloneDeep, isEqual } from "lodash"
export async function handleRequest(
operation: Operation,
@ -88,10 +90,24 @@ export async function save(ctx: UserCtx) {
if (!validateResult.valid) {
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, {
row: inputs,
row,
})
const responseRow = response as { row: Row }
if (!isEqual(table, updatedTable)) {
await sdk.tables.saveTable(updatedTable)
}
const rowId = responseRow.row._id
if (rowId) {
const row = await sdk.rows.external.getRow(tableId, rowId, {

View File

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

View File

@ -12,7 +12,7 @@ import {
TableViewsResponse,
} from "@budibase/types"
import datasources from "../datasources"
import { isEditableColumn, populateExternalTableSchemas } from "./validation"
import { populateExternalTableSchemas } from "./validation"
import sdk from "../../../sdk"
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 {
getAllInternalTables,
getAllExternalTables,
getExternalTable,
getTable,
populateExternalTableSchemas,
isEditableColumn,
enrichViewSchemas,
saveTable,
}

View File

@ -55,13 +55,6 @@ function checkForeignKeysAreAutoColumns(datasource: 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) {
return checkForeignKeysAreAutoColumns(datasource)
}