From 8aaacdf96eb0e21669ddeed673a892bfebf8ac55 Mon Sep 17 00:00:00 2001
From: Aditya Toshniwal <aditya.toshniwal@enterprisedb.com>
Date: Thu, 28 Mar 2019 08:36:41 -0400
Subject: [PATCH] Include inherited column comments and defaults in reverse
 engineered table SQL. Fixes #2627 Include comment SQL for inherited columns
 in reverse engineered table SQL. Fixes #4037 Include inherited columns in
 SELECT scripts. Fixes #4058

---
 docs/en_US/release_notes_4_4.rst              |  3 ++
 .../table/sql/10_plus/properties.sql          |  1 +
 .../table/sql/9.1_plus/properties.sql         |  1 +
 .../templates/table/sql/default/create.sql    |  4 +-
 .../servers/databases/schemas/tables/utils.py | 42 ++++++++-----------
 5 files changed, 24 insertions(+), 27 deletions(-)

#diff --git a/docs/en_US/release_notes_4_4.rst b/docs/en_US/release_notes_4_4.rst
#index a376abb0d3..463bc3360e 100644
#--- a/docs/en_US/release_notes_4_4.rst
#+++ b/docs/en_US/release_notes_4_4.rst
#@@ -38,15 +38,18 @@ Bug fixes
# *********
# 
# | `Bug #1269 <https://redmine.postgresql.org/issues/1269>`_ - Fix naming inconsistency for the column and FTS parser modules.
#+| `Bug #2627 <https://redmine.postgresql.org/issues/2627>`_ - Include inherited column comments and defaults in reverse engineered table SQL.
# | `Bug #3104 <https://redmine.postgresql.org/issues/3104>`_ - Improve a couple of German translations.
# | `Bug #3887 <https://redmine.postgresql.org/issues/3887>`_ - Use the user's full email address (not just the username part) as the basis for the storage directory name.
# | `Bug #3968 <https://redmine.postgresql.org/issues/3968>`_ - Update wcDocker to fix the issue where the Scratch Pad grows in size if the results panel is resized.
# | `Bug #3995 <https://redmine.postgresql.org/issues/3995>`_ - Avoid 'bogus varno' message from Postgres when viewing the SQL for a table with triggers.
# | `Bug #4019 <https://redmine.postgresql.org/issues/4019>`_ - Update all Python and JavaScript dependencies.
#+| `Bug #4037 <https://redmine.postgresql.org/issues/4037>`_ - Include comment SQL for inherited columns in reverse engineered table SQL.
# | `Bug #4050 <https://redmine.postgresql.org/issues/4050>`_ - Make the WHEN field a CodeMirror control on the Event Trigger dialogue.
# | `Bug #4052 <https://redmine.postgresql.org/issues/4052>`_ - Fix the online help button on the resource group dialogue.
# | `Bug #4053 <https://redmine.postgresql.org/issues/4053>`_ - Enable the online help button on the index dialogue.
# | `Bug #4054 <https://redmine.postgresql.org/issues/4054>`_ - Handle resultsets with zero columns correctly in the Query Tool.
#+| `Bug #4058 <https://redmine.postgresql.org/issues/4058>`_ - Include inherited columns in SELECT scripts.
# | `Bug #4060 <https://redmine.postgresql.org/issues/4060>`_ - Fix the latexpdf doc build.
# | `Bug #4062 <https://redmine.postgresql.org/issues/4062>`_ - Fix handling of numeric arrays in View/Edit Data.
# | `Bug #4063 <https://redmine.postgresql.org/issues/4063>`_ - Enlarge the grab handles for resizing dialogs etc.
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/properties.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/properties.sql
index 55b5e48232..8bba0afbf6 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/properties.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/properties.sql
@@ -53,6 +53,7 @@ SELECT rel.oid, rel.relname AS name, rel.reltablespace AS spcoid,rel.relacl AS r
 	array_to_string(tst.reloptions, ',') AS toast_table_vacuum_settings_str,
 	rel.reloptions AS reloptions, tst.reloptions AS toast_reloptions, rel.reloftype,
 	CASE WHEN typ.typname IS NOT NULL THEN (select quote_ident(nspname) FROM pg_namespace WHERE oid = {{scid}}::oid )||'.'||quote_ident(typ.typname) ELSE typ.typname END AS typname,
+	typ.typrelid AS typoid,
 	(CASE WHEN rel.reltoastrelid = 0 THEN false ELSE true END) AS hastoasttable,
     -- Added for pgAdmin4
 	(CASE WHEN (substring(array_to_string(rel.reloptions, ',') FROM 'autovacuum_enabled=([a-z|0-9]*)'))::boolean  THEN true ELSE false END) AS autovacuum_custom,
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/9.1_plus/properties.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/9.1_plus/properties.sql
index c76c805186..3dc29051e9 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/9.1_plus/properties.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/9.1_plus/properties.sql
@@ -52,6 +52,7 @@ SELECT rel.oid, rel.relname AS name, rel.reltablespace AS spcoid,rel.relacl AS r
 	array_to_string(tst.reloptions, ',') AS toast_table_vacuum_settings_str,
 	rel.reloptions AS reloptions, tst.reloptions AS toast_reloptions, rel.reloftype,
 	CASE WHEN typ.typname IS NOT NULL THEN (select quote_ident(nspname) FROM pg_namespace WHERE oid = {{scid}}::oid )||'.'||quote_ident(typ.typname) ELSE typ.typname END AS typname,
