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

Sort schema columns alphabetically #4595

Merged
merged 8 commits into from
Feb 9, 2020
12 changes: 11 additions & 1 deletion redash/handlers/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def get(self, data_source_id):
response = {}

try:
response["schema"] = data_source.get_schema(refresh)
schema = data_source.get_schema(refresh)
except NotSupported:
response["error"] = {
"code": 1,
Expand All @@ -199,6 +199,16 @@ def get(self, data_source_id):
except Exception:
response["error"] = {"code": 2, "message": "Error retrieving schema."}

try:
sorted_schema = [{"name": i['name'], "columns": sorted(i['columns'])}
for i in sorted(schema, key=lambda x: x['name'])]
response["schema"] = sorted_schema
susodapop marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
logging.exception(
"Error sorting schema columns for data_source {}"\
.format(data_source_id))
response['schema'] = schema

return response


Expand Down
4 changes: 1 addition & 3 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ def get_schema(self, refresh=False):

if cache is None:
query_runner = self.query_runner
schema = sorted(
query_runner.get_schema(get_stats=refresh), key=lambda t: t["name"]
)
schema = query_runner.get_schema(get_stats=refresh)
susodapop marked this conversation as resolved.
Show resolved Hide resolved

redis_connection.set(self._schema_key, json_dumps(schema))
else:
Expand Down