Subject: [pgAdmin4][Patch] Add property in trigger node to display if trigger is enabled or not

From: Murtuza Zabuawala <[email-masked]>
Date: Mon, 29 May 2017 15:01:16 +0530

Hi,

PFA attach patch to add property into trigger node to display if trigger is
enabled or disabled in Properties panel and also in  dialog.
RM#2386

Also used node-list-by-name control instead of muti-select control for
listing table columns to simplify the logic.

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/create.sql
index ecb0070..611a593 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/create.sql
@@ -11,7 +11,7 @@ CREATE{% if data.is_constraint_trigger %} CONSTRAINT{% endif %} TRIGGER {{ conn|
 {% endif %}{% if data.evnt_truncate %}
 {% if or_flag %} OR {% endif %}TRUNCATE{% set or_flag = True %}
 {% endif %}{% if data.evnt_update %}
-{% if or_flag %} OR {% endif %}UPDATE {% if data.columns|length > 0 %}OF {% for c in data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c.column) }}{% endfor %}{% endif %}
+{% if or_flag %} OR {% endif %}UPDATE {% if data.columns|length > 0 %}OF {% for c in data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c) }}{% endfor %}{% endif %}
 {% endif %}
 
     ON {{ conn|qtIdent(data.schema, data.table) }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/update.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/update.sql
index 86a7ba1..9b023ef 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/update.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/trigger/sql/default/update.sql
@@ -14,9 +14,9 @@ CREATE OR REPLACE TRIGGER {{ conn|qtIdent(data.name) }}
 {% if or_flag %} OR {% endif %}TRUNCATE{% set or_flag = True %}
 {% endif %}{% else %} {% if data.evnt_truncate %}
 {% if or_flag %} OR {% endif %}TRUNCATE{% set or_flag = True %} {%endif %} {% endif %}{% if data.evnt_update is not defined %}{% if o_data.evnt_update %}
-{% if or_flag %} OR {% endif %}UPDATE {% if o_data.columns|length > 0 %}OF {% for c in o_data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c.column) }}{% endfor %}{% endif %}
+{% if or_flag %} OR {% endif %}UPDATE {% if o_data.columns|length > 0 %}OF {% for c in o_data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c) }}{% endfor %}{% endif %}
 {% endif %}{% else %} {% if data.evnt_update %}
-{% if or_flag %} OR {% endif %}UPDATE {% if o_data.columns|length > 0 %}OF {% for c in o_data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c.column) }}{% endfor %}{% endif %}{% endif %}
+{% if or_flag %} OR {% endif %}UPDATE {% if o_data.columns|length > 0 %}OF {% for c in o_data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c) }}{% endfor %}{% endif %}{% endif %}
 {% endif %}
 
     ON {{ conn|qtIdent(data.schema, data.table) }}
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 be0404f..d3f51af 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
@@ -466,7 +466,7 @@ class TriggerView(PGChildNodeView):
         columns = []
 
         for row in rset['rows']:
-            columns.append({'column': row['name']})
+            columns.append(row['name'])
 
         return columns
 
@@ -572,7 +572,7 @@ class TriggerView(PGChildNodeView):
             # and convert it to string
             data['tgargs'] = self._format_args(data['custom_tgargs'])
 
-        if len(data['tgattr']) > 1:
+        if len(data['tgattr']) >= 1:
             columns = ', '.join(data['tgattr'].split(' '))
             data['columns'] = self._column_details(tid, columns)
 
@@ -790,8 +790,8 @@ class TriggerView(PGChildNodeView):
 
         try:
             sql, name = self.get_sql(scid, tid, trid, data)
