Skip to content

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

chore: update

d-bytebase53de4f6c4c96
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index d1c981495f..e28c6dd5dd 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -120,7 +120,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
return
}
- if err := s.scheduleActiveStageToRunning(ctx); err != nil {
+ if err := s.schedulePendingTasks(ctx); err != nil {
log.Error("Failed to schedule tasks in the active stage",
zap.Error(err),
)
@@ -713,37 +713,18 @@ func canUpdateTaskStatement(task *api.Task) *echo.HTTPError {
return 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)
- if err != nil {
- return false, errors.Wrap(err, "failed to check if task is blocked")
- }
- if blocked {
- return false, nil
- }
-
- if task.EarliestAllowedTs != 0 && time.Now().Before(time.Unix(task.EarliestAllowedTs, 0)) {
- return false, nil
- }
-
- 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
-// 1. its required check does not contain error in the latest run.
// 2. it has no blocking tasks.
// 3. it has passed the earliest allowed time.
func (s *Scheduler) scheduleIfNeeded(ctx context.Context, task *api.Task) error {
- schedule, err := s.canSchedule(ctx, task)
+ blocked, err := s.isTaskBlocked(ctx, task)
if err != nil {
- return err
+ return errors.Wrap(err, "failed to check if task is blocked")
+ }
+ if blocked {
+ return nil
}
- if !schedule {
+ if task.EarliestAllowedTs != 0 && time.Now().Before(time.Unix(task.EarliestAllowedTs, 0)) {
return nil
}
@@ -754,7 +735,6 @@ func (s *Scheduler) scheduleIfNeeded(ctx context.Context, task *api.Task) error
}); err != nil {
return err
}
-
return nil
}
@@ -803,6 +783,7 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
if err != nil {
return errors.Wrap(err, "failed to check if can auto-approve")
}
+ // Checks active stage.
if ok {
if _, err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{
ID: task.ID,
@@ -816,25 +797,16 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
return nil
}
-// scheduleActiveStageToRunning tries to schedule the tasks in the active stage.
-func (s *Scheduler) scheduleActiveStageToRunning(ctx context.Context) error {
- pipelineList, err := s.store.ListActivePipelines(ctx)
+// schedulePendingTasks tries to schedule pending tasks.
+func (s *Scheduler) schedulePendingTasks(ctx context.Context) error {
+ taskStatus := []api.TaskStatus{api.TaskPending}
+ tasks, err := s.store.FindTask(ctx, &api.TaskFind{StatusList: &taskStatus})
if err != nil {
- return errors.Wrap(err, "failed to retrieve open pipelines")
+ return err
}
- for _, pipeline := range pipelineList {
- stage := utils.GetActiveStage(pipeline)
- if stage == nil {
- continue
- }
- for _, task := range stage.TaskList {
- if task.Status != api.TaskPending {
- continue
- }
-
- if err := s.scheduleIfNeeded(ctx, task); err != nil {
- return errors.Wrap(err, "failed to schedule task")
- }
+ for _, task := range tasks {
+ if err := s.scheduleIfNeeded(ctx, task); err != nil {
+ return errors.Wrap(err, "failed to schedule task")
}
}
return nil
diff --git a/backend/server/stage.go b/backend/server/stage.go
index 7e2e3795d5..cc9cbb731b 100644
--- a/backend/server/stage.go
+++ b/backend/server/stage.go
@@ -10,6 +10,7 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
)
func (s *Server) registerStageRoutes(g *echo.Group) {
@@ -70,7 +71,30 @@ 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.
+
+ if stageAllTaskStatusPatch.Status == api.TaskPending {
+ for _, task := range tasks {
+ instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
+ if err != nil {
+ return err
+ }
+ ok, err = utils.PassAllCheck(task, api.TaskCheckStatusWarn, task.TaskCheckRunList, instance.Engine)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return echo.NewHTTPError(http.StatusBadRequest, "The task has not passed all the checks yet")
+ }
+ }
+ composedPipeline, err := s.store.GetPipelineByID(ctx, tasks[0].PipelineID)
+ if err != nil {
+ return err
+ }
+ activeStage := utils.GetActiveStage(composedPipeline)
+ if tasks[0].StageID != activeStage.ID {
+ return echo.NewHTTPError(http.StatusBadRequest, "Tasks in the prior stage are not done yet")
+ }
+ }
var taskIDList []int
for _, task := range tasks {
diff --git a/backend/server/task.go b/backend/server/task.go
index ae84d10c35..b3fd3e18c8 100644
--- a/backend/server/task.go
+++ b/backend/server/task.go
@@ -11,6 +11,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
)
func (s *Server) registerTaskRoutes(g *echo.Group) {
@@ -156,7 +157,28 @@ 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.TaskPending {
+ instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
+ if err != nil {
+ return err
+ }
+ ok, err = utils.PassAllCheck(task, api.TaskCheckStatusWarn, task.TaskCheckRunList, instance.Engine)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return echo.NewHTTPError(http.StatusBadRequest, "The task has not passed all the checks yet")
+ }
+ composedPipeline, err := s.store.GetPipelineByID(ctx, task.PipelineID)
+ if err != nil {
+ return err
+ }
+ activeStage := utils.GetActiveStage(composedPipeline)
+ if task.StageID != activeStage.ID {
+ return echo.NewHTTPError(http.StatusBadRequest, "Tasks in the prior stage are not done yet")
+ }
+ }
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/pipeline.go b/backend/store/pipeline.go
index b0ce771d3f..d4c808ca93 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -28,24 +28,6 @@ func (s *Store) GetPipelineByID(ctx context.Context, id int) (*api.Pipeline, err
return composedPipeline, nil
}
-// 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 active pipelines")
- }
- var composedPipelines []*api.Pipeline
- for _, pipeline := range pipelines {
- composedPipeline, err := s.composePipeline(ctx, pipeline)
- if err != nil {
- return nil, err
- }
- composedPipelines = append(composedPipelines, composedPipeline)
- }
- return composedPipelines, nil
-}
-
func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
composedPipeline := &api.Pipeline{
ID: pipeline.ID,
@@ -136,9 +118,6 @@ type PipelineMessage struct {
// PipelineFind is the API message for finding pipelines.
type PipelineFind struct {
ID *int
-
- // Domain specific fields
- Active *bool
}
// CreatePipelineV2 creates a pipeline.
@@ -202,24 +181,16 @@ func (s *Store) GetPipelineV2ByID(ctx context.Context, id int) (*PipelineMessage
// ListPipelineV2 lists pipelines.
func (s *Store) ListPipelineV2(ctx context.Context, find *PipelineFind) ([]*PipelineMessage, error) {
- // Build WHERE clause.
- joinClause := ""
where, args := []string{"TRUE"}, []interface{}{}
if v := find.ID; v != nil {
where, args = append(where, fmt.Sprintf("pipeline.id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.Active; v != nil {
- joinClause = "JOIN task ON pipeline.id = task.pipeline_id"
- where, args = append(where, fmt.Sprintf(`(SELECT COUNT(1) FROM task WHERE task.status = $%d AND pipeline.id = task.pipeline_id) > 0`, len(args)+1)), append(args, api.TaskPending)
- }
query := fmt.Sprintf(`
SELECT
pipeline.id,
pipeline.name
FROM pipeline
- %s
- WHERE %s
- GROUP BY pipeline.id`, joinClause, strings.Join(where, " AND "))
+ WHERE %s`, strings.Join(where, " AND "))
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index a76cb55cb9..429f72ba0b 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -373,10 +373,16 @@ func MergeTaskCreateLists(taskCreateLists [][]api.TaskCreate, taskIndexDAGLists
}
// 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) {
+func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRuns []*api.TaskCheckRun, engine db.Type) (bool, error) {
+ var runs []*api.TaskCheckRun
+ for _, run := range taskCheckRuns {
+ if run.TaskID == task.ID {
+ runs = append(runs, run)
+ }
+ }
// 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)
+ pass, err := passCheck(runs, api.TaskCheckDatabaseConnect, allowedStatus)
if err != nil {
return false, err
}
@@ -384,7 +390,7 @@ func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRu
return false, nil
}
- pass, err = passCheck(taskCheckRunList, api.TaskCheckInstanceMigrationSchema, allowedStatus)
+ pass, err = passCheck(runs, api.TaskCheckInstanceMigrationSchema, allowedStatus)
if err != nil {
return false, err
}
@@ -393,7 +399,7 @@ func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRu
}
if api.IsSyntaxCheckSupported(engine) {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementSyntax, allowedStatus)
+ ok, err := passCheck(runs, api.TaskCheckDatabaseStatementSyntax, allowedStatus)
if err != nil {
return false, err
}
@@ -403,7 +409,7 @@ func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRu
}
if api.IsSQLReviewSupported(engine) {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementAdvise, allowedStatus)
+ ok, err := passCheck(runs, api.TaskCheckDatabaseStatementAdvise, allowedStatus)
if err != nil {
return false, err
}
@@ -413,7 +419,7 @@ func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRu
}
if engine == db.Postgres {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckDatabaseStatementType, allowedStatus)
+ ok, err := passCheck(runs, api.TaskCheckDatabaseStatementType, allowedStatus)
if err != nil {
return false, err
}
@@ -424,7 +430,7 @@ func PassAllCheck(task *api.Task, allowedStatus api.TaskCheckStatus, taskCheckRu
}
if task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
- ok, err := passCheck(taskCheckRunList, api.TaskCheckGhostSync, allowedStatus)
+ ok, err := passCheck(runs, api.TaskCheckGhostSync, allowedStatus)
if err != nil {
return false, err
}