Ensure auto-complete works for objects in schemas other than public and pg_catalog. Fixes #3630

This commit is contained in:
Akshay Joshi 2018-09-11 12:56:14 +01:00 committed by Dave Page
parent 9c5e42c7a6
commit 4010dc80a9
4 changed files with 66 additions and 2 deletions

View File

@ -18,4 +18,5 @@ Bug fixes
*********
| `Bug #3576 <https://redmine.postgresql.org/issues/3576>`_ - Ensure queries are no longer executed when dashboards are closed.
| `Bug #3630 <https://redmine.postgresql.org/issues/3630>`_ - Ensure auto-complete works for objects in schemas other than public and pg_catalog.

View File

@ -22,6 +22,8 @@ class QueryToolAutoCompleteFeatureTest(BaseFeatureTest):
This feature test will test the query tool auto complete feature.
"""
first_schema_name = ""
second_schema_name = ""
first_table_name = ""
second_table_name = ""
@ -33,6 +35,17 @@ class QueryToolAutoCompleteFeatureTest(BaseFeatureTest):
self.page.wait_for_spinner_to_disappear()
self.page.add_server(self.server)
self.first_schema_name = "test_schema" + \
str(random.randint(1000, 3000))
test_utils.create_schema(self.server, self.test_db,
self.first_schema_name)
self.second_schema_name = "comp_schema" + \
str(random.randint(1000, 3000))
test_utils.create_schema(self.server, self.test_db,
self.second_schema_name)
self.first_table_name = "auto_comp_" + \
str(random.randint(1000, 3000))
test_utils.create_table(self.server, self.test_db,
@ -87,6 +100,18 @@ class QueryToolAutoCompleteFeatureTest(BaseFeatureTest):
print("OK.", file=sys.stderr)
self._clear_query_tool()
print("Auto complete schema other than default start with test_ ... ",
file=sys.stderr, end="")
self._auto_complete("SELECT * FROM te", self.first_schema_name)
print("OK.", file=sys.stderr)
self._clear_query_tool()
print("Auto complete schema other than default starts with comp_ ... ",
file=sys.stderr, end="")
self._auto_complete("SELECT * FROM co", self.second_schema_name)
print("OK.", file=sys.stderr)
self._clear_query_tool()
print("Auto complete first table in public schema ... ",
file=sys.stderr, end="")
self._auto_complete("SELECT * FROM public.", self.first_table_name)

View File

@ -116,16 +116,22 @@ class SQLAutoComplete(object):
self.search_path = []
schema_names = []
# Fetch the search path
if self.conn.connected():
# Fetch the search path
query = render_template(
"/".join([self.sql_path, 'schema.sql']), search_path=True)
status, res = self.conn.execute_dict(query)
if status:
for record in res['rows']:
schema_names.append(record['schema'])
self.search_path.append(record['schema'])
# Fetch the schema names
query = render_template("/".join([self.sql_path, 'schema.sql']))
status, res = self.conn.execute_dict(query)
if status:
for record in res['rows']:
schema_names.append(record['schema'])
pref = Preferences.module('sqleditor')
keywords_in_uppercase = \
pref.preference('keywords_in_uppercase').get()

View File

@ -851,3 +851,35 @@ def get_timezone_without_dst(connection):
pg_cursor.execute(timezone_no_dst_sql)
return pg_cursor.fetchone()[0]
def create_schema(server, db_name, schema_name):
"""
This function create the schema in given database name
:param server: server details
:type server: dict
:param db_name: database name
:type db_name: str
:param schema_name: schema name
:type schema_name: str
:return: None
"""
try:
connection = get_db_connection(
db_name,
server['username'],
server['db_password'],
server['host'],
server['port'],
server['sslmode']
)
old_isolation_level = connection.isolation_level
connection.set_isolation_level(0)
pg_cursor = connection.cursor()
pg_cursor.execute(
'''CREATE SCHEMA "%s"''' % schema_name)
connection.set_isolation_level(old_isolation_level)
connection.commit()
except Exception:
traceback.print_exc(file=sys.stderr)