-            if not isinstance(SQL, (str, unicode)):
-                return SQL
+            if not isinstance(sql, (str, unicode)):
+                return sql
             sql = sql.strip('\n').strip(' ')
 
             if sql == '':
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/templates/trigger/js/trigger.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/templates/trigger/js/trigger.js
index 3f44e7e..370bbbc 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/templates/trigger/js/trigger.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/templates/trigger/js/trigger.js
@@ -194,8 +194,8 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
           id: 'oid', label:'{{ _('OID') }}', cell: 'string',
           type: 'int', disabled: true, mode: ['properties']
         },{
-          id: 'is_enable_trigger', label:'{{ _('Enable trigger?') }}',
-          type: 'switch', disabled: 'inSchema', mode: ['properties'],
+          id: 'is_enable_trigger', label:'{{ _('Trigger enabled?') }}',
+          type: 'switch', disabled: true, mode: ['edit', 'properties'],
           group: '{{ _('Definition') }}'
         },{
           id: 'is_row_trigger', label:'{{ _('Row trigger?') }}',
@@ -419,11 +419,9 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
             control: 'sql-field', visible: true, group: '{{ _('Events') }}'
         },{
             id: 'columns', label: '{{ _('Columns') }}', url: 'nodes',
-            type: 'collection', control: 'multi-select-ajax',
+            control: 'node-list-by-name', cache_node: 'column', type: 'array',
+            select2: {'multiple': true},
             deps: ['evnt_update'], node: 'column', group: '{{ _('Events') }}',
-            model: pgBrowser.Node.Model.extend({
-                keys: ['column'], defaults: { column: undefined }
-            }),
             disabled: function(m) {
                 if(this.node_info &&  'catalog' in this.node_info) {
                     return true;

-- 
Sent via pgadmin-hackers mailing list ([email-masked])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

---

From: Dave Page <[email-masked]>
Date: Tue, 30 May 2017 16:43:57 +0100

> enabled or disabled in Properties panel and also in  dialog.

The switch is added, but I cannot toggle it from the dialogue. Can you
fix that please?

> listing table columns to simplify the logic.

OK.

Thanks!

-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

---

From: Murtuza Zabuawala <[email-masked]>
Date: Tue, 30 May 2017 21:50:28 +0530

Hi,

PFA updated patch for the same.

 
index 86a7ba1..e400cd6 100644
 
     ON {{ conn|qtIdent(data.schema, data.table) }}
@@ -44,3 +44,7 @@ COMMENT ON TRIGGER {{ conn|qtIdent(data.name) }} ON {{ conn|qtIdent(o_data.nspna
 COMMENT ON TRIGGER {{ conn|qtIdent(data.name) }} ON {{ conn|qtIdent(o_data.nspname, o_data.relname) }}
     IS {{data.description|qtLiteral}};
 {% endif %}
+{% if data.is_enable_trigger is defined  and o_data.is_enable_trigger != data.is_enable_trigger %}
+ALTER TABLE {{ conn|qtIdent(o_data.nspname, o_data.relname) }}
+    {% if data.is_enable_trigger == True %}ENABLE{% else %}DISABLE{% endif %} TRIGGER {{ conn|qtIdent(data.name) }};
+{% endif %}
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 be0404f..a3a26d7 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
@@ -466,7 +466,7 @@ class TriggerView(PGChildNodeView):
         columns = []
 
         for row in rset['rows']:
 
         return columns

 
@@ -751,14 +751,32 @@ class TriggerView(PGChildNodeView):
             status, new_trid = self.conn.execute_scalar(SQL)
             if not status:
                 return internal_server_error(errormsg=new_trid)
+            # Fetch updated properties
+            SQL = render_template("/".join([self.template_path,
+                                            'properties.sql']),
+                                  tid=tid, trid=new_trid,
+                                  datlastsysoid=self.datlastsysoid)
+
+            status, res = self.conn.execute_dict(SQL)
+
+            if not status:
+                return internal_server_error(errormsg=res)
+
+            if len(res['rows']) == 0:
+                return gone(
+                    gettext("""Could not find the trigger in the table."""))
+
+            # Making copy of output for future use
+            data = dict(res['rows'][0])
 
             return jsonify(
                 node=self.blueprint.generate_browser_node(
                     new_trid,
                     tid,
                     name,
-                    icon="icon-%s" % self.node_type if self.is_trigger_enabled
-                    else "icon-%s-bad" % self.node_type
+                    icon="icon-%s" % self.node_type if
+                    data['is_enable_trigger'] else
+                    "icon-%s-bad" % self.node_type
                 )
             )
         except Exception as e:
@@ -790,8 +808,8 @@ class TriggerView(PGChildNodeView):
 
         try:
 
             if sql == '':
index 3f44e7e..852b4f8 100644
+          type: 'switch', disabled: 'inSchema', mode: ['edit', 'properties'],

---

From: Dave Page <[email-masked]>
Date: Mon, 5 Jun 2017 17:11:36 +0100

Thanks, applied.