Skip to content

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

fix: forbid updaing plan task if running/pending (#8146)

p0ny0f2a3fb7d140
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 2fe302e64a..12553c0ab5 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -734,7 +734,6 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, request *v1pb.
}
// UpdatePlan updates a plan.
-// Currently, only Spec.Config.Sheet can be updated.
func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePlanRequest) (*v1pb.Plan, error) {
if request.UpdateMask == nil {
return nil, status.Errorf(codes.InvalidArgument, "update_mask must be set")
@@ -780,7 +779,8 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
updatedByID[spec.Id] = spec
}
- var doUpdateSheet bool
+ tasksMap := map[int]*store.TaskMessage{}
+ var taskPatchList []*api.TaskPatch
updaterID := ctx.Value(common.PrincipalIDContextKey).(int)
if oldPlan.PipelineUID != nil {
tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: oldPlan.PipelineUID})
@@ -793,6 +793,7 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
ID: task.ID,
UpdaterID: updaterID,
}
+ tasksMap[task.ID] = task
var taskSpecID struct {
SpecID string `json:"specId"`
@@ -871,7 +872,6 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
return status.Errorf(codes.NotFound, "sheet %q not found", config.ChangeDatabaseConfig.Sheet)
}
doUpdate = true
- doUpdateSheet = true
// TODO(p0ny): update schema version
taskPatch.SheetID = &sheet.UID
}
@@ -884,26 +884,48 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
continue
}
- if _, err := s.store.UpdateTaskV2(ctx, taskPatch); err != nil {
- return nil, status.Errorf(codes.Internal, "failed to update task %q: %v", task.Name, err)
- }
+ taskPatchList = append(taskPatchList, taskPatch)
+ }
+ }
- taskPatched, err := s.store.GetTaskV2ByID(ctx, task.ID)
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to get updated task %q: %v", task.Name, err)
+ for _, taskPatch := range taskPatchList {
+ if taskPatch.SheetID != nil || taskPatch.EarliestAllowedTs != nil {
+ task := tasksMap[taskPatch.ID]
+ if task.LatestTaskRunStatus == api.TaskRunPending || task.LatestTaskRunStatus == api.TaskRunRunning {
+ return nil, status.Errorf(codes.FailedPrecondition, "cannot update plan because task %q is %s", task.Name, task.LatestTaskRunStatus)
}
+ }
+ }
+
+ var doUpdateSheet bool
+ for _, taskPatch := range taskPatchList {
+ if taskPatch.SheetID != nil {
+ doUpdateSheet = true
+ break
+ }
+ }
+
+ for _, taskPatch := range taskPatchList {
+ task := tasksMap[taskPatch.ID]
+ if _, err := s.store.UpdateTaskV2(ctx, taskPatch); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to update task %q: %v", task.Name, err)
+ }
+
+ taskPatched, err := s.store.GetTaskV2ByID(ctx, task.ID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get updated task %q: %v", task.Name, err)
+ }
- // enqueue or cancel after it's written to the database.
- if taskPatch.RollbackEnabled != nil {
- // Enqueue the rollback sql generation if the task done.
- if *taskPatch.RollbackEnabled && taskPatched.LatestTaskRunStatus == api.TaskRunDone {
- s.stateCfg.RollbackGenerate.Store(taskPatched.ID, taskPatched)
- } else if !*taskPatch.RollbackEnabled {
- // Cancel running rollback sql generation.
- if v, ok := s.stateCfg.RollbackCancel.Load(taskPatched.ID); ok {
- if cancel, ok := v.(context.CancelFunc); ok {
- cancel()
- }
+ // enqueue or cancel after it's written to the database.
+ if taskPatch.RollbackEnabled != nil {
+ // Enqueue the rollback sql generation if the task done.
+ if *taskPatch.RollbackEnabled && taskPatched.LatestTaskRunStatus == api.TaskRunDone {
+ s.stateCfg.RollbackGenerate.Store(taskPatched.ID, taskPatched)
+ } else if !*taskPatch.RollbackEnabled {
+ // Cancel running rollback sql generation.
+ if v, ok := s.stateCfg.RollbackCancel.Load(taskPatched.ID); ok {
+ if cancel, ok := v.(context.CancelFunc); ok {
+ cancel()
}
// We don't erase the keys for RollbackCancel and RollbackGenerate here because they will eventually be erased by the rollback runner.
}
diff --git a/backend/api/v1/rollout_service_task.go b/backend/api/v1/rollout_service_task.go
index fa52c989fe..30d601146c 100644
--- a/backend/api/v1/rollout_service_task.go
+++ b/backend/api/v1/rollout_service_task.go
@@ -4,10 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
- "log/slog"
"strconv"
"strings"
+ "log/slog"
+
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
diff --git a/backend/runner/taskrun/pitr_cutover_executor.go b/backend/runner/taskrun/pitr_cutover_executor.go
index b43bf7bc2d..43950e59ea 100644
--- a/backend/runner/taskrun/pitr_cutover_executor.go
+++ b/backend/runner/taskrun/pitr_cutover_executor.go
@@ -78,7 +78,7 @@ func (exec *PITRCutoverExecutor) RunOnce(ctx context.Context, _ context.Context,
payload, err := json.Marshal(api.ActivityPipelineTaskStatusUpdatePayload{
TaskID: task.ID,
- OldStatus: task.Status,
+ OldStatus: api.TaskRunning,
NewStatus: api.TaskDone,
IssueName: issue.Title,
TaskName: task.Name,
diff --git a/backend/server/task.go b/backend/server/task.go
index 240b3ed317..c0b7f1a5d2 100644
--- a/backend/server/task.go
+++ b/backend/server/task.go
@@ -77,7 +77,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
}
// dismiss stale review, re-find the approval template
- if taskPatch.SheetID != nil && task.Status == api.TaskPendingApproval {
+ if taskPatch.SheetID != nil {
issue, err := s.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{
PayloadUpsert: &storepb.IssuePayload{
Approval: &storepb.IssuePayloadApproval{
diff --git a/backend/server/webhook.go b/backend/server/webhook.go
index 3b1c1089c7..4b4c2c7d25 100644
--- a/backend/server/webhook.go
+++ b/backend/server/webhook.go
@@ -2135,9 +2135,6 @@ func (s *Server) tryUpdateTasksFromModifiedFile(ctx context.Context, databases [
// dismiss stale review, re-find the approval template
// it's ok if we failed
if err := func() error {
- if task.Status != api.TaskPendingApproval {
- return nil
- }
issue, err := s.store.UpdateIssueV2(ctx, issue.UID, &store.UpdateIssueMessage{
PayloadUpsert: &storepb.IssuePayload{
Approval: &storepb.IssuePayloadApproval{
diff --git a/backend/store/task.go b/backend/store/task.go
index 2e834777a5..9b10569d68 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -33,7 +33,8 @@ type TaskMessage struct {
TaskRunRawList []*TaskRunMessage
// Domain specific fields
- Name string
+ Name string
+ // Deprecated: use LatestTaskRunStatus instead.
Status api.TaskStatus
Type api.TaskType
Payload string