Allowing external formulas to be updated.
This commit is contained in:
parent
0f54f64ad9
commit
a3ebf0bffd
|
@ -6,13 +6,19 @@ import {
|
||||||
QueryJson,
|
QueryJson,
|
||||||
RenameColumn,
|
RenameColumn,
|
||||||
Table,
|
Table,
|
||||||
|
FieldType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { breakExternalTableId } from "../utils"
|
import { breakExternalTableId } from "../utils"
|
||||||
import SchemaBuilder = Knex.SchemaBuilder
|
import SchemaBuilder = Knex.SchemaBuilder
|
||||||
import CreateTableBuilder = Knex.CreateTableBuilder
|
import CreateTableBuilder = Knex.CreateTableBuilder
|
||||||
import { FieldTypes, RelationshipType } from "../../constants"
|
import { RelationshipType } from "../../constants"
|
||||||
import { utils } from "@budibase/shared-core"
|
import { utils } from "@budibase/shared-core"
|
||||||
|
|
||||||
|
function isIgnoredType(type: FieldType) {
|
||||||
|
const ignored = [FieldType.LINK, FieldType.FORMULA]
|
||||||
|
return ignored.indexOf(type) !== -1
|
||||||
|
}
|
||||||
|
|
||||||
function generateSchema(
|
function generateSchema(
|
||||||
schema: CreateTableBuilder,
|
schema: CreateTableBuilder,
|
||||||
table: Table,
|
table: Table,
|
||||||
|
@ -47,13 +53,13 @@ function generateSchema(
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch (column.type) {
|
switch (column.type) {
|
||||||
case FieldTypes.STRING:
|
case FieldType.STRING:
|
||||||
case FieldTypes.OPTIONS:
|
case FieldType.OPTIONS:
|
||||||
case FieldTypes.LONGFORM:
|
case FieldType.LONGFORM:
|
||||||
case FieldTypes.BARCODEQR:
|
case FieldType.BARCODEQR:
|
||||||
schema.text(key)
|
schema.text(key)
|
||||||
break
|
break
|
||||||
case FieldTypes.BB_REFERENCE:
|
case FieldType.BB_REFERENCE:
|
||||||
const subtype = column.subtype as FieldSubtype
|
const subtype = column.subtype as FieldSubtype
|
||||||
switch (subtype) {
|
switch (subtype) {
|
||||||
case FieldSubtype.USER:
|
case FieldSubtype.USER:
|
||||||
|
@ -66,7 +72,7 @@ function generateSchema(
|
||||||
throw utils.unreachable(subtype)
|
throw utils.unreachable(subtype)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case FieldTypes.NUMBER:
|
case FieldType.NUMBER:
|
||||||
// if meta is specified then this is a junction table entry
|
// if meta is specified then this is a junction table entry
|
||||||
if (column.meta && column.meta.toKey && column.meta.toTable) {
|
if (column.meta && column.meta.toKey && column.meta.toTable) {
|
||||||
const { toKey, toTable } = column.meta
|
const { toKey, toTable } = column.meta
|
||||||
|
@ -76,21 +82,21 @@ function generateSchema(
|
||||||
schema.float(key)
|
schema.float(key)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case FieldTypes.BIGINT:
|
case FieldType.BIGINT:
|
||||||
schema.bigint(key)
|
schema.bigint(key)
|
||||||
break
|
break
|
||||||
case FieldTypes.BOOLEAN:
|
case FieldType.BOOLEAN:
|
||||||
schema.boolean(key)
|
schema.boolean(key)
|
||||||
break
|
break
|
||||||
case FieldTypes.DATETIME:
|
case FieldType.DATETIME:
|
||||||
schema.datetime(key, {
|
schema.datetime(key, {
|
||||||
useTz: !column.ignoreTimezones,
|
useTz: !column.ignoreTimezones,
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
case FieldTypes.ARRAY:
|
case FieldType.ARRAY:
|
||||||
schema.json(key)
|
schema.json(key)
|
||||||
break
|
break
|
||||||
case FieldTypes.LINK:
|
case FieldType.LINK:
|
||||||
// this side of the relationship doesn't need any SQL work
|
// this side of the relationship doesn't need any SQL work
|
||||||
if (
|
if (
|
||||||
column.relationshipType !== RelationshipType.MANY_TO_ONE &&
|
column.relationshipType !== RelationshipType.MANY_TO_ONE &&
|
||||||
|
@ -121,22 +127,18 @@ function generateSchema(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (renamed) {
|
const oldType = renamed ? oldTable?.schema[renamed.old].type : undefined
|
||||||
|
if (renamed && oldType && !isIgnoredType(oldType)) {
|
||||||
schema.renameColumn(renamed.old, renamed.updated)
|
schema.renameColumn(renamed.old, renamed.updated)
|
||||||
}
|
}
|
||||||
|
|
||||||
// need to check if any columns have been deleted
|
// need to check if any columns have been deleted
|
||||||
if (oldTable) {
|
if (oldTable) {
|
||||||
const deletedColumns = Object.entries(oldTable.schema)
|
const deletedColumns = Object.entries(oldTable.schema).filter(
|
||||||
.filter(
|
([key, column]) => isIgnoredType(column.type) && table.schema[key] == null
|
||||||
([key, schema]) =>
|
)
|
||||||
schema.type !== FieldTypes.LINK &&
|
deletedColumns.forEach(([key, column]) => {
|
||||||
schema.type !== FieldTypes.FORMULA &&
|
if (renamed?.old === key || isIgnoredType(column.type)) {
|
||||||
table.schema[key] == null
|
|
||||||
)
|
|
||||||
.map(([key]) => key)
|
|
||||||
deletedColumns.forEach(key => {
|
|
||||||
if (renamed?.old === key) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) {
|
if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) {
|
||||||
|
|
Loading…
Reference in New Issue