+	typ.typrelid AS typoid,
 	(CASE WHEN rel.reltoastrelid = 0 THEN false ELSE true END) AS hastoasttable,
     -- Added for pgAdmin4
 	(CASE WHEN (substring(array_to_string(rel.reloptions, ',') FROM 'autovacuum_enabled=([a-z|0-9]*)'))::boolean  THEN true ELSE false END) AS autovacuum_custom,
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/default/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/default/create.sql
index 82501a9140..b93cc7ca16 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/default/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/default/create.sql
@@ -41,8 +41,8 @@ CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE {{conn|qtIdent(data
 {### Add columns ###}
 {% if data.columns and data.columns|length > 0 %}
 {% for c in data.columns %}
-{% if c.name and c.cltype and not c.inheritedfrom %}
-    {{conn|qtIdent(c.name)}} {% if is_sql %}{{c.displaytypname}}{% else %}{{ GET_TYPE.CREATE_TYPE_SQL(conn, c.cltype, c.attlen, c.attprecision, c.hasSqrBracket) }}{% endif %}{% if c.collspcname %} COLLATE {{c.collspcname}}{% endif %}{% if c.attnotnull %} NOT NULL{% endif %}{% if c.defval %} DEFAULT {{c.defval}}{% endif %}
+{% if c.name and c.cltype %}
+{% if c.inheritedfromtable %}-- Inherited from table {{c.inheritedfromtable}}:{% elif c.inheritedfromtype %}-- Inherited from type {{c.inheritedfromtype}}:{% endif %} {{conn|qtIdent(c.name)}} {% if is_sql %}{{c.displaytypname}}{% else %}{{ GET_TYPE.CREATE_TYPE_SQL(conn, c.cltype, c.attlen, c.attprecision, c.hasSqrBracket) }}{% endif %}{% if c.collspcname %} COLLATE {{c.collspcname}}{% endif %}{% if c.attnotnull %} NOT NULL{% endif %}{% if c.defval %} DEFAULT {{c.defval}}{% endif %}
 {% if not loop.last %},
 {% endif %}
 {% endif %}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
index 372c4edc9a..9dbd1fe989 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
@@ -609,20 +609,21 @@ def _formatter(self, did, scid, tid, data):
         # 4) Find all the columns for tables and filter out columns which are
         #   not inherited from any table & format them one by one
 
+        other_columns = []
+        table_or_type = ''
         # Get of_type table columns and add it into columns dict
-        if data['typname']:
+        if data['typoid']:
             SQL = render_template("/".join([self.table_template_path,
                                             'get_columns_for_table.sql']),
-                                  tname=data['typname'])
+                                  tid=data['typoid'])
 
             status, res = self.conn.execute_dict(SQL)
             if not status:
                 return internal_server_error(errormsg=res)
-            data['columns'] = res['rows']
-
+            other_columns = res['rows']
+            table_or_type = 'type'
         # Get inherited table(s) columns and add it into columns dict
         elif data['coll_inherits'] and len(data['coll_inherits']) > 0:
-            columns = []
             # Return all tables which can be inherited & do not show
             # system columns
             SQL = render_template("/".join([self.table_template_path,
@@ -645,8 +646,9 @@ def _formatter(self, did, scid, tid, data):
                     status, res = self.conn.execute_dict(SQL)
                     if not status:
                         return internal_server_error(errormsg=res)
-                    columns.extend(res['rows'][:])
-            data['columns'] = columns
+                    other_columns.extend(res['rows'][:])
+
+            table_or_type = 'table'
 
         # We will fetch all the columns for the table using
         # columns properties.sql, so we need to set template path
@@ -661,24 +663,14 @@ def _formatter(self, did, scid, tid, data):
             return internal_server_error(errormsg=res)
         all_columns = res['rows']
 
-        # Filter inherited columns from all columns
-        if 'columns' in data and len(data['columns']) > 0 \
-                and len(all_columns) > 0:
-            for row in data['columns']:
-                for i, col in enumerate(all_columns):
-                    # If both name are same then remove it
-                    # as it is inherited from other table
-                    if col['name'] == row['name']:
-                        # Remove same column from all_columns as
-                        # already have it columns collection
-                        del all_columns[i]
-
-            # If any column is added then update columns collection
-            if len(all_columns) > 0:
-                data['columns'] += all_columns
-        # If no inherited columns found then add all columns
-        elif len(all_columns) > 0:
-            data['columns'] = all_columns
+        # Add inheritedfrom details from other columns - type, table
+        for col in all_columns:
+            for other_col in other_columns:
+                if col['name'] == other_col['name']:
+                    col['inheritedfrom' + table_or_type] = \
+                        other_col['inheritedfrom']
+
+        data['columns'] = all_columns
 
         if 'columns' in data and len(data['columns']) > 0:
             data = self._columns_formatter(tid, data)
