From fc206deddf799531e5140b94bac4f0531d7a9c30 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 7 Mar 2023 16:44:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=20mysql=20=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=9B=E5=BB=BA=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/database_mysql.go | 12 ++++++++++-- backend/constant/errs.go | 6 ++++++ backend/i18n/lang/en.yaml | 8 ++++++-- backend/i18n/lang/zh.yaml | 8 ++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/backend/app/service/database_mysql.go b/backend/app/service/database_mysql.go index adca23c30..99154a2ff 100644 --- a/backend/app/service/database_mysql.go +++ b/backend/app/service/database_mysql.go @@ -15,6 +15,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/model" + "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/common" @@ -92,12 +93,19 @@ func (u *MysqlService) Create(ctx context.Context, req dto.MysqlDBCreate) (*mode return nil, errors.WithMessage(constant.ErrStructTransform, err.Error()) } - createSql := fmt.Sprintf("create database if not exists `%s` default character set %s collate %s", req.Name, req.Format, formatMap[req.Format]) + createSql := fmt.Sprintf("create database `%s` default character set %s collate %s", req.Name, req.Format, formatMap[req.Format]) if err := excuteSql(app.ContainerName, app.Password, createSql); err != nil { + if strings.Contains(err.Error(), "ERROR 1007") { + return nil, buserr.New(constant.ErrDatabaseIsExist) + } return nil, err } tmpPermission := req.Permission - if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create user if not exists '%s'@'%s' identified by '%s';", req.Username, tmpPermission, req.Password)); err != nil { + if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create user '%s'@'%s' identified by '%s';", req.Username, tmpPermission, req.Password)); err != nil { + _ = excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop database `%s`", req.Name)) + if strings.Contains(err.Error(), "ERROR 1396") { + return nil, buserr.New(constant.ErrUserIsExist) + } return nil, err } grantStr := fmt.Sprintf("grant all privileges on `%s`.* to '%s'@'%s'", req.Name, req.Username, tmpPermission) diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 93403638d..ae3a7ce9b 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -80,3 +80,9 @@ var ( var ( ErrPathNotFound = "ErrPathNotFound" ) + +//mysql +var ( + ErrUserIsExist = "ErrUserIsExist" + ErrDatabaseIsExist = "ErrDatabaseIsExist" +) diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 3005cc621..2a609a7ec 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -35,10 +35,14 @@ ErrPathNotFound: "Path is not found" ErrDomainIsExist: "Domain is already exist" ErrAliasIsExist: "Alias is already exist" ErrAppDelete: 'Other Website use this App' -ErrGroupIsUsed: 'The group has been associated with a website and cannot be deleted' +ErrGroupIsUsed: 'The group is in use and cannot be deleted' #ssl ErrSSLCannotDelete: "The certificate is being used by the website and cannot be removed" ErrAccountCannotDelete: "The certificate associated with the account cannot be deleted" ErrSSLApply: "The certificate continues to be signed successfully, but openresty reload fails, please check the configuration!" -ErrEmailIsExist: 'Email is already exist' \ No newline at end of file +ErrEmailIsExist: 'Email is already exist' + +#mysql +ErrUserIsExist: "The current user already exists. Please enter a new user" +ErrDatabaseIsExist: "The current database already exists. Please enter a new database" \ No newline at end of file diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index defa4dc50..721d2db89 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -35,10 +35,14 @@ ErrPathNotFound: "目录不存在" ErrDomainIsExist: "域名已存在" ErrAliasIsExist: "代号已存在" ErrAppDelete: '其他网站使用此应用,无法删除' -ErrGroupIsUsed: '分组已经关联网站,无法删除' +ErrGroupIsUsed: '分组正在使用中,无法删除' #ssl ErrSSLCannotDelete: "证书正在被网站使用,无法删除" ErrAccountCannotDelete: "账号关联证书,无法删除" ErrSSLApply: "证书续签成功,openresty reload失败,请检查配置!" -ErrEmailIsExist: '邮箱已存在' \ No newline at end of file +ErrEmailIsExist: '邮箱已存在' + +#mysql +ErrUserIsExist: "当前用户已存在,请重新输入" +ErrDatabaseIsExist: "当前数据库已存在,请重新输入" \ No newline at end of file