Fix for #3721 - deleting invalid relationships if tables have been removed external to budibase - otherwise these could not be removed without deleting the datasource.

This commit is contained in:
mike12345567 2022-01-18 17:21:29 +00:00
parent cdaab9e989
commit e0949b4ac3
5 changed files with 45 additions and 27 deletions

View File

@ -429,13 +429,14 @@ Cypress.Commands.add("addDatasourceConfig", (datasource, skipFetch) => {
// Click to fetch tables // Click to fetch tables
if (skipFetch) { if (skipFetch) {
cy.get(".spectrum-Dialog-grid").within(() => { cy.get(".spectrum-Dialog-grid").within(() => {
cy.get(".spectrum-Button").contains("Skip table fetch") cy.get(".spectrum-Button")
.contains("Skip table fetch")
.click({ force: true }) .click({ force: true })
}) })
} } else {
else {
cy.get(".spectrum-Dialog-grid").within(() => { cy.get(".spectrum-Dialog-grid").within(() => {
cy.get(".spectrum-Button").contains("Save and fetch tables") cy.get(".spectrum-Button")
.contains("Save and fetch tables")
.click({ force: true }) .click({ force: true })
cy.wait(1000) cy.wait(1000)
}) })

View File

@ -1,12 +1,13 @@
// eslint-disable-next-line
const breweries = data const breweries = data
const totals = {} const totals = {}
for (let brewery of breweries) for (let brewery of breweries) {
{const state = brewery.state const state = brewery.state
if (totals[state] == null) if (totals[state] == null) {
{totals[state] = 1 totals[state] = 1
} else } else {
{totals[state]++ totals[state]++
} }
} }
const entries = Object.entries(totals) const entries = Object.entries(totals)

View File

@ -1,15 +1,16 @@
// eslint-disable-next-line
const breweries = data const breweries = data
const totals = {} const totals = {}
for (let brewery of breweries) for (let brewery of breweries) {
{const state = brewery.state const state = brewery.state
if (totals[state] == null) if (totals[state] == null) {
{totals[state] = 1 totals[state] = 1
} else } else {
{totals[state]++ totals[state]++
} }
} }
const stateCodes = const stateCodes = {
{texas: "tx", texas: "tx",
colorado: "co", colorado: "co",
florida: "fl", florida: "fl",
iwoa: "ia", iwoa: "ia",
@ -24,7 +25,7 @@ const stateCodes =
ohio: "oh", ohio: "oh",
} }
const entries = Object.entries(totals) const entries = Object.entries(totals)
return entries.map(([state, count]) => return entries.map(([state, count]) => {
{stateCodes[state.toLowerCase()] stateCodes[state.toLowerCase()]
return { state, count, flag: "http://flags.ox3.in/svg/us/${stateCode}.svg" } return { state, count, flag: "http://flags.ox3.in/svg/us/${stateCode}.svg" }
}) })

View File

@ -6,9 +6,12 @@
export let datasource export let datasource
let name = "" let name = ""
let submitted = false
$: valid = name && name.length > 0 && !datasource?.entities[name] $: valid = name && name.length > 0 && !datasource?.entities[name]
$: error = $: error =
name && datasource?.entities[name] ? "Table name already in use." : null !submitted && name && datasource?.entities[name]
? "Table name already in use."
: null
function buildDefaultTable(tableName, datasourceId) { function buildDefaultTable(tableName, datasourceId) {
return { return {
@ -26,6 +29,7 @@
} }
async function saveTable() { async function saveTable() {
submitted = true
const table = await tables.save(buildDefaultTable(name, datasource._id)) const table = await tables.save(buildDefaultTable(name, datasource._id))
await datasources.fetch() await datasources.fetch()
$goto(`../../table/${table._id}`) $goto(`../../table/${table._id}`)

View File

@ -143,11 +143,21 @@ export function isIsoDateString(str: string) {
return d.toISOString() === str return d.toISOString() === str
} }
function shouldCopyRelationship(column: { type: string, tableId?: string }, tableIds: [string]) {
return column.type === FieldTypes.LINK && column.tableId && tableIds.includes(column.tableId)
}
function shouldCopySpecialColumn(column: { type: string }, fetchedColumn: { type: string } | undefined) {
return column.type === FieldTypes.OPTIONS ||
((!fetchedColumn || fetchedColumn.type === FieldTypes.NUMBER) && column.type === FieldTypes.BOOLEAN)
}
// add the existing relationships from the entities if they exist, to prevent them from being overridden // add the existing relationships from the entities if they exist, to prevent them from being overridden
function copyExistingPropsOver( function copyExistingPropsOver(
tableName: string, tableName: string,
table: Table, table: Table,
entities: { [key: string]: any } entities: { [key: string]: any },
tableIds: [string]
) { ) {
if (entities && entities[tableName]) { if (entities && entities[tableName]) {
if (entities[tableName].primaryDisplay) { if (entities[tableName].primaryDisplay) {
@ -158,11 +168,10 @@ function copyExistingPropsOver(
if (!existingTableSchema.hasOwnProperty(key)) { if (!existingTableSchema.hasOwnProperty(key)) {
continue continue
} }
const column = existingTableSchema[key]
if ( if (
existingTableSchema[key].type === FieldTypes.LINK || shouldCopyRelationship(column, tableIds) ||
existingTableSchema[key].type === FieldTypes.OPTIONS || shouldCopySpecialColumn(column, table.schema[key])
((!table.schema[key] || table.schema[key].type === FieldTypes.NUMBER) &&
existingTableSchema[key].type === FieldTypes.BOOLEAN)
) { ) {
table.schema[key] = existingTableSchema[key] table.schema[key] = existingTableSchema[key]
} }
@ -178,6 +187,8 @@ export function finaliseExternalTables(
const invalidColumns = Object.values(InvalidColumns) const invalidColumns = Object.values(InvalidColumns)
let finalTables: { [key: string]: any } = {} let finalTables: { [key: string]: any } = {}
const errors: { [key: string]: string } = {} const errors: { [key: string]: string } = {}
// @ts-ignore
const tableIds: [string] = Object.values(tables).map(table => table._id)
for (let [name, table] of Object.entries(tables)) { for (let [name, table] of Object.entries(tables)) {
const schemaFields = Object.keys(table.schema) const schemaFields = Object.keys(table.schema)
// make sure every table has a key // make sure every table has a key
@ -189,7 +200,7 @@ export function finaliseExternalTables(
continue continue
} }
// make sure all previous props have been added back // make sure all previous props have been added back
finalTables[name] = copyExistingPropsOver(name, table, entities) finalTables[name] = copyExistingPropsOver(name, table, entities, tableIds)
} }
// sort the tables by name // sort the tables by name
finalTables = Object.entries(finalTables) finalTables = Object.entries(finalTables)