fix: 解决删除数据库过程中导致的锁库

pull/93/head
zhengkunwang223 2022-12-27 16:30:25 +08:00 committed by zhengkunwang223
parent aa9f6f0f2b
commit f8409fbfaf
5 changed files with 13 additions and 65 deletions

View File

@ -227,10 +227,13 @@ func (b *BaseApi) DeleteMysql(c *gin.Context) {
return return
} }
if err := mysqlService.Delete(req); err != nil { tx, ctx := helper.GetTxAndContext()
if err := mysqlService.Delete(ctx, req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
tx.Rollback()
return return
} }
tx.Commit()
helper.SuccessWithData(c, nil) helper.SuccessWithData(c, nil)
} }

View File

@ -20,7 +20,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
@ -36,56 +35,7 @@ var (
Delete DatabaseOp = "delete" Delete DatabaseOp = "delete"
) )
func execDockerCommand(database model.DatabaseMysql, dbInstall model.AppInstall, op DatabaseOp) error {
var auth dto.AuthParam
var dbConfig dto.AppDatabase
dbConfig.Password = database.Password
dbConfig.DbUser = database.Username
dbConfig.DbName = database.Name
_ = json.Unmarshal([]byte(dbInstall.Param), &auth)
execConfig := dto.ContainerExec{
ContainerName: dbInstall.ContainerName,
Auth: auth,
DbParam: dbConfig,
}
out, err := cmd.Exec(getSqlStr(dbInstall.Version, op, execConfig))
if err != nil {
return errors.New(out)
}
return nil
}
func getSqlStr(version string, operate DatabaseOp, exec dto.ContainerExec) string {
var str string
param := exec.DbParam
if strings.Contains(version, "5.7") {
if operate == Add {
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';\" -e \"FLUSH PRIVILEGES;\"",
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser, param.Password)
}
if operate == Delete {
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"drop database %s;\" -e \"drop user %s;\" ",
exec.ContainerName, exec.Auth.RootPassword, param.DbName, param.DbUser)
}
}
if strings.Contains(version, "8.0") {
if operate == Add {
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%';\" -e \"FLUSH PRIVILEGES;\"",
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser)
}
if operate == Delete {
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"drop database %s;\" -e \"drop user %s;\" ",
exec.ContainerName, exec.Auth.RootPassword, param.DbName, param.DbUser)
}
}
return str
}
func checkPort(key string, params map[string]interface{}) (int, error) { func checkPort(key string, params map[string]interface{}) (int, error) {
port, ok := params[key] port, ok := params[key]
if ok { if ok {
portN := int(math.Ceil(port.(float64))) portN := int(math.Ceil(port.(float64)))
@ -191,19 +141,15 @@ func deleteLink(ctx context.Context, install *model.AppInstall) error {
return nil return nil
} }
for _, re := range resources { for _, re := range resources {
mysqlService := NewIMysqlService()
if re.Key == "mysql" { if re.Key == "mysql" {
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId)) database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
if reflect.DeepEqual(database, model.DatabaseMysql{}) { if reflect.DeepEqual(database, model.DatabaseMysql{}) {
continue continue
} }
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByName(database.MysqlName)) if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
if err != nil { ID: database.ID,
return err }); err != nil {
}
if err := execDockerCommand(database, appInstall, Delete); err != nil {
return err
}
if err := mysqlRepo.Delete(ctx, commonRepo.WithByID(database.ID)); err != nil {
return err return err
} }
} }

View File

@ -44,7 +44,7 @@ type IMysqlService interface {
Recover(db dto.RecoverDB) error Recover(db dto.RecoverDB) error
DeleteCheck(id uint) ([]string, error) DeleteCheck(id uint) ([]string, error)
Delete(req dto.MysqlDBDelete) error Delete(ctx context.Context, req dto.MysqlDBDelete) error
LoadStatus() (*dto.MysqlStatus, error) LoadStatus() (*dto.MysqlStatus, error)
LoadVariables() (*dto.MysqlVariables, error) LoadVariables() (*dto.MysqlVariables, error)
LoadBaseInfo() (*dto.DBBaseInfo, error) LoadBaseInfo() (*dto.DBBaseInfo, error)
@ -256,7 +256,7 @@ func (u *MysqlService) DeleteCheck(id uint) ([]string, error) {
return appInUsed, nil return appInUsed, nil
} }
func (u *MysqlService) Delete(req dto.MysqlDBDelete) error { func (u *MysqlService) Delete(ctx context.Context, req dto.MysqlDBDelete) error {
app, err := appInstallRepo.LoadBaseInfo("mysql", "") app, err := appInstallRepo.LoadBaseInfo("mysql", "")
if err != nil && !req.ForceDelete { if err != nil && !req.ForceDelete {
return err return err
@ -288,9 +288,9 @@ func (u *MysqlService) Delete(req dto.MysqlDBDelete) error {
_ = os.RemoveAll(backupDir) _ = os.RemoveAll(backupDir)
} }
} }
_ = backupRepo.DeleteRecord(context.Background(), commonRepo.WithByType("database-mysql"), commonRepo.WithByName(app.Name), backupRepo.WithByDetailName(db.Name)) _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("database-mysql"), commonRepo.WithByName(app.Name), backupRepo.WithByDetailName(db.Name))
_ = mysqlRepo.Delete(context.Background(), commonRepo.WithByID(db.ID)) _ = mysqlRepo.Delete(ctx, commonRepo.WithByID(db.ID))
return nil return nil
} }

View File

@ -104,7 +104,6 @@ const handleParams = () => {
const pObj = p; const pObj = p;
pObj.prop = propStart.value + p.envKey; pObj.prop = propStart.value + p.envKey;
pObj.disabled = p.disabled; pObj.disabled = p.disabled;
console.log(pObj);
paramObjs.value.push(pObj); paramObjs.value.push(pObj);
if (p.default == 'random') { if (p.default == 'random') {
form[p.envKey] = getRandomStr(6); form[p.envKey] = getRandomStr(6);

View File

@ -6,7 +6,7 @@
width="30%" width="30%"
:before-close="handleClose" :before-close="handleClose"
> >
<div :key="key"> <div :key="key" v-loading="loading">
<el-form ref="deleteForm" label-position="left"> <el-form ref="deleteForm" label-position="left">
<el-form-item> <el-form-item>
<el-checkbox v-model="deleteReq.forceDelete" :label="$t('website.forceDelete')" /> <el-checkbox v-model="deleteReq.forceDelete" :label="$t('website.forceDelete')" />