Skip to content

Commit

Permalink
Ensure that automatically created indexes should be shown in the tree…
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-joshi committed Nov 30, 2023
1 parent fd1a075 commit 917e5be
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/en_US/release_notes_8_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Housekeeping
Bug fixes
*********

| `Issue #6717 <https://github.com/pgadmin-org/pgadmin4/issues/6717>`_ - Ensure that automatically created indexes should be shown in the treeview.
| `Issue #6803 <https://github.com/pgadmin-org/pgadmin4/issues/6803>`_ - Fixed an issue where reading process logs throws an error when DATA_DIR is moved to a networked drive.
| `Issue #6887 <https://github.com/pgadmin-org/pgadmin4/issues/6887>`_ - Fixed an issue where syntax error was not highlighting in query tool.
| `Issue #6921 <https://github.com/pgadmin-org/pgadmin4/issues/6921>`_ - Fixed an issue where on entering full screen, the option label is not changed to 'Exit Full Screen' in desktop mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ def list(self, gid, sid, did, scid, tid):
"""

SQL = render_template(
"/".join([self.template_path, self._NODES_SQL]), tid=tid
"/".join([self.template_path, self._NODES_SQL]), tid=tid,
show_sys_objects=self.blueprint.show_system_objects
)
status, res = self.conn.execute_dict(SQL)

Expand Down Expand Up @@ -414,7 +415,8 @@ def node(self, gid, sid, did, scid, tid, idx):
"""
SQL = render_template(
"/".join([self.template_path, self._NODES_SQL]),
tid=tid, idx=idx
tid=tid, idx=idx,
show_sys_objects=self.blueprint.show_system_objects
)
status, rset = self.conn.execute_2darray(SQL)
if not status:
Expand Down Expand Up @@ -453,7 +455,8 @@ def nodes(self, gid, sid, did, scid, tid):
"""
res = []
SQL = render_template(
"/".join([self.template_path, self._NODES_SQL]), tid=tid
"/".join([self.template_path, self._NODES_SQL]), tid=tid,
show_sys_objects=self.blueprint.show_system_objects
)
status, rset = self.conn.execute_2darray(SQL)
if not status:
Expand Down Expand Up @@ -511,7 +514,8 @@ def _fetch_properties(self, did, tid, idx):
SQL = render_template(
"/".join([self.template_path, self._PROPERTIES_SQL]),
did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
show_sys_objects=self.blueprint.show_system_objects
)

status, res = self.conn.execute_dict(SQL)
Expand Down Expand Up @@ -717,7 +721,8 @@ def delete(self, gid, sid, did, scid, tid, **kwargs):
SQL = render_template(
"/".join([self.template_path, self._PROPERTIES_SQL]),
did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
show_sys_objects=self.blueprint.show_system_objects
)

status, res = self.conn.execute_dict(SQL)
Expand Down Expand Up @@ -774,7 +779,8 @@ def update(self, gid, sid, did, scid, tid, idx):
try:
SQL, name = index_utils.get_sql(
self.conn, data=data, did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID)
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
show_sys_objects=self.blueprint.show_system_objects)
if not isinstance(SQL, str):
return SQL
SQL = SQL.strip('\n').strip(' ')
Expand Down Expand Up @@ -839,7 +845,8 @@ def msql(self, gid, sid, did, scid, tid, idx=None):
try:
sql, name = index_utils.get_sql(
self.conn, data=data, did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID, mode='create')
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID, mode='create',
show_sys_objects=self.blueprint.show_system_objects)
if not isinstance(sql, str):
return sql
sql = sql.strip('\n').strip(' ')
Expand Down Expand Up @@ -869,7 +876,8 @@ def sql(self, gid, sid, did, scid, tid, idx):
SQL = index_utils.get_reverse_engineered_sql(
self.conn, schema=self.schema, table=self.table, did=did,
tid=tid, idx=idx, datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
add_not_exists_clause=True
add_not_exists_clause=True,
show_sys_objects=self.blueprint.show_system_objects
)

return ajax_response(response=SQL)
Expand Down Expand Up @@ -899,7 +907,8 @@ def get_sql_from_index_diff(self, **kwargs):

sql, name = index_utils.get_sql(
self.conn, data=data, did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID, mode='create')
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID, mode='create',
show_sys_objects=self.blueprint.show_system_objects)

sql = sql.strip('\n').strip(' ')

Expand All @@ -909,7 +918,8 @@ def get_sql_from_index_diff(self, **kwargs):
table=self.table, did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
template_path=None, with_header=False,
add_not_exists_clause=True
add_not_exists_clause=True,
show_sys_objects=self.blueprint.show_system_objects
)

drop_sql = ''
Expand Down Expand Up @@ -1004,7 +1014,8 @@ def statistics(self, gid, sid, did, scid, tid, idx=None):
SQL = render_template(
"/".join([self.template_path, self._PROPERTIES_SQL]),
did=did, tid=tid, idx=idx,
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID,
show_sys_objects=self.blueprint.show_system_objects
)
status, res = self.conn.execute_dict(SQL)
if not status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
from pgadmin.utils.exception import ObjectGone, ExecuteError
from functools import wraps

AUTO_CREATE_INDEX_MSG = "-- This primary key index is automatically " \
"generated from a constraint with an identical name.\n-- " \
"For more details, refer to the Constraints node. Note that this type " \
"of index is only visible \n-- when the 'Show system objects?' is set " \
"to True in the Preferences.\n\n"


def get_template_path(f):
"""
Expand Down Expand Up @@ -233,12 +239,14 @@ def get_sql(conn, **kwargs):
mode = kwargs.get('mode', None)
template_path = kwargs.get('template_path', None)
if_exists_flag = kwargs.get('if_exists_flag', False)
show_sys_obj = kwargs.get('show_sys_objects', False)

