From 953c90c8d4030a5474a3e35fdff4976fca4af905 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 2 Nov 2021 13:36:23 +0000 Subject: [PATCH] Fixing some issues with MySQL and dropping foreign key constrained columns. --- .../server/src/api/controllers/table/external.js | 14 ++++++++++++++ packages/server/src/definitions/common.ts | 1 + packages/server/src/integrations/base/sqlTable.ts | 4 ++++ packages/server/src/integrations/mysql.ts | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/table/external.js b/packages/server/src/api/controllers/table/external.js index 7d332519ba..48454fbce9 100644 --- a/packages/server/src/api/controllers/table/external.js +++ b/packages/server/src/api/controllers/table/external.js @@ -98,6 +98,7 @@ function generateManyLinkSchema(datasource, column, table, relatedTable) { _id: buildExternalTableId(datasource._id, jcTblName), name: jcTblName, primary: [primary, relatedPrimary], + constrained: [primary, relatedPrimary], schema: { [primary]: foreignKeyStructure(primary, { toTable: table.name, @@ -210,6 +211,12 @@ exports.save = async function (ctx) { relationType ) fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey) + if (fkTable.constrained == null) { + fkTable.constrained = [] + } + if (fkTable.constrained.indexOf(foreignKey) === -1) { + fkTable.constrained.push(foreignKey) + } // foreign key is in other table, need to save it to external if (fkTable._id !== table._id) { extraTablesToUpdate.push(fkTable) @@ -234,6 +241,13 @@ exports.save = async function (ctx) { await makeTableRequest(datasource, op, extraTable, tables, oldExtraTable) } + // make sure the constrained list, all still exist + if (Array.isArray(tableToSave.constrained)) { + tableToSave.constrained = tableToSave.constrained.filter(constraint => + Object.keys(tableToSave.schema).includes(constraint) + ) + } + // store it into couch now for budibase reference datasource.entities[tableToSave.name] = tableToSave await db.put(datasource) diff --git a/packages/server/src/definitions/common.ts b/packages/server/src/definitions/common.ts index 4308aff0a9..c085773fa7 100644 --- a/packages/server/src/definitions/common.ts +++ b/packages/server/src/definitions/common.ts @@ -46,6 +46,7 @@ export interface Table extends Base { schema: TableSchema primaryDisplay?: string sourceId?: string + constrained?: string[] } export interface Row extends Base { diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 6eb6d02c3f..e5249dfe7c 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -19,6 +19,7 @@ function generateSchema(schema: CreateTableBuilder, table: Table, tables: Record schema.primary(metaCols.map(col => col.name)) } + // check if any columns need added const foreignKeys = Object.values(table.schema).map(col => col.foreignKey) for (let [key, column] of Object.entries(table.schema)) { @@ -78,6 +79,9 @@ function generateSchema(schema: CreateTableBuilder, table: Table, tables: Record .filter(([key, schema]) => schema.type !== FieldTypes.LINK && table.schema[key] == null) .map(([key]) => key) deletedColumns.forEach(key => { + if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) { + schema.dropForeign(key) + } schema.dropColumn(key) }) } diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index c6f271b6fe..30cbd836b5 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -265,7 +265,7 @@ module MySQLModule { if (Array.isArray(input)) { const responses = [] for (let query of input) { - responses.push(await internalQuery(this.client, query)) + responses.push(await internalQuery(this.client, query, false)) } return responses }