public inbox for [email protected]  
help / color / mirror / Atom feed
From: Khushboo Vashi <[email protected]>
To: Akshay Joshi <[email protected]>
Cc: pgadmin-hackers <[email protected]>
Subject: Re: [pgAdmin4][Patch]: RM 5053 - Getting an error while changing the columns in the existing view
Date: Tue, 7 Apr 2020 10:00:18 +0530
Message-ID: <CAFOhELfUfAWuneyx889fQvMoEEjp6grRV05CHpgJcKuz0Z9+Nw@mail.gmail.com> (raw)
In-Reply-To: <CANxoLDeUwJbVQa7rKUWc8TadzzRN+bULNj2qnaJFPV2JbBUc8Q@mail.gmail.com>
References: <CAFOhELdGhQ1O2R+GLgNj_jbOtUduDt=rsNvwUdwmxeUxAmes6Q@mail.gmail.com>
	<CANxoLDdfJGOASsQVknwHkbk+L4zWP5UdWjPZBsVqtg8hL6-SfA@mail.gmail.com>
	<CAFOhELeDDEVtAFNE1FjiUEtu7TqFzUFxwi4DysfqvT8AsS6TDg@mail.gmail.com>
	<CANxoLDeUwJbVQa7rKUWc8TadzzRN+bULNj2qnaJFPV2JbBUc8Q@mail.gmail.com>

Hi Akshay,

Please find the attached updated patch.

On Tue, Mar 24, 2020 at 2:47 PM Akshay Joshi <[email protected]>
wrote:

> Hi Khushboo
>
> On Tue, Mar 24, 2020 at 1:47 PM Khushboo Vashi <
> [email protected]> wrote:
>
>> Hi Akshay,
>>
>> On Tue, Jan 14, 2020 at 11:47 AM Akshay Joshi <
>> [email protected]> wrote:
>>
>>> Hi Khushboo
>>>
>>> Following are the review comments:
>>>
>>>    - Fix the PEP8 issue.
>>>    - Drop query should be part of the jinja template for consistency.
>>>    Currently, it is added through the python file.
>>>
>>> The Delete query is already in the template file, I have just reused the
>> delete call and merged the SQL queries in the python file.
>>
>>>
>>>    - Any changes in the view code should not warn the user "Changing
>>>    the columns in a view requires dropping...." and we should not drop the
>>>    view. For example, I have only change the WHERE clause or added 'ORDER BY'.
>>>
>>> I have tested but  couldn't reproduce this issue.  Can you please let me
>> know the proper use case?
>>
>
>    Create a view with 'SELECT 1;' as code. Then change the code to 'SELECT
> 1234;' and click on the Save button.
>    Warning popup is displayed "Changing the columns in a view....". Click
> on the 'Yes' button and check the OID of the view. You will get the same
> OID, it means view is not recreated.
>
>
I can reproduce this issue with the given SQL but the problem is as per the
PostgreSQL documentation, (Ref:
https://www.postgresql.org/docs/12/sql-createview.html)

"CREATE OR REPLACE VIEW is similar, but if a view of the same name already
exists, it is replaced. The new query must generate the same columns that
were generated by the existing view query (that is, the same column names
in the same order and with the same data types), but it may add additional
columns to the end of the list. The calculations giving rise to the output
columns may be completely different."

So, I put a check on the columns and if the column is changed, the message
will popup.

In case of the example given by you, the column name is not changed as if
you don't give the column name it will be default and I think view would
have the column names properly.


>     I have observed below error in the browser while changing the code:
>             view.js:241 Uncaught TypeError: Cannot read property 'replace'
> of undefined
>             at child.onChange (view.js:241)
>             at HTMLDivElement.dispatch (jquery.js:5237)
>             at HTMLDivElement.elemData.handle (jquery.js:5044)
>
> Fixed.

Thanks,
Khushboo

>
>> Thanks,
>> Khushboo
>>
>>>
>>>
>>
>>> On Tue, Jan 14, 2020 at 10:27 AM Khushboo Vashi <
>>> [email protected]> wrote:
>>>
>>>> Hi,
>>>>
>>>> Please find the attached patch for RM #5053 - Getting an error while
>>>> changing the columns in the existing view.
>>>>
>>>> PostgreSQL doesn't allow to change the view columns. So, while
>>>> performing this task the existing view should be dropped first and then
>>>> recreate it and also user will get a warning first.
>>>>
>>>> Thanks,
>>>> Khushboo
>>>>
>>>
>>>
>>> --
>>> *Thanks & Regards*
>>> *Akshay Joshi*
>>>
>>> *Sr. Software Architect*
>>> *EnterpriseDB Software India Private Limited*
>>> *Mobile: +91 976-788-8246*
>>>
>>
>
> --
> *Thanks & Regards*
> *Akshay Joshi*
>
> *Sr. Software Architect*
> *EnterpriseDB Software India Private Limited*
> *Mobile: +91 976-788-8246*
>


Attachments:

  [application/octet-stream] RM_5053_v1.patch (8.3K, 3-RM_5053_v1.patch)
  download | inline diff:
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py
index fb1ad76ac..d502b29aa 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py
@@ -10,6 +10,7 @@
 """Implements View and Materialized View Node"""
 
 import copy
+import re
 from functools import wraps
 
 import simplejson as json
@@ -496,7 +497,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
                     )
                 )
         try:
