From d2d871af359017be0d8f2023d9e0b5c84e904cb0 Mon Sep 17 00:00:00 2001 From: WenShuang Lu Date: Thu, 28 Nov 2019 17:09:42 +0800 Subject: [PATCH] cli: getuser command can get Super_priv --- src/model/mysql.go | 5 +++-- src/mysql/mock.go | 6 ++++-- src/mysql/mysqlbase.go | 4 ++-- src/mysql/mysqlbase_test.go | 13 ++++++++----- src/server/rpc_user_test.go | 6 ++++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/model/mysql.go b/src/model/mysql.go index ee1d615..859c476 100644 --- a/src/model/mysql.go +++ b/src/model/mysql.go @@ -181,8 +181,9 @@ type MysqlUserRPCRequest struct { } type MysqlUser struct { - User string - Host string + User string + Host string + SuperPriv string } type MysqlUserRPCResponse struct { diff --git a/src/mysql/mock.go b/src/mysql/mock.go index 335c8d5..1da99c1 100644 --- a/src/mysql/mock.go +++ b/src/mysql/mock.go @@ -310,9 +310,11 @@ func (mogtid *MockGTID) CreateUser(db *sql.DB, user string, passwd string) error func DefaultGetUser(db *sql.DB) ([]model.MysqlUser, error) { return []model.MysqlUser{ {User: "user1", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "N"}, {User: "root", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "Y"}, }, nil } diff --git a/src/mysql/mysqlbase.go b/src/mysql/mysqlbase.go index de8808c..7ef2b24 100644 --- a/src/mysql/mysqlbase.go +++ b/src/mysql/mysqlbase.go @@ -292,17 +292,17 @@ func (my *MysqlBase) CheckUserExists(db *sql.DB, user string) (bool, error) { // GetUser used to get the mysql user list func (my *MysqlBase) GetUser(db *sql.DB) ([]model.MysqlUser, error) { - query := fmt.Sprintf("SELECT User, Host FROM mysql.user") + query := fmt.Sprintf("SELECT User, Host, Super_priv FROM mysql.user") rows, err := Query(db, query) if err != nil { return nil, err } var Users = make([]model.MysqlUser, len(rows)) - for i, v := range rows { Users[i].User = v["User"] Users[i].Host = v["Host"] + Users[i].SuperPriv = v["Super_priv"] } return Users, nil } diff --git a/src/mysql/mysqlbase_test.go b/src/mysql/mysqlbase_test.go index eaa95d2..8ee7972 100644 --- a/src/mysql/mysqlbase_test.go +++ b/src/mysql/mysqlbase_test.go @@ -436,15 +436,18 @@ func TestGetUser(t *testing.T) { mysql := NewMysql(conf, log) mysql.db = db - query := "SELECT User, Host FROM mysql.user" - columns := []string{"User", "Host"} + query := "SELECT User, Host, Super_priv FROM mysql.user" + columns := []string{"User", "Host", "Super_priv"} want := []model.MysqlUser{ {User: "user1", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "N"}, {User: "root", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "Y"}, } - mockRows := sqlmock.NewRows(columns).AddRow("user1", "localhost").AddRow("root", "localhost") + + mockRows := sqlmock.NewRows(columns).AddRow("user1", "localhost", "N").AddRow("root", "localhost", "Y") mock.ExpectQuery(query).WillReturnRows(mockRows) got, err := mysql.GetUser() diff --git a/src/server/rpc_user_test.go b/src/server/rpc_user_test.go index f66ac90..fed347c 100644 --- a/src/server/rpc_user_test.go +++ b/src/server/rpc_user_test.go @@ -106,9 +106,11 @@ func TestServerRPCUser(t *testing.T) { want1 := []model.MysqlUser{ {User: "user1", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "N"}, {User: "root", - Host: "localhost"}, + Host: "localhost", + SuperPriv: "Y"}, } got1 := rsp.Users assert.Equal(t, want1, got1)