Skip to content

Commit

Permalink
Support for excluding schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
sumit0k committed Jul 22, 2021
1 parent 92e5d78 commit dfaa6ff
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ def configuration_schema(cls):
"title": "Query Group for Scheduled Queries",
"default": "default",
},
"exclude_schemas": {
"type": "string",
"title": "Exclude schemas from scanning tables",
"default": "pg_internal,pg_catalog,information_schema,pg_temp_%",
},
},
"order": [
"host",
Expand All @@ -349,6 +354,7 @@ def configuration_schema(cls):
"password",
"dbname",
"sslmode",
"exclude_schemas",
"adhoc_query_group",
"scheduled_query_group",
],
Expand Down Expand Up @@ -378,15 +384,18 @@ def _get_tables(self, schema):
# https://docs.aws.amazon.com/redshift/latest/dg/r_HAS_SCHEMA_PRIVILEGE.html
# https://docs.aws.amazon.com/redshift/latest/dg/r_SVV_EXTERNAL_SCHEMAS.html
# https://docs.aws.amazon.com/redshift/latest/dg/r_HAS_TABLE_PRIVILEGE.html
schemas_to_exclude = self.configuration.get('exclude_schemas').split(',')
full_schemas_to_exclude = '\',\''.join([schema for schema in schemas_to_exclude if '%' not in schema])
pattern_schemas_to_exclude = '\' AND table_schema NOT LIKE \''.join([schema for schema in schemas_to_exclude if ' %' in schema])
query = """
WITH tables AS (
SELECT DISTINCT table_name,
table_schema,
column_name,
ordinal_position AS pos
FROM svv_columns
WHERE table_schema NOT IN ('pg_internal','pg_catalog','information_schema')
AND table_schema NOT LIKE 'pg_temp_%'
WHERE table_schema NOT IN ('{full_schemas_to_exclude}')
AND table_schema NOT LIKE '{pattern_schemas_to_exclude}'
)
SELECT table_name, table_schema, column_name
FROM tables
Expand All @@ -397,7 +406,10 @@ def _get_tables(self, schema):
HAS_TABLE_PRIVILEGE('"' || table_schema || '"."' || table_name || '"', 'SELECT')
)
ORDER BY table_name, pos
"""
""".format(
full_schemas_to_exclude=full_schemas_to_exclude,
pattern_schemas_to_exclude=pattern_schemas_to_exclude
)

self._get_definitions(schema, query)

Expand Down Expand Up @@ -460,6 +472,11 @@ def configuration_schema(cls):
"title": "Query Group for Scheduled Queries",
"default": "default",
},
"exclude_schemas": {
"type": "string",
"title": "Exclude schemas from scanning tables",
"default": "pg_internal,pg_catalog,information_schema,pg_temp_%",
},
},
"order": [
"rolename",
Expand All @@ -472,6 +489,7 @@ def configuration_schema(cls):
"user",
"dbname",
"sslmode",
"exclude_schemas",
"adhoc_query_group",
"scheduled_query_group",
],
Expand Down

0 comments on commit dfaa6ff

Please sign in to comment.