From e67bed8783e48306ab307c4cc8447ef313853ef1 Mon Sep 17 00:00:00 2001 From: Hughman Date: Sun, 19 Dec 2021 15:41:04 +1100 Subject: [PATCH 1/3] Fix findConstraints The schema is a separate field from the query --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index c5f962c..b9e1fb2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -84,7 +84,7 @@ function findFunctions (pgFunctions, schemaName, tableName) { function findConstraints (pgFkConstraints, schemaName, tableName) { return pgFkConstraints - .filter(fk => fk.source_table === `${schemaName}.${tableName}`) + .filter(fk => (fk.source_schema === schemaName && fk.source_table === tableName)) .reduce((fkConstraints, constraint) => { const fkName = constraint.constraint_name if (!Object.prototype.hasOwnProperty.call(fkConstraints, fkName)) { From 9c7e273e59eb68800e2eedd51e20b58144455707 Mon Sep 17 00:00:00 2001 From: Hughman Date: Sun, 19 Dec 2021 15:44:55 +1100 Subject: [PATCH 2/3] Define 'public' as default in forign key query Public schema does not have a schema in this table, so this makes it default to it of there is no '.' in the table name --- lib/queries.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/queries.js b/lib/queries.js index d89849b..ef846d8 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -53,7 +53,8 @@ module.exports = function queries () { 'WHERE ns.nspname = ANY($1)', // List of foreign key constraints - 'SELECT constraint_name,split_part(source_table, \'.\', 1) AS source_schema, source_table, source_column, target_table, target_column, update_action, delete_action, match_type FROM ' + + 'SELECT constraint_name,(CASE position(\'.\' in SOURCE_TABLE) WHEN 0 THEN \'public\' ELSE split_part(SOURCE_TABLE, \'.\', 1) END) AS source_schema,' + + 'source_table, source_column, target_table, target_column, update_action, delete_action, match_type FROM ' + '(SELECT constraint_name,source_table::regclass::text AS source_table, source_attr.attname AS source_column, ' + 'target_table::regclass::text, target_attr.attname AS target_column, update_action, delete_action, match_type ' + 'FROM pg_attribute target_attr, pg_attribute source_attr, ' + @@ -67,7 +68,7 @@ module.exports = function queries () { ') query2 ' + 'WHERE target_attr.attnum = target_constraints AND target_attr.attrelid = target_table ' + 'AND source_attr.attnum = source_constraints AND source_attr.attrelid = source_table) AS fk_constraints ' + - 'WHERE split_part(source_table, \'.\', 1) = ANY($1)', + 'WHERE (CASE position(\'.\' in SOURCE_TABLE) WHEN 0 THEN \'public\' ELSE split_part(SOURCE_TABLE, \'.\', 1) END) = ANY($1)', // List of triggers 'SELECT trigger_name, trigger_schema, event_object_schema, event_object_table, event_manipulation, action_condition, action_statement, action_orientation, action_timing ' + From 5ce298516a8f4c654903c6c68a19b1ce4e08b08c Mon Sep 17 00:00:00 2001 From: Hughman Date: Sun, 19 Dec 2021 16:16:06 +1100 Subject: [PATCH 3/3] Pass on dot format targetTable in findConstraints --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index b9e1fb2..97b04b8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -89,7 +89,7 @@ function findConstraints (pgFkConstraints, schemaName, tableName) { const fkName = constraint.constraint_name if (!Object.prototype.hasOwnProperty.call(fkConstraints, fkName)) { fkConstraints[fkName] = { - targetTable: constraint.target_table, + targetTable: `${constraint.source_schema}.${constraint.target_table}`, sourceColumns: [constraint.source_column], targetColumns: [constraint.target_column], updateAction: FK_ACTION_CODES[constraint.update_action],