Skip to content

fix: ignore case while sorting usernames #7870

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

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
2 changes: 1 addition & 1 deletion coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams

// Database orders by username
slices.SortFunc(users, func(a, b database.User) bool {
return a.Username < b.Username
return strings.ToLower(a.Username) < strings.ToLower(b.Username)
Copy link
Member

Choose a reason for hiding this comment

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

I hope not, but can we have multiple users named the same? ZeroCool, zerocool, Zerocool and zeroCool. So that we'd need to handle lower(a) == lower(b) case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fortunately: User already exists. :)

})

// Filter out deleted since they should never be returned..
Expand Down
6 changes: 3 additions & 3 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ WHERE
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the username field, so select all
-- rows after the cursor.
(username) > (
(LOWER(username)) > (
SELECT
username
LOWER(username)
FROM
users
WHERE
Expand Down Expand Up @@ -184,7 +184,7 @@ WHERE
-- End of filters
ORDER BY
-- Deterministic and consistent ordering of all users. This is to ensure consistent pagination.
username ASC OFFSET @offset_opt
LOWER(username) ASC OFFSET @offset_opt
LIMIT
-- A null limit means "no limit", so 0 means return all
NULLIF(@limit_opt :: int, 0);
Expand Down
5 changes: 4 additions & 1 deletion coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,9 @@ func TestPaginatedUsers(t *testing.T) {
email = fmt.Sprintf("%d@gmail.com", i)
username = fmt.Sprintf("specialuser%d", i)
}
if i%3 == 0 {
username = strings.ToUpper(username)
}
// One side effect of having to use the api vs the db calls directly, is you cannot
// mock time. Ideally I could pass in mocked times and space these users out.
//
Expand Down Expand Up @@ -1694,7 +1697,7 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client
// sortUsers sorts by (created_at, id)
func sortUsers(users []codersdk.User) {
sort.Slice(users, func(i, j int) bool {
return users[i].Username < users[j].Username
return strings.ToLower(users[i].Username) < strings.ToLower(users[j].Username)
})
}

Expand Down