Skip to content

Commit cef80ce

Browse files
zhexuanyshenli
authored andcommitted
[cherrypick-2.0] executor: fix drop user bug (#6624) (#7014)
1 parent 43ab800 commit cef80ce

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

executor/simple.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,46 @@ func (e *SimpleExec) executeDropUser(s *ast.DropUserStmt) error {
230230
}
231231
continue
232232
}
233+
234+
// begin a transaction to delete a user.
235+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "begin"); err != nil {
236+
return errors.Trace(err)
237+
}
233238
sql := fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.UserTable, user.Hostname, user.Username)
234-
_, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql)
235-
if err != nil {
239+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil {
240+
failedUsers = append(failedUsers, user.String())
241+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "rollback"); err != nil {
242+
return errors.Trace(err)
243+
}
244+
continue
245+
}
246+
247+
// delete privileges from mysql.db
248+
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.DBTable, user.Hostname, user.Username)
249+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil {
250+
failedUsers = append(failedUsers, user.String())
251+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "rollback"); err != nil {
252+
return errors.Trace(err)
253+
}
254+
continue
255+
}
256+
257+
// delete privileges from mysql.tables_priv
258+
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.TablePrivTable, user.Hostname, user.Username)
259+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil {
260+
failedUsers = append(failedUsers, user.String())
261+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "rollback"); err != nil {
262+
return errors.Trace(err)
263+
}
264+
continue
265+
}
266+
267+
//TODO: need delete columns_priv once we implement columns_priv functionality.
268+
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "commit"); err != nil {
236269
failedUsers = append(failedUsers, user.String())
237270
}
238271
}
239272
if len(failedUsers) > 0 {
240-
// Commit the transaction even if we returns error
241-
err := e.ctx.Txn().Commit(sessionctx.SetConnID2Ctx(context.Background(), e.ctx))
242-
if err != nil {
243-
return errors.Trace(err)
244-
}
245273
errMsg := "Operation DROP USER failed for " + strings.Join(failedUsers, ",")
246274
return terror.ClassExecutor.New(CodeCannotUser, errMsg)
247275
}

executor/simple_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,25 @@ func (s *testSuite) TestUser(c *C) {
178178
tk.MustExec(createUserSQL)
179179
dropUserSQL = `DROP USER 'test1'@'localhost';`
180180
tk.MustExec(dropUserSQL)
181+
tk.MustQuery("select * from mysql.db").Check(testkit.Rows(
182+
"localhost test testDB Y Y Y Y Y Y Y N Y Y N N N N N N Y N N",
183+
"localhost test testDB1 Y Y Y Y Y Y Y N Y Y N N N N N N Y N N] [% dddb_% dduser Y Y Y Y Y Y Y N Y Y N N N N N N Y N N",
184+
"% test test Y N N N N N N N N N N N N N N N N N N",
185+
"localhost test testDBRevoke N N N N N N N N N N N N N N N N N N N",
186+
))
187+
188+
// Test drop user meet error
189+
_, err = tk.Exec(dropUserSQL)
190+
c.Assert(terror.ErrorEqual(err, terror.ClassExecutor.New(executor.CodeCannotUser, "")), IsTrue)
191+
192+
createUserSQL = `CREATE USER 'test1'@'localhost'`
193+
tk.MustExec(createUserSQL)
194+
createUserSQL = `CREATE USER 'test2'@'localhost'`
195+
tk.MustExec(createUserSQL)
196+
197+
dropUserSQL = `DROP USER 'test1'@'localhost', 'test2'@'localhost', 'test3'@'localhost';`
198+
_, err = tk.Exec(dropUserSQL)
199+
c.Assert(terror.ErrorEqual(err, terror.ClassExecutor.New(executor.CodeCannotUser, "")), IsTrue)
181200
}
182201

183202
func (s *testSuite) TestSetPwd(c *C) {

0 commit comments

Comments
 (0)