From 0bb31f6caf179653d1341011eb3c74b8235104bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=B9=E5=87=B8=E6=9B=BC?= Date: Tue, 13 Jun 2023 10:29:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=97=B6=E8=AF=AF=E5=88=A0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=94=A8=E6=88=B7=E7=9A=84bug=20(#1358)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 凹凸曼 --- backend/app/service/database_mysql.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/app/service/database_mysql.go b/backend/app/service/database_mysql.go index 8ae11a7bd..e3ae5fae2 100644 --- a/backend/app/service/database_mysql.go +++ b/backend/app/service/database_mysql.go @@ -484,10 +484,11 @@ func (u *MysqlService) createUser(container, password, version string, req dto.M for _, user := range userlist { 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") { + handleCreateError(container, password, req.Name, userlist, false) return buserr.New(constant.ErrUserIsExist) } + handleCreateError(container, password, req.Name, userlist, true) return err } grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", req.Name, user) @@ -498,17 +499,19 @@ func (u *MysqlService) createUser(container, password, version string, req dto.M grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, req.Password) } if err := excSQL(container, password, grantStr); err != nil { - handleCreateError(container, password, req.Name, userlist) + handleCreateError(container, password, req.Name, userlist, true) return err } } 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)) - for _, user := range userlist { - if err := excSQL(contaienr, password, fmt.Sprintf("drop user if exists %s", user)); err != nil { - global.LOG.Errorf("drop user failed, err: %v", err) + if dropUser { + for _, user := range userlist { + if err := excSQL(contaienr, password, fmt.Sprintf("drop user if exists %s", user)); err != nil { + global.LOG.Errorf("drop user failed, err: %v", err) + } } } }