name = data['name'] if 'name' in data else None
if idx is not None:
sql = render_template("/".join([template_path, 'properties.sql']),
did=did, tid=tid, idx=idx,
datlastsysoid=datlastsysoid)
datlastsysoid=datlastsysoid,
show_sys_objects=show_sys_obj)

status, res = conn.execute_dict(sql)
if not status:
Expand Down Expand Up @@ -300,10 +308,12 @@ def get_reverse_engineered_sql(conn, **kwargs):
template_path = kwargs.get('template_path', None)
with_header = kwargs.get('with_header', True)
if_exists_flag = kwargs.get('add_not_exists_clause', False)
show_sys_obj = kwargs.get('show_sys_objects', False)

SQL = render_template("/".join([template_path, 'properties.sql']),
did=did, tid=tid, idx=idx,
datlastsysoid=datlastsysoid)
datlastsysoid=datlastsysoid,
show_sys_objects=show_sys_obj)

status, res = conn.execute_dict(SQL)
if not status:
Expand Down Expand Up @@ -336,7 +346,12 @@ def get_reverse_engineered_sql(conn, **kwargs):
if_exists_flag=if_exists_flag)

if with_header:
sql_header = "-- Index: {0}\n\n-- ".format(data['name'])
sql_header = ''
# Add a Note if index is automatically created.
if 'conname' in data and data['conname'] is not None:
sql_header += AUTO_CREATE_INDEX_MSG

sql_header += "-- Index: {0}\n\n-- ".format(data['name'])

sql_header += render_template("/".join([template_path, 'delete.sql']),
data=data, conn=conn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey,
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname,
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
Expand All @@ -29,6 +29,8 @@ FROM pg_catalog.pg_index idx
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey,
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname,
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
Expand All @@ -30,6 +30,8 @@ FROM pg_catalog.pg_index idx
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey,
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname,
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
Expand All @@ -30,6 +30,8 @@ FROM pg_catalog.pg_index idx
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ FROM pg_catalog.pg_index idx
LEFT JOIN pg_catalog.pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='pg_constraint') AND dep.deptype='i')
LEFT OUTER JOIN pg_catalog.pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid)
WHERE indrelid = {{tid}}::OID
{### To show system objects ###}
{% if not showsysobj %}
AND conname is NULL
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ FROM pg_catalog.pg_index idx
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}
AND cls.oid = {{ idx }}::OID
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey,
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname,
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
Expand All @@ -23,6 +23,8 @@ FROM pg_catalog.pg_index idx
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname

0 comments on commit 917e5be

Please sign in to comment.