Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an environment variable to switch on/off sort columns #7119

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_schema(self, refresh=False):
schema = query_runner.get_schema(get_stats=refresh)

try:
out_schema = self._sort_schema(schema)
out_schema = self._sort_schema(schema, settings.SCHEMAS_SORT_COLUMNS)
except Exception:
logging.exception("Error sorting schema columns for data_source {}".format(self.id))
out_schema = schema
Expand All @@ -226,11 +226,17 @@ def get_schema(self, refresh=False):

return out_schema

def _sort_schema(self, schema):
return [
{"name": i["name"], "columns": sorted(i["columns"], key=lambda x: x["name"] if isinstance(x, dict) else x)}
for i in sorted(schema, key=lambda x: x["name"])
]
def _sort_schema(self, schema, sort_columns=True):
if sort_columns:
return [
{
"name": i["name"],
"columns": sorted(i["columns"], key=lambda x: x["name"] if isinstance(x, dict) else x),
}
for i in sorted(schema, key=lambda x: x["name"])
]
else:
return sorted(schema, key=lambda x: x["name"])

@property
def _schema_key(self):
Expand Down
1 change: 1 addition & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
# default set query results expired ttl 86400 seconds
QUERY_RESULTS_EXPIRED_TTL = int(os.environ.get("REDASH_QUERY_RESULTS_EXPIRED_TTL", "86400"))

SCHEMAS_SORT_COLUMNS = parse_boolean(os.environ.get("REDASH_SCHEMAS_SORT_COLUMNS", "true"))
SCHEMAS_REFRESH_SCHEDULE = int(os.environ.get("REDASH_SCHEMAS_REFRESH_SCHEDULE", 30))

AUTH_TYPE = os.environ.get("REDASH_AUTH_TYPE", "api_key")
Expand Down
23 changes: 22 additions & 1 deletion tests/models/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,28 @@ def test_schema_sorter(self):
{"name": "zoo", "columns": ["is_cow", "is_snake", "is_zebra"]},
]

real_output = self.factory.data_source._sort_schema(input_data)
real_output = self.factory.data_source._sort_schema(input_data, sort_columns=True)

self.assertEqual(real_output, expected_output)

def test_schema_sorter_columns_unsorted(self):
input_data = [
{"name": "zoo", "columns": ["is_zebra", "is_snake", "is_cow"]},
{
"name": "all_terain_vehicle",
"columns": ["has_wheels", "has_engine", "has_all_wheel_drive"],
},
]

expected_output = [
{
"name": "all_terain_vehicle",
"columns": ["has_wheels", "has_engine", "has_all_wheel_drive"],
},
{"name": "zoo", "columns": ["is_zebra", "is_snake", "is_cow"]},
]

real_output = self.factory.data_source._sort_schema(input_data, sort_columns=False)

self.assertEqual(real_output, expected_output)

Expand Down
Loading