Skip to content

Commit ee45b3d

Browse files
authored
fix: ignore case while sorting usernames (coder#7870)
1 parent 660bbb8 commit ee45b3d

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

coderd/database/dbfake/databasefake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
933933

934934
// Database orders by username
935935
slices.SortFunc(users, func(a, b database.User) bool {
936-
return a.Username < b.Username
936+
return strings.ToLower(a.Username) < strings.ToLower(b.Username)
937937
})
938938

939939
// Filter out deleted since they should never be returned..

coderd/database/queries.sql.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/users.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ WHERE
145145
-- The pagination cursor is the last ID of the previous page.
146146
-- The query is ordered by the username field, so select all
147147
-- rows after the cursor.
148-
(username) > (
148+
(LOWER(username)) > (
149149
SELECT
150-
username
150+
LOWER(username)
151151
FROM
152152
users
153153
WHERE
@@ -184,7 +184,7 @@ WHERE
184184
-- End of filters
185185
ORDER BY
186186
-- Deterministic and consistent ordering of all users. This is to ensure consistent pagination.
187-
username ASC OFFSET @offset_opt
187+
LOWER(username) ASC OFFSET @offset_opt
188188
LIMIT
189189
-- A null limit means "no limit", so 0 means return all
190190
NULLIF(@limit_opt :: int, 0);

coderd/users_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,9 @@ func TestPaginatedUsers(t *testing.T) {
15531553
email = fmt.Sprintf("%d@gmail.com", i)
15541554
username = fmt.Sprintf("specialuser%d", i)
15551555
}
1556+
if i%3 == 0 {
1557+
username = strings.ToUpper(username)
1558+
}
15561559
// One side effect of having to use the api vs the db calls directly, is you cannot
15571560
// mock time. Ideally I could pass in mocked times and space these users out.
15581561
//
@@ -1694,7 +1697,7 @@ func assertPagination(ctx context.Context, t *testing.T, client *codersdk.Client
16941697
// sortUsers sorts by (created_at, id)
16951698
func sortUsers(users []codersdk.User) {
16961699
sort.Slice(users, func(i, j int) bool {
1697-
return users[i].Username < users[j].Username
1700+
return strings.ToLower(users[i].Username) < strings.ToLower(users[j].Username)
16981701
})
16991702
}
17001703

0 commit comments

Comments
 (0)