Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: improvement to task checks (#4429)

Danny Xue85ce2ea77ce
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index dc79d2cc35..2bc1701c1c 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -23,7 +23,6 @@ import (
"github.com/bytebase/bytebase/backend/component/state"
enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
api "github.com/bytebase/bytebase/backend/legacyapi"
- "github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/runner/apprun"
"github.com/bytebase/bytebase/backend/runner/schemasync"
"github.com/bytebase/bytebase/backend/store"
@@ -714,118 +713,6 @@ func canUpdateTaskStatement(task *api.Task) *echo.HTTPError {
return nil
}
-func (s *Scheduler) passAllCheck(ctx context.Context, task *api.Task, allowedStatus api.TaskCheckStatus) (bool, error) {
- statusList := []api.TaskCheckRunStatus{api.TaskCheckRunDone, api.TaskCheckRunFailed}
- taskCheckRunFind := &api.TaskCheckRunFind{
- TaskID: &task.ID,
- StatusList: &statusList,
- }
- taskCheckRunList, err := s.store.FindTaskCheckRun(ctx, taskCheckRunFind)
- if err != nil {
- return false, err
- }
-
- // schema update, data update and gh-ost sync task have required task check.
- if task.Type == api.TaskDatabaseSchemaUpdate || task.Type == api.TaskDatabaseSchemaUpdateSDL || task.Type == api.TaskDatabaseDataUpdate || task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
- pass, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseConnect, allowedStatus)
- if err != nil {
- return false, err
- }
- if !pass {
- return false, nil
- }
-
- pass, err = passCheck(taskCheckRunList, api.TaskCheckInstanceMigrationSchema, allowedStatus)
- if err != nil {
- return false, err
- }
- if !pass {
- return false, nil
- }
-
- instance, err := s.store.GetInstanceByID(ctx, task.InstanceID)
- if err != nil {
- return false, err
- }
- if instance == nil {
- return false, errors.Errorf("instance ID not found %v", task.InstanceID)
- }
-
- if api.IsSyntaxCheckSupported(instance.Engine) {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementSyntax, allowedStatus)
- if err != nil {
- return false, err
- }
- if !ok {
- return false, nil
- }
- }
-
- if api.IsSQLReviewSupported(instance.Engine) {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementAdvise, allowedStatus)
- if err != nil {
- return false, err
- }
- if !ok {
- return false, nil
- }
- }
-
- if instance.Engine == db.Postgres {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementType, allowedStatus)
- if err != nil {
- return false, err
- }
- if !ok {
- return false, nil
- }
- }
- }
-
- if task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckGhostSync, allowedStatus)
- if err != nil {
- return false, err
- }
- if !ok {
- return false, nil
- }
- }
-
- return true, nil
-}
-
-// Returns true only if the task check run result is at least the minimum required level.
-// For PendingApproval->Pending transitions, the minimum level is SUCCESS.
-// For Pending->Running transitions, the minimum level is WARN.
-// TODO(dragonly): refactor arguments.
-func passCheck(taskCheckRunList []*api.TaskCheckRun, checkType api.TaskCheckType, allowedStatus api.TaskCheckStatus) (bool, error) {
- var lastRun *api.TaskCheckRun
- for _, run := range taskCheckRunList {
- if checkType != run.Type {
- continue
- }
- if lastRun == nil || lastRun.ID < run.ID {
- lastRun = run
- }
- }
-
- if lastRun == nil || lastRun.Status != api.TaskCheckRunDone {
- return false, nil
- }
- checkResult := &api.TaskCheckRunResultPayload{}
- if err := json.Unmarshal([]byte(lastRun.Result), checkResult); err != nil {
- return false, err
- }
- for _, result := range checkResult.ResultList {
- if result.Status.LessThan(allowedStatus) {
- return false, nil
- }
- }
-
- return true, nil
-}
-
// canSchedule returns whether a task can be scheduled.
func (s *Scheduler) canSchedule(ctx context.Context, task *api.Task) (bool, error) {
blocked, err := s.isTaskBlocked(ctx, task)
@@ -840,7 +727,11 @@ func (s *Scheduler) canSchedule(ctx context.Context, task *api.Task) (bool, erro
return false, nil
}
- return s.passAllCheck(ctx, task, api.TaskCheckStatusWarn)
+ instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
+ if err != nil {
+ return false, err
+ }
+ return utils.PassAllCheck(task, api.TaskCheckStatusWarn, task.TaskCheckRunList, instance.Engine)
}
// scheduleIfNeeded schedules the task if
@@ -873,7 +764,7 @@ func (s *Scheduler) isTaskBlocked(ctx context.Context, task *api.Task) (bool, er
if err != nil {
return true, errors.Wrapf(err, "failed to convert id string to int, id string: %v", blockingTaskIDString)
}
- blockingTask, err := s.store.GetTaskByID(ctx, blockingTaskID)
+ blockingTask, err := s.store.GetTaskV2ByID(ctx, blockingTaskID)
if err != nil {
return true, errors.Wrapf(err, "failed to fetch the blocking task, id: %v", blockingTaskID)
}
@@ -904,7 +795,11 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
continue
}
- ok, err := s.passAllCheck(ctx, task, api.TaskCheckStatusSuccess)
+ instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.Database.InstanceID})
+ if err != nil {
+ return err
+ }
+ ok, err := utils.PassAllCheck(task, api.TaskCheckStatusSuccess, task.TaskCheckRunList, instance.Engine)
if err != nil {
return errors.Wrap(err, "failed to check if can auto-approve")
}
@@ -923,11 +818,7 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
// scheduleActiveStageToRunning tries to schedule the tasks in the active stage.
func (s *Scheduler) scheduleActiveStageToRunning(ctx context.Context) error {
- active := true
- pipelineFind := &store.PipelineFind{
- Active: &active,
- }
- pipelineList, err := s.store.FindPipeline(ctx, pipelineFind)
+ pipelineList, err := s.store.ListActivePipelines(ctx)
if err != nil {
return errors.Wrap(err, "failed to retrieve open pipelines")
}
diff --git a/backend/server/stage.go b/backend/server/stage.go
index d63875fe23..7e2e3795d5 100644
--- a/backend/server/stage.go
+++ b/backend/server/stage.go
@@ -70,6 +70,8 @@ func (s *Server) registerStageRoutes(g *echo.Group) {
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Not allowed to change task status")
}
+ // TODO(d): check pass checks here.
+
var taskIDList []int
for _, task := range tasks {
taskIDList = append(taskIDList, task.ID)
diff --git a/backend/server/task.go b/backend/server/task.go
index f7636824fb..ae84d10c35 100644
--- a/backend/server/task.go
+++ b/backend/server/task.go
@@ -156,6 +156,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Not allowed to change task status")
}
+ // TODO(d): check pass checks here.
if taskStatusPatch.Status == api.TaskDone {
// the user marks the task as DONE, set Skipped to true and SkippedReason to Comment.
diff --git a/backend/store/issue.go b/backend/store/issue.go
index a8e0d7bcd6..a4dc0d2889 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -312,42 +312,11 @@ func (s *Store) composeIssueStripped(ctx context.Context, issue *IssueMessage) (
if pipeline == nil {
return nil, nil
}
- composedPipeline := &api.Pipeline{
- ID: pipeline.ID,
- Name: pipeline.Name,
- }
-
- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &issue.PipelineUID})
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", issue.PipelineUID)
- }
- stages, err := s.ListStageV2(ctx, issue.PipelineUID)
+ composedPipeline, err := s.composeSimplePipeline(ctx, pipeline)
if err != nil {
- return nil, errors.Wrapf(err, "failed to find stage list")
- }
- var composedStages []*api.Stage
- for _, stage := range stages {
- environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
- if err != nil {
- return nil, err
- }
- composedStage := &api.Stage{
- ID: stage.ID,
- Name: stage.Name,
- EnvironmentID: stage.EnvironmentID,
- Environment: environment,
- PipelineID: stage.PipelineID,
- }
-
- for _, task := range tasks {
- if task.StageID == stage.ID {
- composedStage.TaskList = append(composedStage.TaskList, task.toTask())
- }
- }
- composedStages = append(composedStages, composedStage)
+ return nil, err
}
- composedPipeline.StageList = composedStages
composedIssue.Pipeline = composedPipeline
return composedIssue, nil
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index d32cc06dd7..b0ce771d3f 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -28,11 +28,12 @@ func (s *Store) GetPipelineByID(ctx context.Context, id int) (*api.Pipeline, err
return composedPipeline, nil
}
-// FindPipeline finds a list of Pipeline instances.
-func (s *Store) FindPipeline(ctx context.Context, find *PipelineFind) ([]*api.Pipeline, error) {
- pipelines, err := s.ListPipelineV2(ctx, find)
+// ListActivePipelines finds a list of active pipelines.
+func (s *Store) ListActivePipelines(ctx context.Context) ([]*api.Pipeline, error) {
+ active := true
+ pipelines, err := s.ListPipelineV2(ctx, &PipelineFind{Active: &active})
if err != nil {
- return nil, errors.Wrapf(err, "failed to find Pipeline list with PipelineFind[%+v]", find)
+ return nil, errors.Wrapf(err, "failed to find active pipelines")
}
var composedPipelines []*api.Pipeline
for _, pipeline := range pipelines {
@@ -45,7 +46,6 @@ func (s *Store) FindPipeline(ctx context.Context, find *PipelineFind) ([]*api.Pi
return composedPipelines, nil
}
-// Note: MUST keep in sync with composePipelineValidateOnly.
func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
composedPipeline := &api.Pipeline{
ID: pipeline.ID,
@@ -87,6 +87,45 @@ func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage)
return composedPipeline, nil
}
+func (s *Store) composeSimplePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
+ tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", pipeline.ID)
+ }
+
+ stages, err := s.ListStageV2(ctx, pipeline.ID)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find stage list")
+ }
+ var composedStages []*api.Stage
+ for _, stage := range stages {
+ environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
+ if err != nil {
+ return nil, err
+ }
+ composedStage := &api.Stage{
+ ID: stage.ID,
+ Name: stage.Name,
+ EnvironmentID: stage.EnvironmentID,
+ Environment: environment,
+ PipelineID: stage.PipelineID,
+ }
+
+ for _, task := range tasks {
+ if task.StageID == stage.ID {
+ composedStage.TaskList = append(composedStage.TaskList, task.toTask())
+ }
+ }
+ composedStages = append(composedStages, composedStage)
+ }
+
+ return &api.Pipeline{
+ ID: pipeline.ID,
+ Name: pipeline.Name,
+ StageList: composedStages,
+ }, nil
+}
+
// PipelineMessage is the message for pipelines.
type PipelineMessage struct {
Name string
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index ebc43e8ab9..a76cb55cb9 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -371,3 +371,97 @@ func MergeTaskCreateLists(taskCreateLists [][]api.TaskCreate, taskIndexDAGLists
}
return resTaskCreateList, resTaskIndexDAGList, nil
}
+
+// PassAllCheck checks whether a task has passed all task checks.
+func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRunList []*api.TaskCheckRun, engine db.Type) (bool, error) {
+ // schema update, data update and gh-ost sync task have required task check.
+ if task.Type == api.TaskDatabaseSchemaUpdate || task.Type == api.TaskDatabaseSchemaUpdateSDL || task.Type == api.TaskDatabaseDataUpdate || task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
+ pass, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseConnect, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !pass {
+ return false, nil
+ }
+
+ pass, err = passCheck(taskCheckRunList, api.TaskCheckInstanceMigrationSchema, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !pass {
+ return false, nil
+ }
+
+ if api.IsSyntaxCheckSupported(engine) {
+ ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementSyntax, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !ok {
+ return false, nil
+ }
+ }
+
+ if api.IsSQLReviewSupported(engine) {
+ ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementAdvise, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !ok {
+ return false, nil
+ }
+ }
+
+ if engine == db.Postgres {
+ ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementType, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !ok {
+ return false, nil
+ }
+ }
+ }
+
+ if task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
+ ok, err := passCheck(taskCheckRunList, api.TaskCheckGhostSync, allowedStatus)
+ if err != nil {
+ return false, err
+ }
+ if !ok {
+ return false, nil
+ }
+ }
+
+ return true, nil
+}
+
+// Returns true only if the task check run result is at least the minimum required level.
+// For PendingApproval->Pending transitions, the minimum level is SUCCESS.
+// For Pending->Running transitions, the minimum level is WARN.
+func passCheck(taskCheckRunList []*api.TaskCheckRun, checkType api.TaskCheckType, allowedStatus api.TaskCheckStatus) (bool, error) {
+ var lastRun *api.TaskCheckRun
+ for _, run := range taskCheckRunList {
+ if checkType != run.Type {
+ continue
+ }
+ if lastRun == nil || lastRun.ID < run.ID {
+ lastRun = run
+ }
+ }
+
+ if lastRun == nil || lastRun.Status != api.TaskCheckRunDone {
+ return false, nil
+ }
+ checkResult := &api.TaskCheckRunResultPayload{}
+ if err := json.Unmarshal([]byte(lastRun.Result), checkResult); err != nil {
+ return false, err
+ }
+ for _, result := range checkResult.ResultList {
+ if result.Status.LessThan(allowedStatus) {
+ return false, nil
+ }
+ }
+
+ return true, nil
+}