Skip to content

Commit

Permalink
executor: fix drop role not work as expected in the show result. (#29667
Browse files Browse the repository at this point in the history
)
  • Loading branch information
7yyo authored Nov 15, 2021
1 parent 4324e4e commit 8f7ed14
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error {
}
_, err := internalSession.(sqlexec.SQLExecutor).ExecuteInternal(ctx,
`INSERT INTO %n.%n (Host, User, authentication_string, plugin) VALUES (%?, %?, %?, %?);`,
mysql.SystemDB, mysql.UserTable, user.User.Hostname, user.User.Username, pwd, authPlugin)
mysql.SystemDB, mysql.UserTable, strings.ToLower(user.User.Hostname), user.User.Username, pwd, authPlugin)
if err != nil {
return err
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func (e *GrantExec) grantGlobalLevel(priv *ast.PrivElem, user *ast.UserSpec, int
if err != nil {
return err
}
sqlexec.MustFormatSQL(sql, ` WHERE User=%? AND Host=%?`, user.User.Username, user.User.Hostname)
sqlexec.MustFormatSQL(sql, ` WHERE User=%? AND Host=%?`, user.User.Username, strings.ToLower(user.User.Hostname))

_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(context.Background(), sql.String())
return err
Expand Down
2 changes: 1 addition & 1 deletion executor/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (e *RevokeExec) revokeGlobalPriv(internalSession sessionctx.Context, priv *
if err != nil {
return err
}
sqlexec.MustFormatSQL(sql, " WHERE User=%? AND Host=%?", user, host)
sqlexec.MustFormatSQL(sql, " WHERE User=%? AND Host=%?", user, strings.ToLower(host))

_, err = internalSession.(sqlexec.SQLExecutor).ExecuteInternal(context.Background(), sql.String())
return err
Expand Down
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ func (e *ShowExec) fetchShowCreateUser(ctx context.Context) error {

exec := e.ctx.(sqlexec.RestrictedSQLExecutor)

stmt, err := exec.ParseWithParams(ctx, `SELECT plugin FROM %n.%n WHERE User=%? AND Host=%?`, mysql.SystemDB, mysql.UserTable, userName, hostName)
stmt, err := exec.ParseWithParams(ctx, `SELECT plugin FROM %n.%n WHERE User=%? AND Host=%?`, mysql.SystemDB, mysql.UserTable, userName, strings.ToLower(hostName))
if err != nil {
return errors.Trace(err)
}
Expand Down
8 changes: 4 additions & 4 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func (e *SimpleExec) executeAlterUser(ctx context.Context, s *ast.AlterUserStmt)
}
stmt, err := exec.ParseWithParams(ctx,
`UPDATE %n.%n SET authentication_string=%?, plugin=%? WHERE Host=%? and User=%?;`,
mysql.SystemDB, mysql.UserTable, pwd, spec.AuthOpt.AuthPlugin, spec.User.Hostname, spec.User.Username,
mysql.SystemDB, mysql.UserTable, pwd, spec.AuthOpt.AuthPlugin, strings.ToLower(spec.User.Hostname), spec.User.Username,
)
if err != nil {
return err
Expand Down Expand Up @@ -1158,7 +1158,7 @@ func renameUserHostInSystemTable(sqlExecutor sqlexec.SQLExecutor, tableName, use
sqlexec.MustFormatSQL(sql, `UPDATE %n.%n SET %n = %?, %n = %? WHERE %n = %? and %n = %?;`,
mysql.SystemDB, tableName,
usernameColumn, users.NewUser.Username, hostColumn, strings.ToLower(users.NewUser.Hostname),
usernameColumn, users.OldUser.Username, hostColumn, users.OldUser.Hostname)
usernameColumn, users.OldUser.Username, hostColumn, strings.ToLower(users.OldUser.Hostname))
_, err := sqlExecutor.ExecuteInternal(context.TODO(), sql.String())
return err
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ func (e *SimpleExec) executeDropUser(ctx context.Context, s *ast.DropUserStmt) e

// begin a transaction to delete a user.
sql.Reset()
sqlexec.MustFormatSQL(sql, `DELETE FROM %n.%n WHERE Host = %? and User = %?;`, mysql.SystemDB, mysql.UserTable, user.Hostname, user.Username)
sqlexec.MustFormatSQL(sql, `DELETE FROM %n.%n WHERE Host = %? and User = %?;`, mysql.SystemDB, mysql.UserTable, strings.ToLower(user.Hostname), user.Username)
if _, err = sqlExecutor.ExecuteInternal(context.TODO(), sql.String()); err != nil {
failedUsers = append(failedUsers, user.String())
break
Expand Down Expand Up @@ -1417,7 +1417,7 @@ func (e *SimpleExec) executeSetPwd(ctx context.Context, s *ast.SetPwdStmt) error

// update mysql.user
exec := e.ctx.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParams(ctx, `UPDATE %n.%n SET authentication_string=%? WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, pwd, u, h)
stmt, err := exec.ParseWithParams(ctx, `UPDATE %n.%n SET authentication_string=%? WHERE User=%? AND Host=%?;`, mysql.SystemDB, mysql.UserTable, pwd, u, strings.ToLower(h))
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ func (s *testSuite7) TestUser(c *C) {
Check(testkit.Rows("engineering india"))
tk.MustQuery("select user,host from mysql.user where user='engineering' and host = 'us'").
Check(testkit.Rows("engineering us"))

tk.MustExec("drop role engineering@INDIA;")
tk.MustExec("drop role engineering@US;")

tk.MustQuery("select user from mysql.user where user='engineering' and host = 'india'").Check(testkit.Rows())
tk.MustQuery("select user from mysql.user where user='engineering' and host = 'us'").Check(testkit.Rows())
}

func (s *testSuite3) TestSetPwd(c *C) {
Expand Down

0 comments on commit 8f7ed14

Please sign in to comment.