diff --git a/backend/app/api/v1/database_mysql.go b/backend/app/api/v1/database_mysql.go index 6370df3ee..0a40385a0 100644 --- a/backend/app/api/v1/database_mysql.go +++ b/backend/app/api/v1/database_mysql.go @@ -227,10 +227,13 @@ func (b *BaseApi) DeleteMysql(c *gin.Context) { 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) + tx.Rollback() return } + tx.Commit() helper.SuccessWithData(c, nil) } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 1ae5c6e96..fa2b2cb7a 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -20,7 +20,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "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/compose" "github.com/1Panel-dev/1Panel/backend/utils/files" @@ -36,56 +35,7 @@ var ( 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) { - port, ok := params[key] if ok { portN := int(math.Ceil(port.(float64))) @@ -191,19 +141,15 @@ func deleteLink(ctx context.Context, install *model.AppInstall) error { return nil } for _, re := range resources { + mysqlService := NewIMysqlService() if re.Key == "mysql" { database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId)) if reflect.DeepEqual(database, model.DatabaseMysql{}) { continue } - appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByName(database.MysqlName)) - if err != nil { - return err - } - if err := execDockerCommand(database, appInstall, Delete); err != nil { - return err - } - if err := mysqlRepo.Delete(ctx, commonRepo.WithByID(database.ID)); err != nil { + if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{ + ID: database.ID, + }); err != nil { return err } } diff --git a/backend/app/service/database_mysql.go b/backend/app/service/database_mysql.go index baac7ce85..8614ccf4a 100644 --- a/backend/app/service/database_mysql.go +++ b/backend/app/service/database_mysql.go @@ -44,7 +44,7 @@ type IMysqlService interface { Recover(db dto.RecoverDB) error DeleteCheck(id uint) ([]string, error) - Delete(req dto.MysqlDBDelete) error + Delete(ctx context.Context, req dto.MysqlDBDelete) error LoadStatus() (*dto.MysqlStatus, error) LoadVariables() (*dto.MysqlVariables, error) LoadBaseInfo() (*dto.DBBaseInfo, error) @@ -256,7 +256,7 @@ func (u *MysqlService) DeleteCheck(id uint) ([]string, error) { 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", "") if err != nil && !req.ForceDelete { return err @@ -288,9 +288,9 @@ func (u *MysqlService) Delete(req dto.MysqlDBDelete) error { _ = 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 } diff --git a/frontend/src/views/app-store/detail/params/index.vue b/frontend/src/views/app-store/detail/params/index.vue index 39c342dda..fc61695e8 100644 --- a/frontend/src/views/app-store/detail/params/index.vue +++ b/frontend/src/views/app-store/detail/params/index.vue @@ -104,7 +104,6 @@ const handleParams = () => { const pObj = p; pObj.prop = propStart.value + p.envKey; pObj.disabled = p.disabled; - console.log(pObj); paramObjs.value.push(pObj); if (p.default == 'random') { form[p.envKey] = getRandomStr(6); diff --git a/frontend/src/views/website/website/delete/index.vue b/frontend/src/views/website/website/delete/index.vue index 9325accb6..f650f44cd 100644 --- a/frontend/src/views/website/website/delete/index.vue +++ b/frontend/src/views/website/website/delete/index.vue @@ -6,7 +6,7 @@ width="30%" :before-close="handleClose" > -
+