fix: 修复创建数据库时误删数据库用户的bug (#1358)

Co-authored-by: 凹凸曼 <xx@xx>
pull/1365/head
凹凸曼 1 year ago committed by GitHub
parent 03c8e4d3cb
commit 0bb31f6caf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -484,10 +484,11 @@ func (u *MysqlService) createUser(container, password, version string, req dto.M
for _, user := range userlist { for _, user := range userlist {
if err := excSQL(container, password, fmt.Sprintf("create user %s identified by '%s';", user, req.Password)); err != nil { if err := excSQL(container, password, fmt.Sprintf("create user %s identified by '%s';", user, req.Password)); err != nil {
handleCreateError(container, password, req.Name, userlist)
if strings.Contains(err.Error(), "ERROR 1396") { if strings.Contains(err.Error(), "ERROR 1396") {
handleCreateError(container, password, req.Name, userlist, false)
return buserr.New(constant.ErrUserIsExist) return buserr.New(constant.ErrUserIsExist)
} }
handleCreateError(container, password, req.Name, userlist, true)
return err return err
} }
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", req.Name, user) grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", req.Name, user)
@ -498,19 +499,21 @@ func (u *MysqlService) createUser(container, password, version string, req dto.M
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, req.Password) grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, req.Password)
} }
if err := excSQL(container, password, grantStr); err != nil { if err := excSQL(container, password, grantStr); err != nil {
handleCreateError(container, password, req.Name, userlist) handleCreateError(container, password, req.Name, userlist, true)
return err return err
} }
} }
return nil return nil
} }
func handleCreateError(contaienr, password, dbName string, userlist []string) { func handleCreateError(contaienr, password, dbName string, userlist []string, dropUser bool) {
_ = excSQL(contaienr, password, fmt.Sprintf("drop database `%s`", dbName)) _ = excSQL(contaienr, password, fmt.Sprintf("drop database `%s`", dbName))
if dropUser {
for _, user := range userlist { for _, user := range userlist {
if err := excSQL(contaienr, password, fmt.Sprintf("drop user if exists %s", user)); err != nil { if err := excSQL(contaienr, password, fmt.Sprintf("drop user if exists %s", user)); err != nil {
global.LOG.Errorf("drop user failed, err: %v", err) global.LOG.Errorf("drop user failed, err: %v", err)
} }
} }
}
} }
func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) { func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) {

Loading…
Cancel
Save