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
Merged

Conversation

susodapop
Copy link
Contributor

@susodapop susodapop commented Jan 27, 2020

What type of PR is this? (check all applicable)

  • Feature

Description

This PR sorts the column names shown in the schema viewer alphabetically at the API level. It also moves table name sorting from the Model code into the get_schema() method of the schema resource. If the sort raises an exception then the API returns the columns in the order sent by the query runner.

Users will need to refresh their schemas before the change is visible in the front-end.

Related Tickets & Documents

https://github.com/redashlabs/product/issues/15.

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

Before

before-edited

After

after-edited

…ted`

raises an Exception it returns the column names unaltered from the query
runner.
@susodapop susodapop changed the title Adds logic to sort column names returned by the query runner. If `sor… Sort schema columns alphabetically Jan 27, 2020
@susodapop
Copy link
Contributor Author

I'd like to add tests for this behavior. What's the best way to do so? I have a few ideas:

  1. Inject a schema into the data source factory
  2. patch the get_schema method to return an unordered list
  3. Build an in-memory data-source and point SQLA there

Copy link
Member

@arikfr arikfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re. tests, there are three options I can think of:

  1. Mock get_schema to return some data.
  2. Extract sorting logic into its own function and test it with dummy data.
  3. Test this at the handler level with more of an integration test, and use the test Postgres database as the target.

redash/models/__init__.py Outdated Show resolved Hide resolved
@susodapop
Copy link
Contributor Author

I used @arikfr's second testing option. Extracted the sort into its own function and tested with dummy data.

@susodapop
Copy link
Contributor Author

Pending further feedback I think this is ready to merge.

@susodapop susodapop requested a review from arikfr January 28, 2020 18:22
@susodapop
Copy link
Contributor Author

Not sure what's causing CircleCI e2e tests to fail. I didn't change any front-end files.

@gabrieldutra
Copy link
Member

Not sure what's causing CircleCI e2e tests to fail. I didn't change any front-end files.

😪, looks like it kept flaky after #4545, for now it's a matter of re-trigger the build. I guess Cypress indeed keeps the chained element and we will need to make sure the results are fresh before asserting (cc @kravets-levko).

@kravets-levko
Copy link
Collaborator

@gabrieldutra I found another thing that makes pivot test flaky: cypress input is too fast. When test adds a new line to query - it immediately clicks Execute button. But query editor is debounced, so 50/50 it will execute query with old text - therefore test fails. Here is my attempt to fix this - ec2ede4 I have no other idea for how to detect when query object received a new text from editor.

@susodapop
Copy link
Contributor Author

I'm continuing to test local. This isn't ready to merge yet. When connected to Redash's internal metadata database the schema sort doesn't work at all.

…ever returned!

   ____  ____  ____  _____
  / __ \/ __ \/ __ \/ ___/
 / /_/ / /_/ / /_/ (__  )
 \____/\____/ .___/____/
           /_/
@susodapop
Copy link
Contributor Author

Found the issue. When I refactored to _sort_schema() I returned the wrong variable from the sort method. And I trusted my test instead of testing manually.

          _ __            
   __  __(_) /_____  _____
  / / / / / //_/ _ \/ ___/
 / /_/ / / ,< /  __(__  ) 
 \__, /_/_/|_|\___/____/  
/____/                    
                          

@susodapop
Copy link
Contributor Author

Alrighty. I added a second test case that would have caught this issue addressed in a31f901.

@susodapop
Copy link
Contributor Author

On my second unit test, would it be better to use call_count to ensure that the sort_schema() method is called? It feels strangely duplicative using identical assertions on separate tests.

The first test verifies the sort behavior. The second test verifies that Redash uses that sort behavior. Seems like the call_count is the important bit.

Thoughts @arikfr or @LevkO?

Copy link
Member

@arikfr arikfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants