Skip to content

Commit

Permalink
Merge pull request #2737 from actiontech/1956-issue
Browse files Browse the repository at this point in the history
api definition: for backup and recovery
  • Loading branch information
iwanghc authored Nov 7, 2024
2 parents b049eab + 755054d commit 39357ce
Show file tree
Hide file tree
Showing 7 changed files with 913 additions and 37 deletions.
92 changes: 92 additions & 0 deletions sqle/api/controller/v1/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package v1

import "github.com/labstack/echo/v4"

type UpdateSqlBackupStrategyReq struct {
Strategy string `json:"strategy" enum:"none,manual,reverse_sql,origin_row"`
}

// UpdateSqlBackupStrategy
// @Summary 更新单条SQL的备份策略
// @Description update back up strategy for one sql in workflow
// @Tags workflow
// @Accept json
// @Produce json
// @Id UpdateSqlBackupStrategyV1
// @Security ApiKeyAuth
// @Param task_id path string true "task id"
// @Param sql_id path string true "sql id"
// @Param strategy body v1.UpdateSqlBackupStrategyReq true "update back up strategy for one sql in workflow"
// @Success 200 {object} controller.BaseRes
// @router /v1/tasks/audits/{task_id}/sqls/{sql_id}/ [patch]
func UpdateSqlBackupStrategy(c echo.Context) error {
return nil
}

type UpdateTaskBackupStrategyReq struct {
Strategy string `json:"strategy" enum:"none,manual,reverse_sql,origin_row"`
}

// UpdateTaskBackupStrategy
// @Summary 更新工单中数据源对应所有SQL的备份策略
// @Description update back up strategy for all sqls in task
// @Tags workflow
// @Accept json
// @Produce json
// @Id UpdateTaskBackupStrategyV1
// @Security ApiKeyAuth
// @Param task_id path string true "task id"
// @Param strategy body v1.UpdateTaskBackupStrategyReq true "update back up strategy for sqls in workflow"
// @Success 200 {object} controller.BaseRes
// @router /v1/tasks/audits/{task_id}/ [patch]
func UpdateTaskBackupStrategy(c echo.Context) error {
return nil
}

// @Summary 下载工单中的SQL备份
// @Description download SQL back up file for the audit task
// @Tags task
// @Id downloadBackupFileV1
// @Security ApiKeyAuth
// @Param workflow_id path string true "workflow id"
// @Param project_name path string true "project name"
// @Param task_id path string true "task id"
// @Success 200 file 1 "sql file"
// @router /v1/projects/{project_name}/workflows/{workflow_id}/tasks/{task_id}/backup_files/download [get]
func DownloadSqlBackupFile(c echo.Context) error {
return nil
}

type BackupSqlListReq struct {
FilterInstanceId string `json:"filter_instance_id" query:"filter_instance_id"`
FilterExecStatus string `json:"filter_exec_status" query:"filter_exec_status"`
PageIndex uint32 `json:"page_index" query:"page_index" valid:"required"`
PageSize uint32 `json:"page_size" query:"page_size" valid:"required"`
}

type BackupSqlListRes struct {
ExecOrder uint `json:"exec_order"`
ExecSqlID uint `json:"exec_sql_id"`
OriginSQL string `json:"origin_sql"`
BackupSqls []string `json:"backup_sqls"`
BackupStrategy string `json:"backup_strategy" enum:"none,manual,reverse_sql,origin_row"`
InstanceName string `json:"instance_name"`
InstanceId string `json:"instance_id "`
ExecStatus string `json:"exec_status"`
Description string `json:"description"`
}

// @Summary 获取工单下所有回滚SQL的列表
// @Description get backup sql list
// @Tags workflow
// @Id GetBackupSqlListV1
// @Security ApiKeyAuth
// @Param filter_exec_status query string false "filter: exec status of task sql" Enums(initialized,doing,succeeded,failed,manually_executed,terminating,terminate_succeeded,terminate_failed)
// @Param filter_instance_id query uint false "filter: instance id in workflow"
// @Param page_index query string true "page index"
// @Param page_size query string true "page size"
// @Success 200 {object} v1.BackupSqlListRes
// @router /v1/projects/{project_name}/workflows/{workflow_id}/backup_sqls [get]
func GetBackupSqlList(c echo.Context) error {
return nil
}
42 changes: 25 additions & 17 deletions sqle/api/controller/v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type CreateAuditTaskReqV1 struct {
InstanceSchema string `json:"instance_schema" form:"instance_schema" example:"db1"`
Sql string `json:"sql" form:"sql" example:"alter table tb1 drop columns c1"`
ExecMode string `json:"exec_mode" form:"exec_mode" enums:"sql_file,sqls"`
EnableBackup bool `json:"enable_backup" form:"enable_backup"`
FileOrderMethod string `json:"file_order_method" form:"file_order_method"`
}

Expand All @@ -49,20 +50,22 @@ type GetAuditTaskResV1 struct {
}

type AuditTaskResV1 struct {
Id uint `json:"task_id"`
InstanceName string `json:"instance_name"`
InstanceDbType string `json:"instance_db_type"`
InstanceSchema string `json:"instance_schema" example:"db1"`
AuditLevel string `json:"audit_level" enums:"normal,notice,warn,error,"`
Score int32 `json:"score"`
PassRate float64 `json:"pass_rate"`
Status string `json:"status" enums:"initialized,audited,executing,exec_success,exec_failed,manually_executed"`
SQLSource string `json:"sql_source" enums:"form_data,sql_file,mybatis_xml_file,audit_plan,zip_file,git_repository"`
ExecStartTime *time.Time `json:"exec_start_time,omitempty"`
ExecEndTime *time.Time `json:"exec_end_time,omitempty"`
FileOrderMethod string `json:"file_order_method,omitempty"`
ExecMode string `json:"exec_mode,omitempty"`
AuditFiles []AuditFileResp `json:"audit_files,omitempty"`
Id uint `json:"task_id"`
InstanceName string `json:"instance_name"`
InstanceDbType string `json:"instance_db_type"`
InstanceSchema string `json:"instance_schema" example:"db1"`
AuditLevel string `json:"audit_level" enums:"normal,notice,warn,error,"`
Score int32 `json:"score"`
PassRate float64 `json:"pass_rate"`
Status string `json:"status" enums:"initialized,audited,executing,exec_success,exec_failed,manually_executed"`
SQLSource string `json:"sql_source" enums:"form_data,sql_file,mybatis_xml_file,audit_plan,zip_file,git_repository"`
ExecStartTime *time.Time `json:"exec_start_time,omitempty"`
ExecEndTime *time.Time `json:"exec_end_time,omitempty"`
FileOrderMethod string `json:"file_order_method,omitempty"`
ExecMode string `json:"exec_mode,omitempty"`
EnableBackup bool `json:"enable_backup"`
BackupConflictWithInstance bool `json:"backup_conflict_with_instance"`
AuditFiles []AuditFileResp `json:"audit_files,omitempty"`
}

