From ce146b679c053f2ecc6bec0bfc562d94df8191c9 Mon Sep 17 00:00:00 2001 From: Yogesh Mahajan Date: Thu, 3 Aug 2023 09:42:17 +0530 Subject: [PATCH] Add support for TRUNCATE trigger in foreign table. #6448 --- .../databases/schemas/foreign_tables/__init__.py | 16 +++++++++------- .../schemas/foreign_tables/children/__init__.py | 13 +++++++++++++ .../foreign_tables/static/js/foreign_table.js | 3 ++- .../schemas/tables/columns/static/js/column.js | 4 +++- .../schemas/tables/constraints/__init__.py | 4 +++- .../static/js/check_constraint.js | 2 +- .../static/js/exclusion_constraint.js | 5 ++++- .../foreign_key/static/js/foreign_key.js | 6 +++++- .../index_constraint/static/js/primary_key.js | 2 ++ .../static/js/unique_constraint.js | 2 ++ .../tables/constraints/static/js/constraints.js | 4 ++-- .../schemas/tables/triggers/__init__.py | 7 ++++--- .../schemas/tables/triggers/static/js/trigger.js | 12 +++++++++--- 13 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/children/__init__.py diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py index 9b8142138..07924f93d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py @@ -73,13 +73,6 @@ class ForeignTableModule(SchemaChildModule): base_template_path=ForeignTableView.BASE_TEMPLATE_PATH): yield self.generate_browser_collection_node(scid) - @property - def node_inode(self): - """ - Make the node as leaf node. - """ - return False - @property def script_load(self): """ @@ -88,6 +81,15 @@ class ForeignTableModule(SchemaChildModule): """ return databases.DatabaseModule.node_type + def register(self, app, options): + from pgadmin.browser.server_groups.servers.databases.schemas.\ + tables.triggers import blueprint as module + self.submodules.append(module) + from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ + constraints import blueprint as module + self.submodules.append(module) + super().register(app, options) + blueprint = ForeignTableModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/children/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/children/__init__.py new file mode 100644 index 000000000..d165c500b --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/children/__init__.py @@ -0,0 +1,13 @@ +""" +We will use the existing modules for creating children module under +foreign tables. + +Do not remove these imports as they will be automatically imported by the view +module as its children +""" +from pgadmin.browser.server_groups.servers.databases.schemas.tables.columns \ + import blueprint as columns_module +from pgadmin.browser.server_groups.servers.databases.schemas.tables.triggers \ + import blueprint as triggers_modules +from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ + constraints import blueprint as constraints_modules diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js index a224da3d6..5e9eab873 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js @@ -16,7 +16,8 @@ import _ from 'lodash'; define('pgadmin.node.foreign_table', [ 'sources/gettext', 'sources/url_for', 'pgadmin.browser', 'pgadmin.node.schema.dir/child', 'pgadmin.node.schema.dir/schema_child_tree_node', - 'pgadmin.browser.collection', + 'pgadmin.browser.collection','pgadmin.node.column', + 'pgadmin.node.constraints' ], function( gettext, url_for, pgBrowser, schemaChild, schemaChildTreeNode ) { diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js index 50d24df0b..0d6824bdb 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js @@ -31,7 +31,9 @@ define('pgadmin.node.column', [ if (!pgBrowser.Nodes['column']) { pgBrowser.Nodes['column'] = pgBrowser.Node.extend({ - parent_type: ['table', 'view', 'mview'], + // Foreign table is added in parent_type to support triggers on + // foreign table where we need column information. + parent_type: ['table', 'view', 'mview', 'foreign_table'], collection_type: ['coll-table', 'coll-view', 'coll-mview'], type: 'column', label: gettext('Column'), diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py index b63c0590d..79acb1ee3 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py @@ -55,10 +55,12 @@ class ConstraintsModule(CollectionNodeModule): self.max_ver = None super().__init__(*args, **kwargs) - def get_nodes(self, gid, sid, did, scid, tid): + def get_nodes(self, gid, sid, did, scid, **kwargs): """ Generate the collection node """ + assert ('tid' in kwargs or 'vid' in kwargs or 'foid' in kwargs) + tid = kwargs.get('tid', kwargs.get('vid', kwargs.get('foid', None))) nodes = [] for name in ConstraintRegistry.registry: view = (ConstraintRegistry.registry[name])['nodeview'] diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js index a945f5128..f73041c33 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js @@ -30,7 +30,7 @@ define('pgadmin.node.check_constraint', [ dialogHelp: url_for('help.static', {'filename': 'check_dialog.html'}), hasSQL: true, hasDepends: true, - parent_type: ['table','partition'], + parent_type: ['table','partition','foreign_table'], url_jump_after_node: 'schema', Init: function() { // Avoid mulitple registration of menus diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js index 534d1bcad..840412bd8 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js @@ -75,7 +75,10 @@ define('pgadmin.node.exclusion_constraint', [ // If it is schema then allow user to create table if (_.indexOf(['schema'], d._type) > -1) - return !is_immediate_parent_table_partitioned; + {return !is_immediate_parent_table_partitioned;} + else if (_.indexOf(['foreign_table', 'coll-foreign_table'], d._type) > -1) { + return false; + } parents.push(d._type); i = t.hasParent(i) ? t.parent(i) : null; d = i ? t.itemData(i) : null; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js index 5a482e2c6..6715739ec 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.js @@ -110,8 +110,12 @@ define('pgadmin.node.foreign_key', [ } // If it is schema then allow user to c reate table - if (_.indexOf(['schema'], d._type) > -1) + if (_.indexOf(['schema'], d._type) > -1){ return !is_immediate_parent_table_partitioned; + }else if (_.indexOf(['foreign_table', 'coll-foreign_table'], d._type) > -1) { + return false; + } + parents.push(d._type); i = t.hasParent(i) ? t.parent(i) : null; d = i ? t.itemData(i) : null; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.js index ecda8d942..f894a7e59 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.js @@ -86,6 +86,8 @@ define('pgadmin.node.primary_key', [ } }); return !primary_key_found; + }else if (_.indexOf(['foreign_table', 'coll-foreign_table'], d._type) > -1) { + return false; } parents.push(d._type); i = t.hasParent(i) ? t.parent(i) : null; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.js index 60c2f0904..72958b96e 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.js @@ -71,6 +71,8 @@ define('pgadmin.node.unique_constraint', [ // If it is schema then allow user to c reate table if (_.indexOf(['schema'], d._type) > -1) { return !is_immediate_parent_table_partitioned; + }else if (_.indexOf(['foreign_table', 'coll-foreign_table'], d._type) > -1) { + return false; } parents.push(d._type); i = t.hasParent(i) ? t.parent(i) : null; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/static/js/constraints.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/static/js/constraints.js index 67e690ce8..a3a4ed87b 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/static/js/constraints.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/static/js/constraints.js @@ -31,8 +31,8 @@ define('pgadmin.node.constraints', [ pgAdmin.Browser.Nodes['constraints'] = pgBrowser.Node.extend({ type: 'constraints', label: gettext('Constraints'), - collection_type: 'coll-constraints', - parent_type: ['table','partition'], + collection_type: ['coll-constraints','coll-foreign_table'], + parent_type: ['table','foreign_table'], url_jump_after_node: 'schema', Init: function() { /* Avoid mulitple registration of menus */ diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py index 61ca51c8c..b838180cb 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/__init__.py @@ -100,12 +100,13 @@ class TriggerModule(CollectionNodeModule): """ Generate the collection node """ - assert ('tid' in kwargs or 'vid' in kwargs) + assert ('tid' in kwargs or 'vid' in kwargs or 'foid' in kwargs) + tid = kwargs.get('tid', kwargs.get('vid', kwargs.get('foid', None))) if self.has_nodes(sid, did, scid=scid, - tid=kwargs.get('tid', kwargs.get('vid', None)), + tid=tid, base_template_path=TriggerView.BASE_TEMPLATE_PATH): yield self.generate_browser_collection_node( - kwargs['tid'] if 'tid' in kwargs else kwargs['vid'] + tid ) @property diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js index 0733534ac..09d028a55 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/static/js/trigger.js @@ -35,8 +35,8 @@ define('pgadmin.node.trigger', [ if (!pgBrowser.Nodes['trigger']) { pgAdmin.Browser.Nodes['trigger'] = pgBrowser.Node.extend({ - parent_type: ['table', 'view', 'partition'], - collection_type: ['coll-table', 'coll-view'], + parent_type: ['table', 'view', 'partition', 'foreign_table'], + collection_type: ['coll-table', 'coll-view','coll-foreign_table'], type: 'trigger', label: gettext('Trigger'), hasSQL: true, @@ -93,7 +93,13 @@ define('pgadmin.node.trigger', [ category: 'create', priority: 4, label: gettext('Trigger...'), data: {action: 'create', check: true}, enable: 'canCreate', - }, + },{ + name: 'create_trigger_onForeignTable', node: 'foreign_table', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 3, label: gettext('Trigger...'), + data: {action: 'create', check: true}, + enable: 'canCreate', + } ]); }, callbacks: {