From 0fd6edab640e629a3ba244256f42ce72c5e2b52f Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Fri, 3 Apr 2020 19:15:10 +0200 Subject: [PATCH] services/sqlstore: Order results in UserSearch by username/email (#23328) * services/sqlstore: Order results in UserSearch by username/email * Add index (login,email) on user table --- pkg/services/sqlstore/migrations/user_mig.go | 4 ++++ pkg/services/sqlstore/sqlstore.go | 2 -- pkg/services/sqlstore/user.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/services/sqlstore/migrations/user_mig.go b/pkg/services/sqlstore/migrations/user_mig.go index 8873f2bea79..5efac52d06e 100644 --- a/pkg/services/sqlstore/migrations/user_mig.go +++ b/pkg/services/sqlstore/migrations/user_mig.go @@ -122,6 +122,10 @@ func addUserMigrations(mg *Migrator) { mg.AddMigration("Add is_disabled column to user", NewAddColumnMigration(userV2, &Column{ Name: "is_disabled", Type: DB_Bool, Nullable: false, Default: "0", })) + + mg.AddMigration("Add index user.login/user.email", NewAddIndexMigration(userV2, &Index{ + Cols: []string{"login", "email"}, + })) } type AddMissingUserSaltAndRandsMigration struct { diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index 42decd7c345..23a0b9e3b81 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -68,7 +68,6 @@ func (ss *SqlStore) Init() error { ss.readConfig() engine, err := ss.getEngine() - if err != nil { return fmt.Errorf("Fail to connect to database: %v", err) } @@ -232,7 +231,6 @@ func (ss *SqlStore) buildConnectionString() (string, error) { func (ss *SqlStore) getEngine() (*xorm.Engine, error) { connectionString, err := ss.buildConnectionString() - if err != nil { return nil, err } diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index c401ef18bbe..3b16bfeed80 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -454,7 +454,7 @@ func SearchUsers(query *models.SearchUsersQuery) error { offset := query.Limit * (query.Page - 1) sess.Limit(query.Limit, offset) sess.Cols("u.id", "u.email", "u.name", "u.login", "u.is_admin", "u.is_disabled", "u.last_seen_at", "user_auth.auth_module") - sess.OrderBy("u.id") + sess.Asc("u.login", "u.email") if err := sess.Find(&query.Result.Users); err != nil { return err }