-            SQL, nameOrError = self.getSQL(gid, sid, did, data)
+            SQL, nameOrError = self.getSQL(gid, sid, did, scid, data)
             if SQL is None:
                 return nameOrError
             SQL = SQL.strip('\n').strip(' ')
@@ -541,7 +542,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
             request.data, encoding='utf-8'
         )
         try:
-            SQL, name = self.getSQL(gid, sid, did, data, vid)
+            SQL, name = self.getSQL(gid, sid, did, scid, data, vid)
             if SQL is None:
                 return name
             SQL = SQL.strip('\n').strip(' ')
@@ -678,7 +679,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
             except ValueError:
                 data[k] = v
 
-        sql, nameOrError = self.getSQL(gid, sid, did, data, vid)
+        sql, nameOrError = self.getSQL(gid, sid, did, scid, data, vid)
         if sql is None:
             return nameOrError
 
@@ -692,7 +693,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
             status=200
         )
 
-    def getSQL(self, gid, sid, did, data, vid=None):
+    def getSQL(self, gid, sid, did, scid, data, vid=None):
         """
         This function will generate sql from model data
         """
@@ -716,7 +717,20 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
             if 'schema' not in data:
                 data['schema'] = res['rows'][0]['schema']
 
-            acls = []
+            DEL_SQL = None
+            if 'definition' in data:
+                new_def = re.sub(r"\W", "", data['definition']).split('FROM')
+                old_def = re.sub(r"\W", "", res['rows'][0]['definition']
+                                 ).split('FROM')
+                if 'definition' in data and (
+                        old_def[0] != new_def[0] and
+                        old_def[0] not in new_def[0]
+                ):
+                    DEL_SQL = self.delete(gid=gid, sid=sid, did=did,
+                                          scid=scid,
+                                          vid=vid, only_sql=True
+                                          )
+
             try:
                 acls = render_template(
                     "/".join([self.template_path, 'sql/allowed_privs.json'])
@@ -740,6 +754,9 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
                 SQL = render_template("/".join(
                     [self.template_path, 'sql/update.sql']), data=data,
                     o_data=old_data, conn=self.conn)
+
+                if DEL_SQL:
+                    SQL = DEL_SQL + SQL
             except Exception as e:
                 current_app.logger.exception(e)
                 return None, internal_server_error(errormsg=str(e))
@@ -1439,7 +1456,14 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare):
         if data:
             if diff_schema:
                 data['schema'] = diff_schema
