Disabling auto-column fieldtype and subtypes. This is not something that is currently supported through the UI, so this is not a breaking change, just making sure these type of scenarios don't actually occur as they don't work particularly well.
This commit is contained in:
parent
c99e8103e2
commit
0a96bb9684
|
@ -1,8 +1,8 @@
|
||||||
import { context, events } from "@budibase/backend-core"
|
import { context, events } from "@budibase/backend-core"
|
||||||
import {
|
import {
|
||||||
AutoFieldSubType,
|
AutoFieldSubType,
|
||||||
Datasource,
|
|
||||||
BBReferenceFieldSubType,
|
BBReferenceFieldSubType,
|
||||||
|
Datasource,
|
||||||
FieldType,
|
FieldType,
|
||||||
INTERNAL_TABLE_SOURCE_ID,
|
INTERNAL_TABLE_SOURCE_ID,
|
||||||
InternalTable,
|
InternalTable,
|
||||||
|
@ -214,6 +214,57 @@ describe.each([
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("external table validation", () => {
|
||||||
|
!isInternal &&
|
||||||
|
it("should error if column is of type auto", async () => {
|
||||||
|
const table = basicTable(datasource)
|
||||||
|
await config.api.table.save(
|
||||||
|
{
|
||||||
|
...table,
|
||||||
|
schema: {
|
||||||
|
...table.schema,
|
||||||
|
auto: {
|
||||||
|
name: "auto",
|
||||||
|
autocolumn: true,
|
||||||
|
type: FieldType.AUTO,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message: `Column "auto" has type "${FieldType.AUTO}" - this is not supported.`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
!isInternal &&
|
||||||
|
it("should error if column has auto subtype", async () => {
|
||||||
|
const table = basicTable(datasource)
|
||||||
|
await config.api.table.save(
|
||||||
|
{
|
||||||
|
...table,
|
||||||
|
schema: {
|
||||||
|
...table.schema,
|
||||||
|
auto: {
|
||||||
|
name: "auto",
|
||||||
|
autocolumn: true,
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
subtype: AutoFieldSubType.AUTO_ID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message: `Column "auto" has subtype "${AutoFieldSubType.AUTO_ID}" - this is not supported.`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it("should add a new column for an internal DB table", async () => {
|
it("should add a new column for an internal DB table", async () => {
|
||||||
const saveTableRequest: SaveTableRequest = {
|
const saveTableRequest: SaveTableRequest = {
|
||||||
...basicTable(),
|
...basicTable(),
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
Table,
|
Table,
|
||||||
TableRequest,
|
TableRequest,
|
||||||
ViewV2,
|
ViewV2,
|
||||||
|
AutoFieldSubType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
import { buildExternalTableId } from "../../../../integrations/utils"
|
import { buildExternalTableId } from "../../../../integrations/utils"
|
||||||
|
@ -29,6 +30,39 @@ import { populateExternalTableSchemas } from "../validation"
|
||||||
import datasourceSdk from "../../datasources"
|
import datasourceSdk from "../../datasources"
|
||||||
import * as viewSdk from "../../views"
|
import * as viewSdk from "../../views"
|
||||||
|
|
||||||
|
function noPrimaryKey(table: Table) {
|
||||||
|
return table.primary == null || table.primary.length === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate(table: Table, oldTable?: Table) {
|
||||||
|
if (!oldTable && table.schema.id && noPrimaryKey(table)) {
|
||||||
|
throw new Error(
|
||||||
|
"External tables with no `primary` column set will define an `id` column, but we found an `id` column in the supplied schema. Either set a `primary` column or remove the `id` column."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasTypeChanged(table, oldTable)) {
|
||||||
|
throw new Error("A column type has changed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
const autoSubTypes = Object.values(AutoFieldSubType)
|
||||||
|
// check for auto columns, they are not allowed
|
||||||
|
for (let [key, column] of Object.entries(table.schema)) {
|
||||||
|
// the auto-column type should never be used
|
||||||
|
if (column.type === FieldType.AUTO) {
|
||||||
|
throw new Error(
|
||||||
|
`Column "${key}" has type "${FieldType.AUTO}" - this is not supported.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (column.subtype && autoSubTypes.includes(column.subtype)) {
|
||||||
|
throw new Error(
|
||||||
|
`Column "${key}" has subtype "${column.subtype}" - this is not supported.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
datasourceId: string,
|
datasourceId: string,
|
||||||
update: Table,
|
update: Table,
|
||||||
|
@ -47,16 +81,10 @@ export async function save(
|
||||||
oldTable = await getTable(tableId)
|
oldTable = await getTable(tableId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
// this will throw an error if something is wrong
|
||||||
!oldTable &&
|
validate(tableToSave, oldTable)
|
||||||
(tableToSave.primary == null || tableToSave.primary.length === 0)
|
|
||||||
) {
|
|
||||||
if (tableToSave.schema.id) {
|
|
||||||
throw new Error(
|
|
||||||
"External tables with no `primary` column set will define an `id` column, but we found an `id` column in the supplied schema. Either set a `primary` column or remove the `id` column."
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!oldTable && noPrimaryKey(tableToSave)) {
|
||||||
tableToSave.primary = ["id"]
|
tableToSave.primary = ["id"]
|
||||||
tableToSave.schema.id = {
|
tableToSave.schema.id = {
|
||||||
type: FieldType.NUMBER,
|
type: FieldType.NUMBER,
|
||||||
|
@ -65,10 +93,6 @@ export async function save(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasTypeChanged(tableToSave, oldTable)) {
|
|
||||||
throw new Error("A column type has changed.")
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let view in tableToSave.views) {
|
for (let view in tableToSave.views) {
|
||||||
const tableView = tableToSave.views[view]
|
const tableView = tableToSave.views[view]
|
||||||
if (!tableView || !viewSdk.isV2(tableView)) continue
|
if (!tableView || !viewSdk.isV2(tableView)) continue
|
||||||
|
|
Loading…
Reference in New Issue