type AuditFileResp struct {
Expand Down Expand Up @@ -285,6 +288,7 @@ func getFileHeaderFromContext(c echo.Context) (fileHeader *multipart.FileHeader,
// @Param project_name path string true "project name"
// @Param instance_name formData string true "instance name"
// @Param instance_schema formData string false "schema of instance"
// @Param enable_backup formData bool false "enable backup"
// @Param sql formData string false "sqls for audit"
// @Param input_sql_file formData file false "input SQL file"
// @Param input_mybatis_xml_file formData file false "input mybatis XML file"
Expand All @@ -295,6 +299,7 @@ func getFileHeaderFromContext(c echo.Context) (fileHeader *multipart.FileHeader,
// @Success 200 {object} v1.GetAuditTaskResV1
// @router /v1/projects/{project_name}/tasks/audits [post]
func CreateAndAuditTask(c echo.Context) error {
// TODO 不同SQL模式审核增加备份配置
req := new(CreateAuditTaskReqV1)
if err := controller.BindAndValidateReq(c, req); err != nil {
return err
Expand Down Expand Up @@ -926,8 +931,9 @@ func CreateAuditTasksGroupV1(c echo.Context) error {
}

type AuditTaskGroupReqV1 struct {
TaskGroupId uint `json:"task_group_id" form:"task_group_id" valid:"required"`
Sql string `json:"sql" form:"sql" example:"alter table tb1 drop columns c1"`
TaskGroupId uint `json:"task_group_id" form:"task_group_id" valid:"required"`
Sql string `json:"sql" form:"sql" example:"alter table tb1 drop columns c1"`
EnableBackup bool `json:"enable_backup" form:"enable_backup"`
}

type AuditTaskGroupRes struct {
Expand All @@ -954,13 +960,15 @@ type AuditTaskGroupResV1 struct {
// @Security ApiKeyAuth
// @Param task_group_id formData uint true "group id of tasks"
// @Param sql formData string false "sqls for audit"
// @Param enable_backup formData bool false "enable backup"
// @Param file_order_method formData string false "file order method"
// @Param input_sql_file formData file false "input SQL file"
// @Param input_mybatis_xml_file formData file false "input mybatis XML file"
// @Param input_zip_file formData file false "input ZIP file"
// @Success 200 {object} v1.AuditTaskGroupResV1
// @router /v1/task_groups/audit [post]
func AuditTaskGroupV1(c echo.Context) error {
func AuditTaskGroupV1(c echo.Context) error {
// TODO 单数据源审核,以及多数据源相同SQL模式审核,增加备份配置
req := new(AuditTaskGroupReqV1)
if err := controller.BindAndValidateReq(c, req); err != nil {
return err
Expand Down
27 changes: 15 additions & 12 deletions sqle/api/controller/v2/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ type GetAuditTaskSQLsResV2 struct {
}

type AuditTaskSQLResV2 struct {
Number uint `json:"number"`
ExecSQL string `json:"exec_sql"`
SQLSourceFile string `json:"sql_source_file"`
SQLStartLine uint64 `json:"sql_start_line"`
AuditResult []*AuditResult `json:"audit_result"`
AuditLevel string `json:"audit_level"`
AuditStatus string `json:"audit_status"`
ExecResult string `json:"exec_result"`
ExecStatus string `json:"exec_status"`
RollbackSQL string `json:"rollback_sql,omitempty"`
Description string `json:"description"`
SQLType string `json:"sql_type"`
ExecSqlID uint `json:"exec_sql_id"`
Number uint `json:"number"`
ExecSQL string `json:"exec_sql"`
SQLSourceFile string `json:"sql_source_file"`
SQLStartLine uint64 `json:"sql_start_line"`
AuditResult []*AuditResult `json:"audit_result"`
AuditLevel string `json:"audit_level"`
AuditStatus string `json:"audit_status"`
ExecResult string `json:"exec_result"`
ExecStatus string `json:"exec_status"`
RollbackSQL string `json:"rollback_sql,omitempty"`
Description string `json:"description"`
SQLType string `json:"sql_type"`
BackupStrategy string `json:"backup_strategy" enum:"none,manual,reverse_sql,origin_row"`
BackupStrategyTip string `json:"backup_strategy_tip"`
}

type AuditResult struct {
Expand Down
12 changes: 7 additions & 5 deletions sqle/api/controller/v2/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,11 @@ func convertWorkflowToTasksSummaryRes(taskDetails []*model.WorkflowTasksSummaryD
}

type CreateWorkflowReqV2 struct {
Subject string `json:"workflow_subject" form:"workflow_subject" valid:"required,name"`
Desc string `json:"desc" form:"desc"`
SqlVersionID *uint `json:"sql_version_id" form:"sql_version_id"`
TaskIds []uint `json:"task_ids" form:"task_ids" valid:"required"`
Subject string `json:"workflow_subject" form:"workflow_subject" valid:"required,name"`
Desc string `json:"desc" form:"desc"`
SqlVersionID *uint `json:"sql_version_id" form:"sql_version_id"`
TaskIds []uint `json:"task_ids" form:"task_ids" valid:"required"`
AssociatedWorkflowId string `json:"associated_workflow_id"`
}

type CreateWorkflowResV2 struct {
Expand Down Expand Up @@ -794,7 +795,7 @@ type UpdateWorkflowReqV2 struct {
}

// UpdateWorkflowV2
// @Summary 更新工单(驳回后才可更新
// @Summary 更新工单(工单被驳回、工单被关闭、执行成功、执行失败后才可更新
// @Description update workflow when it is rejected to creator.
// @Tags workflow
// @Accept json
Expand Down Expand Up @@ -901,6 +902,7 @@ func UpdateWorkflowV2(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errTaskHasBeenUsed)
}
// When workflow status is rejected or exec failed, the user can recommit workflow. And the workflow becomes waiting for the audit process.
// TODO 重试工单 驳回、成功、失败、关闭的工单可以重试,应抽离出一个函数
if workflow.Record.Status != model.WorkflowStatusReject && workflow.Record.Status != model.WorkflowStatusExecFailed {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid,
fmt.Errorf("workflow status is %s, not allow operate it", workflow.Record.Status)))
Expand Down
Loading

0 comments on commit 39357ce

Please sign in to comment.