Skip to content

feat: sort users by username #7838

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 4 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: sort users by username
  • Loading branch information
mtojek committed Jun 5, 2023
commit 1358d816b19271b90858d7a19eabed95e649e31e
9 changes: 2 additions & 7 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,14 +931,9 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
users := make([]database.User, len(q.users))
copy(users, q.users)

// Database orders by created_at
// Database orders by username
slices.SortFunc(users, func(a, b database.User) bool {
if a.CreatedAt.Equal(b.CreatedAt) {
// Technically the postgres database also orders by uuid. So match
// that behavior
return a.ID.String() < b.ID.String()
}
return a.CreatedAt.Before(b.CreatedAt)
return sort.StringsAreSorted([]string{a.Username, 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.

Suggestion: This is essentially just a a.Username < b.Username comparison, could be simplified.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

})

// Filter out deleted since they should never be returned..
Expand Down
20 changes: 10 additions & 10 deletions coderd/database/dbfake/databasefake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"reflect"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -106,26 +107,25 @@ func TestExactMethods(t *testing.T) {
}
}

// TestUserOrder ensures that the fake database returns users in the order they
// were created.
// TestUserOrder ensures that the fake database returns users sorted by username.
func TestUserOrder(t *testing.T) {
t.Parallel()

db := dbfake.New()
now := database.Now()
count := 10
for i := 0; i < count; i++ {
dbgen.User(t, db, database.User{
Username: fmt.Sprintf("user%d", i),
CreatedAt: now,
})

usernames := []string{"b-user", "d-user", "a-user", "c-user", "e-user"}
for _, username := range usernames {
dbgen.User(t, db, database.User{Username: username, CreatedAt: now})
}

users, err := db.GetUsers(context.Background(), database.GetUsersParams{})
require.NoError(t, err)
require.Lenf(t, users, count, "expected %d users", count)
require.Lenf(t, users, len(usernames), "expected %d users", len(usernames))

sort.Strings(usernames)
for i, user := range users {
require.Equal(t, fmt.Sprintf("user%d", i), user.Username)
require.Equal(t, usernames[i], user.Username)
}
}

Expand Down
5 changes: 2 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.

5 changes: 2 additions & 3 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ WHERE
END
-- End of filters
ORDER BY
-- Deterministic and consistent ordering of all users, even if they share
-- a timestamp. This is to ensure consistent pagination.
(created_at, id) ASC OFFSET @offset_opt
-- Deterministic and consistent ordering of all users. This is to ensure consistent pagination.
username ASC OFFSET @offset_opt
LIMIT
-- A null limit means "no limit", so 0 means return all
NULLIF(@limit_opt :: int, 0);
Expand Down