-            sql, nameOrError = self.getSQL(gid, sid, did, data, oid)
+            sql, nameOrError = self.getSQL(gid, sid, did, scid, data, oid)
+            if sql.find('DROP VIEW') != -1:
+                sql = gettext("""
+-- Changing the columns in a view requires dropping and re-creating the view.
+-- This may fail if other objects are dependent upon this view,
+-- or may cause procedural functions to fail if they are not modified to
+-- take account of the changes.
+""") + sql
         else:
             if drop_sql:
                 sql = self.delete(gid=gid, sid=sid, did=did,
@@ -1547,8 +1571,8 @@ class MViewNode(ViewNode, VacuumSettings):
             data['vacuum_data']['reset'] = []
 
             # table vacuum: separate list of changed and reset data for
-            if ('vacuum_table' in data):
-                if ('changed' in data['vacuum_table']):
+            if 'vacuum_table' in data:
+                if 'changed' in data['vacuum_table']:
                     for item in data['vacuum_table']['changed']:
                         if 'value' in item.keys():
                             if item['value'] is None:
@@ -1581,8 +1605,8 @@ class MViewNode(ViewNode, VacuumSettings):
                      'value': data['autovacuum_enabled']})
 
             # toast autovacuum: separate list of changed and reset data
-            if ('vacuum_toast' in data):
-                if ('changed' in data['vacuum_toast']):
+            if 'vacuum_toast' in data:
+                if 'changed' in data['vacuum_toast']:
                     for item in data['vacuum_toast']['changed']:
                         if 'value' in item.keys():
                             toast_key = 'toast_' + item['name']
@@ -1671,14 +1695,14 @@ class MViewNode(ViewNode, VacuumSettings):
                 if 'value' in item.keys() and item['value'] is not None]
 
             # add table_enabled & toast_enabled settings
-            if ('autovacuum_custom' in data and data['autovacuum_custom']):
+            if 'autovacuum_custom' in data and data['autovacuum_custom']:
                 vacuum_table.append(
                     {
                         'name': 'autovacuum_enabled',
                         'value': str(data['autovacuum_enabled'])
                     }
                 )
-            if ('toast_autovacuum' in data and data['toast_autovacuum']):
+            if 'toast_autovacuum' in data and data['toast_autovacuum']:
                 vacuum_table.append(
                     {
                         'name': 'toast.autovacuum_enabled',
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/view.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/view.js
index 72ea59c13..49237089a 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/view.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/static/js/view.js
@@ -154,8 +154,29 @@ define('pgadmin.node.view', [
           id: 'definition', label: gettext('Code'), cell: 'string',
           type: 'text', mode: ['create', 'edit'], group: gettext('Code'),
           tabPanelCodeClass: 'sql-code-control',
-          control: Backform.SqlCodeControl,
           disabled: 'notInSchema',
+          control: Backform.SqlCodeControl.extend({
+            onChange: function() {
+              Backform.SqlCodeControl.prototype.onChange.apply(this, arguments);
+              if(this.model && this.model.changed) {
+                if(this.model.origSessAttrs && this.model.changed.definition !== undefined && (this.model.changed.definition != this.model.origSessAttrs.definition)) {
+                  let old_def = this.model.origSessAttrs.definition.replace(/\s/gi, '').split('FROM'),
+                    new_def = this.model.changed.definition.replace(/\s/gi, '').split('FROM');
+                  if (old_def[0] != new_def[0]) {
+                    this.model.warn_text = gettext('Changing the columns in a view requires dropping and re-creating the view. This may fail if other objects are dependent upon this view, or may cause procedural functions to fail if they are not modified to take account of the changes. Do you wish to continue?');
+                  } else {
+                    this.model.warn_text = undefined;
+                  }
+                }
+                else {
+                  this.model.warn_text = undefined;
+                }
+              }
+              else {
+                this.model.warn_text = undefined;
+              }
+            },
+          }),
         }, pgBrowser.SecurityGroupSchema, {
           // Add Privilege Control
           id: 'datacl', label: gettext('Privileges'), type: 'collection',


view thread (10+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: [pgAdmin4][Patch]: RM 5053 - Getting an error while changing the columns in the existing view
  In-Reply-To: <CAFOhELfUfAWuneyx889fQvMoEEjp6grRV05CHpgJcKuz0Z9+Nw@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox