Skip to content

Commit

Permalink
validate db_service data before deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdongcheng authored and rocky114 committed Nov 30, 2023
1 parent de72dd1 commit 1ddc585
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Masterminds/semver/v3 v3.1.1
github.com/actiontech/dms v0.0.0-20231019085256-6c3ffc8b3489
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62
github.com/actiontech/mybatis-mapper-2-sql v0.4.0
github.com/agiledragon/gomonkey v2.0.2+incompatible
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdc
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/actiontech/dms v0.0.0-20231019085256-6c3ffc8b3489 h1:K+fg7bPud+sMFyjbZrxNxRrjh++6lLlRayMb7Ylzlws=
github.com/actiontech/dms v0.0.0-20231019085256-6c3ffc8b3489/go.mod h1:NjDg7X3pKEQCaXSSphdwEGTh+D6nvB19OnKj/AF2CX0=
github.com/actiontech/dms v0.0.0-20231129060158-ff9d70a10557 h1:eeJqVzkX5X5gMfaN67X2wCFbqoVfbDgIsoSYeJJfCzs=
github.com/actiontech/dms v0.0.0-20231129060158-ff9d70a10557/go.mod h1:jG8WjxiSBcUccweOqRPONDx95j88FwZytoa1/PI8I44=
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0 h1:BZlpGNWg6aJhFcWZc17VHlOLWHvuxJZIKFY5ePlY5ZQ=
github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0/go.mod h1:jG8WjxiSBcUccweOqRPONDx95j88FwZytoa1/PI8I44=
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62 h1:JM7WnLzlvXOGE90KKd+aigi+qUDS+U5dLwQMNpTKZxE=
github.com/actiontech/java-sql-extractor v0.0.0-20231103015812-cdd5fc040f62/go.mod h1:adDZHhAf2LRMx2h0JzofPXn12x2XlyQjVE116KXquwo=
github.com/actiontech/mybatis-mapper-2-sql v0.4.0 h1:FSzK3qnnD9/JjOUJMRcctGxQjK7cg1M01yRMq59TSZg=
Expand Down
37 changes: 29 additions & 8 deletions sqle/api/controller/v1/dms_handle.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package v1

import (
"context"
"fmt"
"net/http"
"strconv"

baseV1 "github.com/actiontech/dms/pkg/dms-common/api/base/v1"
dmsV1 "github.com/actiontech/dms/pkg/dms-common/api/dms/v1"
"github.com/actiontech/dms/pkg/dms-common/dmsobject"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/common"
"github.com/labstack/echo/v4"
)

func init() {
dmsobject.InitOperateHandlers([]dmsobject.OperationHanler{AfterDeleteProject{}, BeforeDeleteProject{}, BeforeArvhiveProject{}, AfterCreateProject{}})
dmsobject.InitOperateHandlers([]dmsobject.OperationHandler{
AfterDeleteProject{},
BeforeDeleteProject{},
BeforeArchiveProject{},
AfterCreateProject{},
BeforeDeleteDbService{},
})
}

// 内部接口
Expand All @@ -21,23 +30,35 @@ func OperateDataResourceHandle(c echo.Context) error {
if err := controller.BindAndValidateReq(c, req); err != nil {
return controller.JSONBaseErrorReq(c, err)
}
h, ok := dmsobject.OperateHandlers[fmt.Sprintf("%s_%s_%s", req.OperationTiming, req.OperationType, req.DataResourceType)]
if ok {
err := h.Hanle(c.Request().Context(), "", req.DataResourceUid)
if err != nil {
return c.JSON(http.StatusOK, dmsV1.OperateDataResourceHandleReply{GenericResp: baseV1.GenericResp{Code: http.StatusBadRequest, Message: err.Error()}})
}
h := dmsobject.GetOperateHandle(fmt.Sprintf("%s_%s_%s", req.OperationTiming, req.OperationType, req.DataResourceType))

if err := h.Handle(c.Request().Context(), "", req.DataResourceUid); err != nil {
return c.JSON(http.StatusOK, dmsV1.OperateDataResourceHandleReply{GenericResp: baseV1.GenericResp{Code: http.StatusBadRequest, Message: err.Error()}})
}

return c.JSON(http.StatusOK, dmsV1.OperateDataResourceHandleReply{GenericResp: baseV1.GenericResp{Message: "OK"}})
}

type AfterDeleteProject struct {
}

type BeforeArvhiveProject struct {
type BeforeArchiveProject struct {
}

type BeforeDeleteProject struct {
}

type AfterCreateProject struct {
}

type BeforeDeleteDbService struct {
}

func (h BeforeDeleteDbService) Handle(ctx context.Context, currentUserId string, instanceIdStr string) error {
instanceId, err := strconv.ParseInt(instanceIdStr, 10, 64)
if err != nil {
return err
}

return common.CheckDeleteInstance(instanceId)
}
8 changes: 4 additions & 4 deletions sqle/api/controller/v1/dms_handle_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"context"
)

func (h BeforeArvhiveProject) Hanle(ctx context.Context, currentUserId string, dataResourceId string) error {
func (h BeforeArchiveProject) Handle(ctx context.Context, currentUserId string, dataResourceId string) error {
return nil
}
func (h AfterDeleteProject) Hanle(ctx context.Context, currentUserId string, dataResourceId string) error {
func (h AfterDeleteProject) Handle(ctx context.Context, currentUserId string, dataResourceId string) error {
return nil
}
func (h BeforeDeleteProject) Hanle(ctx context.Context, currentUserId string, dataResourceId string) error {
func (h BeforeDeleteProject) Handle(ctx context.Context, currentUserId string, dataResourceId string) error {
return nil
}
func (h AfterCreateProject) Hanle(ctx context.Context, currentUserId string, dataResourceId string) error {
func (h AfterCreateProject) Handle(ctx context.Context, currentUserId string, dataResourceId string) error {
return nil
}
5 changes: 4 additions & 1 deletion sqle/api/controller/v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,13 @@ func getTaskById(ctx context.Context, taskId string) (*model.Task, error) {
return nil, errors.NewTaskNoExistOrNoAccessErr()
}

instance, _, err := dms.GetInstancesById(ctx, task.InstanceId)
instance, exist, err := dms.GetInstancesById(ctx, task.InstanceId)
if err != nil {
return nil, err
}
if !exist {
return nil, errors.NewInstanceNoExistErr()
}
task.Instance = instance

return task, nil
Expand Down
2 changes: 1 addition & 1 deletion sqle/common/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CheckInstanceIsConnectable(instance *model.Instance) error {
return nil
}

func CheckDeleteInstance(instanceId uint) error {
func CheckDeleteInstance(instanceId int64) error {
s := model.GetStorage()

isUnFinished, err := s.IsWorkflowUnFinishedByInstanceId(instanceId)
Expand Down
4 changes: 4 additions & 0 deletions sqle/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ func NewNotSupportGetTaskAnalysisDataErr() error {
func NewTaskNoExistOrNoAccessErr() error {
return New(DataNotExist, fmt.Errorf("task is not exist or you can't access it"))
}

func NewInstanceNoExistErr() error {
return New(DataNotExist, fmt.Errorf("instance is not exist"))
}
2 changes: 1 addition & 1 deletion sqle/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ func (s *Storage) GetWorkflowBySubject(subject string) (*Workflow, bool, error)
return workflow, true, errors.New(errors.ConnectStorageError, err)
}

func (s *Storage) IsWorkflowUnFinishedByInstanceId(instanceId uint) (bool, error) {
func (s *Storage) IsWorkflowUnFinishedByInstanceId(instanceId int64) (bool, error) {
count := 0
err := s.db.Table("workflow_records").
Joins("LEFT JOIN workflow_instance_records ON workflow_records.id = workflow_instance_records.workflow_record_id").
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ github.com/StackExchange/wmi
# github.com/acomagu/bufpipe v1.0.4
## explicit; go 1.12
github.com/acomagu/bufpipe
# github.com/actiontech/dms v0.0.0-20231019085256-6c3ffc8b3489
# github.com/actiontech/dms v0.0.0-20231129083427-a97a77db08e0
## explicit; go 1.19
github.com/actiontech/dms/pkg/dms-common/api/base/v1
github.com/actiontech/dms/pkg/dms-common/api/dms/v1
Expand Down

0 comments on commit 1ddc585

Please sign in to comment.