Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: prevent stale approval flows (#20701)
p0nyff99620b7478
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 644960c691..c3a9a72ab2 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -39,6 +39,8 @@ type IssueService struct {iamManager *iam.Manager}+var errStaleApprovalFinding = errors.New("stale approval finding")+type filterIssueMessage struct {ApprovalStatus *v1pb.ApprovalStatus// Approver is the user who can approve the issue.@@ -600,6 +602,11 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1return nil, connect.NewError(connect.CodeInternal, errors.New("approval template is required"))}+ plan, approvalInputVersion, err := s.getIssuePlanApprovalInputVersion(ctx, issue)+ if err != nil {+ return nil, err+ }+rejectedRole := utils.FindRejectedRole(payload.Approval)if rejectedRole != "" {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("cannot approve because the issue has been rejected"))@@ -629,18 +636,27 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1Principal: common.FormatUserEmail(user.Email),})- approved, err := utils.CheckApprovalApproved(payload.Approval)+ approved, err := utils.CheckIssueApprovedForPlan(issue, plan)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the approval is approved"))}- issue, err = s.store.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{- Approval: payload.Approval,- },- })+ issue, err = s.updateIssueApprovalPayload(ctx, issue, &storepb.Issue{Approval: payload.Approval}, plan, approvalInputVersion)if err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to update issue"))+ if errors.Is(err, errStaleApprovalFinding) {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("cannot approve because approval finding is stale"))+ }+ return nil, connect.NewError(connect.CodeInternal, err)+ }+ if plan != nil {+ plan, _, err = s.getIssuePlanApprovalInputVersion(ctx, issue)+ if err != nil {+ return nil, err+ }+ approved, err = utils.CheckIssueApprovedForPlan(issue, plan)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the approval is approved"))+ }}if _, err := s.store.CreateIssueComments(ctx, user.Email, &store.IssueCommentMessage{@@ -708,6 +724,11 @@ func (s *IssueService) RejectIssue(ctx context.Context, req *connect.Request[v1preturn nil, connect.NewError(connect.CodeInternal, errors.New("approval template is required"))}+ plan, approvalInputVersion, err := s.getIssuePlanApprovalInputVersion(ctx, issue)+ if err != nil {+ return nil, err+ }+rejectedRole := utils.FindRejectedRole(payload.Approval)if rejectedRole != "" {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("cannot reject because the issue has been rejected"))@@ -737,13 +758,12 @@ func (s *IssueService) RejectIssue(ctx context.Context, req *connect.Request[v1pPrincipal: common.FormatUserEmail(user.Email),})- issue, err = s.store.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{- Approval: payload.Approval,- },- })+ issue, err = s.updateIssueApprovalPayload(ctx, issue, &storepb.Issue{Approval: payload.Approval}, plan, approvalInputVersion)if err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to update issue"))+ if errors.Is(err, errStaleApprovalFinding) {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("cannot reject because approval finding is stale"))+ }+ return nil, connect.NewError(connect.CodeInternal, err)}if _, err := s.store.CreateIssueComments(ctx, user.Email, &store.IssueCommentMessage{@@ -818,6 +838,11 @@ func (s *IssueService) RequestIssue(ctx context.Context, req *connect.Request[v1return nil, connect.NewError(connect.CodeInternal, errors.New("approval template is required"))}+ plan, approvalInputVersion, err := s.getIssuePlanApprovalInputVersion(ctx, issue)+ if err != nil {+ return nil, err+ }+rejectedRole := utils.FindRejectedRole(payload.Approval)if rejectedRole == "" {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("cannot request issues because the issue is not rejected"))@@ -842,13 +867,12 @@ func (s *IssueService) RequestIssue(ctx context.Context, req *connect.Request[v1}payload.Approval.Approvers = updatedApprovers- issue, err = s.store.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{- Approval: payload.Approval,- },- })+ issue, err = s.updateIssueApprovalPayload(ctx, issue, &storepb.Issue{Approval: payload.Approval}, plan, approvalInputVersion)if err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to update issue"))+ if errors.Is(err, errStaleApprovalFinding) {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("cannot request issue because approval finding is stale"))+ }+ return nil, connect.NewError(connect.CodeInternal, err)}approval.NotifyApprovalRequested(ctx, s.store, s.webhookManager, issue, project)@@ -875,6 +899,58 @@ func (s *IssueService) RequestIssue(ctx context.Context, req *connect.Request[v1return connect.NewResponse(issueV1), nil}+func (s *IssueService) getIssuePlanApprovalInputVersion(ctx context.Context, issue *store.IssueMessage) (*store.PlanMessage, int64, error) {+ if issue.Type != storepb.Issue_DATABASE_CHANGE || issue.PlanUID == nil {+ return nil, 0, nil+ }++ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})+ if err != nil {+ return nil, 0, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get plan"))+ }+ if plan == nil {+ return nil, 0, connect.NewError(connect.CodeNotFound, errors.New("plan not found"))+ }+ if plan.Config == nil {+ return plan, 0, nil+ }+ return plan, plan.Config.GetApprovalInputVersion(), nil+}++func (s *IssueService) updateIssueApprovalPayload(ctx context.Context, issue *store.IssueMessage, payloadPatch *storepb.Issue, plan *store.PlanMessage, approvalInputVersion int64) (*store.IssueMessage, error) {+ if plan == nil {+ updatedIssue, err := s.store.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{+ PayloadUpsert: payloadPatch,+ })+ if err != nil {+ return nil, errors.Wrapf(err, "failed to update issue")+ }+ return updatedIssue, nil+ }++ updated, err := s.store.UpdateIssuePayloadIfCurrentApprovalInputVersion(ctx, issue.ProjectID, issue.UID, payloadPatch, approvalInputVersion)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to update issue")+ }+ if !updated {+ return nil, errStaleApprovalFinding+ }++ uid := issue.UID+ updatedIssue, err := s.store.GetIssue(ctx, &store.FindIssueMessage{+ Workspace: common.GetWorkspaceIDFromContext(ctx),+ ProjectIDs: []string{issue.ProjectID},+ UID: &uid,+ })+ if err != nil {+ return nil, errors.Wrapf(err, "failed to refresh issue")+ }+ if updatedIssue == nil {+ return nil, errors.New("issue not found after update")+ }+ return updatedIssue, nil+}+// RetryIssueApproval re-runs approval-template finding for an issue stuck// in CHECKING. The synchronous post-create path in `postCreateIssue`// swallows errors (e.g. CEL evaluation failure against a malformed@@ -904,8 +980,21 @@ func (s *IssueService) RetryIssueApproval(ctx context.Context, req *connect.Requreturn nil, connect.NewError(connect.CodePermissionDenied, errors.New("only the issue creator can retry approval finding"))}- // No-op fast path: nothing to retry if the previous attempt completed.- if issue.Payload.GetApproval().GetApprovalFindingDone() {+ approvalFindingCurrent := true+ var plan *store.PlanMessage+ if issue.Type == storepb.Issue_DATABASE_CHANGE && issue.PlanUID != nil {+ plan, err = s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get plan"))+ }+ if plan == nil {+ return nil, connect.NewError(connect.CodeNotFound, errors.New("plan not found"))+ }+ approvalFindingCurrent = issue.Payload.GetApproval().GetApprovalInputVersion() == plan.Config.GetApprovalInputVersion()+ }++ // No-op fast path: nothing to retry if the previous attempt completed for the current approval input.+ if issue.Payload.GetApproval().GetApprovalFindingDone() && approvalFindingCurrent {issueV1, err := s.convertToIssue(issue)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to issue"))@@ -939,7 +1028,16 @@ func (s *IssueService) RetryIssueApproval(ctx context.Context, req *connect.Requ// * ACCESS_GRANT / ROLE_GRANT → activate the grant via// `completeAccessRequestIssue`.// * DATABASE_CHANGE with a plan → enqueue rollout creation.- approved, err := utils.CheckIssueApproved(refreshed)+ if plan == nil && refreshed.Type == storepb.Issue_DATABASE_CHANGE && refreshed.PlanUID != nil {+ plan, err = s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: refreshed.ProjectID, UID: refreshed.PlanUID})+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get plan"))+ }+ if plan == nil {+ return nil, connect.NewError(connect.CodeNotFound, errors.New("plan not found"))+ }+ }+ approved, err := utils.CheckIssueApprovedForPlan(refreshed, plan)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check approval state"))}diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 591f48f00d..0b98badb96 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -213,7 +213,7 @@ func (s *PlanService) CreatePlan(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan check run for plan"))}if planCheckRun != nil {- if err := s.store.CreatePlanCheckRun(ctx, planCheckRun); err != nil {+ if _, err := s.store.CreatePlanCheckRun(ctx, planCheckRun); err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create plan check run"))}}@@ -334,6 +334,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vconfig.Specs = allSpecsplanUpdate.Config = configplanUpdate.BumpApprovalInputVersion = true+ planUpdate.RequireNoRollout = true// Trigger plan check runs.planCheckRunsTrigger = true@@ -367,6 +368,9 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vupdatedPlan, err := s.store.UpdatePlan(ctx, planUpdate)if err != nil {+ if errors.Is(err, store.ErrPlanHasRollout) {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf("cannot update specs for plan that has a rollout"))+ }return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update plan %q: %v", req.Plan.Name, err))}@@ -374,17 +378,18 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vif issueToReset == nil {return nil}- updatedIssue, err := s.store.UpdateIssue(ctx, issueToReset.ProjectID, issueToReset.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{- Approval: &storepb.IssuePayloadApproval{- ApprovalFindingDone: false,- },- },- })+ updatedIssue, updated, err := resetIssueApprovalFindingIfPlanApprovalInputVersion(ctx, s.store, issueToReset, updatedPlan.Config.GetApprovalInputVersion())if err != nil {slog.Error("failed to reset approval finding status after plan update", log.BBError(err))return nil}+ if !updated {+ slog.Info("skip stale approval finding reset after plan update",+ slog.String("project", issueToReset.ProjectID),+ slog.Int64("issue_uid", issueToReset.UID),+ slog.Int64("approval_input_version", updatedPlan.Config.GetApprovalInputVersion()))+ return nil+ }return updatedIssue}@@ -396,11 +401,12 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan check run for plan"))}if planCheckRun != nil {- if err := s.store.CreatePlanCheckRun(ctx, planCheckRun); err != nil {+ created, err := s.store.CreatePlanCheckRun(ctx, planCheckRun)+ if err != nil {resetApprovalFinding()return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create plan check run"))}- planCheckRunCreated = true+ planCheckRunCreated = created}}@@ -455,6 +461,40 @@ func (s *PlanService) GetPlanCheckRun(ctx context.Context, request *connect.Requreturn connect.NewResponse(converted), nil}+func resetIssueApprovalFindingIfPlanApprovalInputVersion(ctx context.Context, stores *store.Store, issue *store.IssueMessage, approvalInputVersion int64) (*store.IssueMessage, bool, error) {+ payloadPatch := &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: false,+ ApprovalInputVersion: approvalInputVersion,+ },+ }+ if issue.Type == storepb.Issue_DATABASE_CHANGE {+ // Plan check rows are visible before the reset finishes. If another worker already+ // recomputed approval for this version, the reset must not move the issue back to CHECKING.+ updated, err := stores.ResetIssueApprovalFindingIfPlanApprovalInputVersion(ctx, issue.ProjectID, issue.UID, approvalInputVersion)+ if err != nil {+ return nil, false, err+ }+ if !updated {+ return nil, false, nil+ }+ uid := issue.UID+ updatedIssue, err := stores.GetIssue(ctx, &store.FindIssueMessage{+ ProjectIDs: []string{issue.ProjectID},+ UID: &uid,+ })+ return updatedIssue, true, err+ }++ updatedIssue, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{+ PayloadUpsert: payloadPatch,+ })+ if err != nil {+ return nil, false, err+ }+ return updatedIssue, true, nil+}+// RunPlanChecks runs plan checks for a plan.func (s *PlanService) RunPlanChecks(ctx context.Context, request *connect.Request[v1pb.RunPlanChecksRequest]) (*connect.Response[v1pb.RunPlanChecksResponse], error) {req := request.Msg@@ -514,10 +554,15 @@ func (s *PlanService) RunPlanChecks(ctx context.Context, request *connect.Requesif err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan check run for plan"))}- if planCheckRun != nil {- if err := s.store.CreatePlanCheckRun(ctx, planCheckRun); err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create plan check run"))- }+ if planCheckRun == nil {+ return connect.NewResponse(&v1pb.RunPlanChecksResponse{}), nil+ }+ created, err := s.store.CreatePlanCheckRun(ctx, planCheckRun)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create plan check run"))+ }+ if !created {+ return connect.NewResponse(&v1pb.RunPlanChecksResponse{}), nil}// Tickle plan check scheduler.@@ -557,21 +602,27 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.Rreturn nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("plan check run is not running or available"))}+ approvalInputVersion := planCheckRun.Result.GetApprovalInputVersion()++ // Update the status to canceled.+ canceled, err := s.store.CancelPlanCheckRunIfApprovalInputVersion(ctx, projectID, planCheckRun.UID, approvalInputVersion)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to cancel plan check run"))+ }+ if !canceled {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("cannot cancel because plan check run is stale"))+ }+// Cancel in-flight plan check run if running.- if cancelFunc, ok := s.bus.RunningPlanCheckRunsCancelFunc.Load(bus.PlanCheckRunRef{ProjectID: projectID, UID: planCheckRun.UID}); ok {+ if cancelFunc, ok := s.bus.RunningPlanCheckRunsCancelFunc.Load(bus.PlanCheckRunRef{ProjectID: projectID, UID: planCheckRun.UID, ApprovalInputVersion: approvalInputVersion}); ok {cancelFunc.(context.CancelFunc)()}// Broadcast cancel signal to all replicas for HA.- if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, projectID, planCheckRun.UID); err != nil {+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, projectID, planCheckRun.UID, approvalInputVersion); err != nil {slog.Warn("failed to send cancel signal", log.BBError(err))}- // Update the status to canceled.- if err := s.store.BatchCancelPlanCheckRuns(ctx, projectID, []int64{planCheckRun.UID}); err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to cancel plan check run"))- }-return connect.NewResponse(&v1pb.CancelPlanCheckRunResponse{}), nil}@@ -820,7 +871,10 @@ func getPlanCheckRunFromPlan(ctx context.Context, s *store.Store, project *storereturn &store.PlanCheckRunMessage{ProjectID: plan.ProjectID,PlanUID: plan.UID,- Status: store.PlanCheckRunStatusRunning,+ Status: store.PlanCheckRunStatusAvailable,+ Result: &storepb.PlanCheckRunResult{+ ApprovalInputVersion: plan.Config.GetApprovalInputVersion(),+ },}, nil}diff --git a/backend/api/v1/plan_service_test.go b/backend/api/v1/plan_service_test.goindex 2a27a528d1..7b9e5b6726 100644--- a/backend/api/v1/plan_service_test.go+++ b/backend/api/v1/plan_service_test.go@@ -1,11 +1,19 @@package v1import (+ "context"+ "fmt""testing""github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+ "github.com/bytebase/bytebase/backend/common/testcontainer"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/migrator"+ "github.com/bytebase/bytebase/backend/store"++ _ "github.com/bytebase/bytebase/backend/plugin/db/pg")func cdcSpec(id, sheet string, targets []string, priorBackup bool) *storepb.PlanConfig_Spec {@@ -94,3 +102,75 @@ func TestPlanSpecsEqualSet(t *testing.T) {})}}++func TestResetIssueApprovalFindingSkipsStalePlanApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanServiceTestStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ },+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updatedIssue, updated, err := resetIssueApprovalFindingIfPlanApprovalInputVersion(ctx, s, issue, 1)+ require.NoError(t, err)+ require.False(t, updated)+ require.Nil(t, updatedIssue)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())+ require.EqualValues(t, 1, got.Payload.GetApproval().GetApprovalInputVersion())++ updatedIssue, updated, err = resetIssueApprovalFindingIfPlanApprovalInputVersion(ctx, s, issue, 2)+ require.NoError(t, err)+ require.True(t, updated)+ require.NotNil(t, updatedIssue)+ require.False(t, updatedIssue.Payload.GetApproval().GetApprovalFindingDone())+ require.EqualValues(t, 2, updatedIssue.Payload.GetApproval().GetApprovalInputVersion())+}++func setupPlanServiceTestStore(ctx context.Context, t *testing.T) *store.Store {+ t.Helper()++ container := testcontainer.GetTestPgContainer(ctx, t)+ t.Cleanup(func() { container.Close(ctx) })++ db := container.GetDB()+ require.NoError(t, migrator.MigrateSchema(ctx, db))++ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ `)+ require.NoError(t, err)++ pgURL := fmt.Sprintf(+ "host=%s port=%s user=postgres password=root-password database=postgres",+ container.GetHost(), container.GetPort(),+ )+ s, err := store.New(ctx, pgURL, false)+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, s.Close()) })+ return s+}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 297363bc8b..020712b798 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -12,7 +12,6 @@ import ("connectrpc.com/connect""github.com/jackc/pgtype""github.com/pkg/errors"- "google.golang.org/protobuf/proto""google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/common"@@ -42,6 +41,13 @@ type RolloutService struct {iamManager *iam.Manager}+var errStaleRolloutApproval = errors.New("cannot create rollout because issue approval is stale")++// IsStaleRolloutApprovalError reports whether rollout creation lost the approval-version race.+func IsStaleRolloutApprovalError(err error) bool {+ return errors.Is(err, errStaleRolloutApproval)+}+// NewRolloutService returns a rollout service instance.func NewRolloutService(store *store.Store, dbFactory *dbfactory.DBFactory, bus *bus.Bus, webhookManager *webhook.Manager, iamManager *iam.Manager) *RolloutService {return &RolloutService{@@ -279,6 +285,16 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request}}+ if project.Setting.RequireIssueApproval && issue != nil {+ approved, err := utils.CheckIssueApprovedForPlan(issue, plan)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the issue is approved"))+ }+ if !approved {+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("cannot create rollout because issue approval is required but the issue is not approved"))+ }+ }+tasks, err := GetPipelineCreate(ctx, s.store, plan.Config.GetSpecs(), project.ResourceID)if err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to get pipeline create"))@@ -291,6 +307,9 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request}if err := CreateRolloutAndPendingTasks(ctx, s.store, user.Email, plan, issue, project, tasks); err != nil {+ if IsStaleRolloutApprovalError(err) {+ return nil, connect.NewError(connect.CodeFailedPrecondition, err)+ }return nil, connect.NewError(connect.CodeInternal, err)}@@ -388,23 +407,19 @@ func CreateRolloutAndPendingTasks(}}- // Create rollout tasks- tasks, err = s.CreateTasks(ctx, project.ResourceID, plan.UID, tasks)+ var approvalInputVersion *int64+ if issue != nil && issue.Type == storepb.Issue_DATABASE_CHANGE {+ v := plan.Config.GetApprovalInputVersion()+ approvalInputVersion = &v+ }+ marked, createdTasks, err := s.CreateRolloutTasks(ctx, project.ResourceID, plan.UID, approvalInputVersion, tasks)if err != nil {return errors.Wrap(err, "failed to create rollout tasks")}-- // Update plan to set hasRollout to true- config := proto.CloneOf(plan.Config)- config.HasRollout = true- _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{- UID: plan.UID,- ProjectID: project.ResourceID,- Config: config,- })- if err != nil {- return errors.Wrap(err, "failed to update plan hasRollout")+ if !marked {+ return errStaleRolloutApproval}+ tasks = createdTasks// Update issue status to DONE when rollout is createdif issue != nil {@@ -798,7 +813,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request// Check if issue approval is required according to the project settingsif project.Setting.RequireIssueApproval && issueN != nil {- approved, err := utils.CheckIssueApproved(issueN)+ approved, err := utils.CheckIssueApprovedForPlan(issueN, plan)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the issue is approved"))}diff --git a/backend/component/bus/bus.go b/backend/component/bus/bus.goindex 798bfb5647..93384ba12e 100644--- a/backend/component/bus/bus.go+++ b/backend/component/bus/bus.go@@ -25,8 +25,9 @@ type IssueRef struct {// PlanCheckRunRef identifies a plan check run by project and UID.type PlanCheckRunRef struct {- ProjectID string- UID int64+ ProjectID string+ UID int64+ ApprovalInputVersion int64}// Bus is the message bus for all in-memory communication within the server.diff --git a/backend/component/bus/bus_test.go b/backend/component/bus/bus_test.gonew file mode 100644index 0000000000..ff429fc551--- /dev/null+++ b/backend/component/bus/bus_test.go@@ -0,0 +1,14 @@+package bus++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestPlanCheckRunRefIncludesApprovalInputVersion(t *testing.T) {+ require.NotEqual(t,+ PlanCheckRunRef{ProjectID: "project-a", UID: 1, ApprovalInputVersion: 1},+ PlanCheckRunRef{ProjectID: "project-a", UID: 1, ApprovalInputVersion: 2},+ )+}diff --git a/backend/generated-go/store/signal.pb.go b/backend/generated-go/store/signal.pb.goindex fb98021019..a8bfb3b8e7 100644--- a/backend/generated-go/store/signal.pb.go+++ b/backend/generated-go/store/signal.pb.go@@ -73,12 +73,13 @@ func (Signal_Type) EnumDescriptor() ([]byte, []int) {// Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coordination.type Signal struct {- state protoimpl.MessageState `protogen:"open.v1"`- Type Signal_Type `protobuf:"varint,1,opt,name=type,proto3,enum=bytebase.store.Signal_Type" json:"type,omitempty"`- Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"`- Project string `protobuf:"bytes,3,opt,name=project,proto3" json:"project,omitempty"`- unknownFields protoimpl.UnknownFields- sizeCache protoimpl.SizeCache+ state protoimpl.MessageState `protogen:"open.v1"`+ Type Signal_Type `protobuf:"varint,1,opt,name=type,proto3,enum=bytebase.store.Signal_Type" json:"type,omitempty"`+ Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"`+ Project string `protobuf:"bytes,3,opt,name=project,proto3" json:"project,omitempty"`+ ApprovalInputVersion int64 `protobuf:"varint,4,opt,name=approval_input_version,json=approvalInputVersion,proto3" json:"approval_input_version,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache}func (x *Signal) Reset() {@@ -132,15 +133,23 @@ func (x *Signal) GetProject() string {return ""}+func (x *Signal) GetApprovalInputVersion() int64 {+ if x != nil {+ return x.ApprovalInputVersion+ }+ return 0+}+var File_store_signal_proto protoreflect.FileDescriptorconst file_store_signal_proto_rawDesc = "" +"\n" +- "\x12store/signal.proto\x12\x0ebytebase.store\"\xb3\x01\n" ++ "\x12store/signal.proto\x12\x0ebytebase.store\"\xe9\x01\n" +"\x06Signal\x12/\n" +"\x04type\x18\x01 \x01(\x0e2\x1b.bytebase.store.Signal.TypeR\x04type\x12\x10\n" +"\x03uid\x18\x02 \x01(\x03R\x03uid\x12\x18\n" +- "\aproject\x18\x03 \x01(\tR\aproject\"L\n" ++ "\aproject\x18\x03 \x01(\tR\aproject\x124\n" ++ "\x16approval_input_version\x18\x04 \x01(\x03R\x14approvalInputVersion\"L\n" +"\x04Type\x12\x14\n" +"\x10TYPE_UNSPECIFIED\x10\x00\x12\x19\n" +"\x15CANCEL_PLAN_CHECK_RUN\x10\x01\x12\x13\n" +diff --git a/backend/generated-go/store/signal_equal.pb.go b/backend/generated-go/store/signal_equal.pb.goindex 6a1de7e124..f95904fa38 100644--- a/backend/generated-go/store/signal_equal.pb.go+++ b/backend/generated-go/store/signal_equal.pb.go@@ -19,5 +19,8 @@ func (x *Signal) Equal(y *Signal) bool {if x.Project != y.Project {return false}+ if x.ApprovalInputVersion != y.ApprovalInputVersion {+ return false+ }return true}diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex fd0d1b84e0..248592eed9 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -22,6 +22,7 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/runner/plancheck""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils")@@ -139,7 +140,18 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {return}- approved, err := utils.CheckIssueApproved(issue)+ planID := *issue.PlanUID+ plan, err := r.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: &planID})+ if err != nil {+ slog.Error("failed to get plan for approval check",+ slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID), log.BBError(err))+ return+ }+ if plan == nil {+ return+ }++ approved, err := utils.CheckIssueApprovedForPlan(issue, plan)if err != nil {slog.Error("failed to check if issue is approved",slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID), log.BBError(err))@@ -153,9 +165,6 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {func findApprovalTemplateForIssue(ctx context.Context, stores *store.Store, webhookManager *webhook.Manager, licenseService *enterprise.LicenseService, issue *store.IssueMessage, approvalSetting *storepb.WorkspaceApprovalSetting) error {payload := issue.Payload- if payload.Approval != nil && payload.Approval.ApprovalFindingDone {- return nil- }project, err := stores.GetProjectByResourceID(ctx, issue.ProjectID)if err != nil {@@ -165,31 +174,40 @@ func findApprovalTemplateForIssue(ctx context.Context, stores *store.Store, webhreturn errors.Errorf("project %s not found", issue.ProjectID)}- approvalTemplate, celVarsList, done, err := func() (*storepb.ApprovalTemplate, []map[string]any, bool, error) {+ approvalInputVersion, err := getIssuePlanApprovalInputVersion(ctx, stores, issue)+ if err != nil {+ return err+ }+ if payload.Approval != nil && payload.Approval.ApprovalFindingDone && payload.Approval.GetApprovalInputVersion() == approvalInputVersion {+ return nil+ }++ approvalTemplate, celVarsList, approvalInputVersion, done, err := func() (*storepb.ApprovalTemplate, []map[string]any, int64, bool, error) {// no need to find if feature is not enabledif licenseService.IsFeatureEnabled(ctx, project.Workspace, v1pb.PlanFeature_FEATURE_APPROVAL_WORKFLOW) != nil {// nolint:nilerr- return nil, nil, true, nil+ return nil, nil, approvalInputVersion, true, nil}// Step 1: Determine approval source from issue typeapprovalSource, err := getApprovalSourceFromIssue(ctx, stores, issue)if err != nil {- return nil, nil, false, errors.Wrap(err, "failed to get approval source from issue")+ return nil, nil, approvalInputVersion, false, errors.Wrap(err, "failed to get approval source from issue")}if approvalSource == storepb.WorkspaceApprovalSetting_Rule_SOURCE_UNSPECIFIED {// Cannot determine source, no approval needed- return nil, nil, true, nil+ return nil, nil, approvalInputVersion, true, nil}// Step 2: Build CEL variables for evaluation- celVarsList, done, err := buildCELVariablesForIssue(ctx, stores, issue)+ celVarsList, celApprovalInputVersion, done, err := buildCELVariablesForIssue(ctx, stores, issue)+ approvalInputVersion = celApprovalInputVersionif err != nil {- return nil, nil, false, errors.Wrap(err, "failed to build CEL variables for issue")+ return nil, nil, approvalInputVersion, false, errors.Wrap(err, "failed to build CEL variables for issue")}if !done {// Not ready yet (e.g., waiting for plan check runs)- return nil, nil, false, nil+ return nil, nil, approvalInputVersion, false, nil}// Step 3: Inject risk level into CEL variables for CHANGE_DATABASE issues@@ -203,10 +221,10 @@ func findApprovalTemplateForIssue(ctx context.Context, stores *store.Store, webh// Step 4: Find matching approval templateapprovalTemplate, err := getApprovalTemplate(approvalSetting, approvalSource, celVarsList)if err != nil {- return nil, nil, false, errors.Wrapf(err, "failed to get approval template for source: %v", approvalSource)+ return nil, nil, approvalInputVersion, false, errors.Wrapf(err, "failed to get approval template for source: %v", approvalSource)}- return approvalTemplate, celVarsList, true, nil+ return approvalTemplate, celVarsList, approvalInputVersion, true, nil}()if err != nil {// Don't persist error - it will be logged by caller@@ -222,17 +240,27 @@ func findApprovalTemplateForIssue(ctx context.Context, stores *store.Store, webhriskLevel := calculateRiskLevelFromCELVars(celVarsList)payload.Approval = &storepb.IssuePayloadApproval{- ApprovalFindingDone: true,- ApprovalTemplate: approvalTemplate,- Approvers: nil,+ ApprovalFindingDone: true,+ ApprovalTemplate: approvalTemplate,+ Approvers: nil,+ ApprovalInputVersion: approvalInputVersion,}payload.RiskLevel = riskLevel- if _, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{- Approval: payload.Approval,- RiskLevel: riskLevel,- },+ payloadPatch := &storepb.Issue{+ Approval: payload.Approval,+ RiskLevel: riskLevel,+ }+ if issue.Type == storepb.Issue_DATABASE_CHANGE {+ updated, err := stores.UpdateIssuePayloadIfPlanApprovalInputVersion(ctx, issue.ProjectID, issue.UID, payloadPatch, approvalInputVersion)+ if err != nil {+ return errors.Wrap(err, "failed to update issue payload")+ }+ if !updated {+ return nil+ }+ } else if _, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{+ PayloadUpsert: payloadPatch,}); err != nil {return errors.Wrap(err, "failed to update issue payload")}@@ -242,6 +270,23 @@ func findApprovalTemplateForIssue(ctx context.Context, stores *store.Store, webhreturn nil}+func getIssuePlanApprovalInputVersion(ctx context.Context, stores *store.Store, issue *store.IssueMessage) (int64, error) {+ if issue.Type != storepb.Issue_DATABASE_CHANGE {+ return 0, nil+ }+ if issue.PlanUID == nil {+ return 0, errors.Errorf("expected plan UID in issue %v", issue.UID)+ }+ plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})+ if err != nil {+ return 0, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)+ }+ if plan == nil {+ return 0, errors.Errorf("plan %v not found", *issue.PlanUID)+ }+ return plan.Config.GetApprovalInputVersion(), nil+}+// calculateRiskLevelFromCELVars calculates the risk level from CEL variables.// This is separated from approval flow generation to allow independent evolution.func calculateRiskLevelFromCELVars(celVarsList []map[string]any) storepb.RiskLevel {@@ -387,18 +432,21 @@ func buildFallbackCELVars(celVarsList []map[string]any) []map[string]any {// buildCELVariablesForIssue builds the CEL variable maps for evaluating approval rules.// Returns a list of CEL variable maps (one per task/component), done flag, and error.// done=false means the caller should retry later (e.g., waiting for plan check runs).-func buildCELVariablesForIssue(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, bool, error) {+func buildCELVariablesForIssue(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, int64, bool, error) {switch issue.Type {case storepb.Issue_ROLE_GRANT:- return buildCELVariablesForRoleGrant(ctx, stores, issue)+ celVarsList, done, err := buildCELVariablesForRoleGrant(ctx, stores, issue)+ return celVarsList, 0, done, errcase storepb.Issue_DATABASE_CHANGE:return buildCELVariablesForDatabaseChange(ctx, stores, issue)case storepb.Issue_DATABASE_EXPORT:- return buildCELVariablesForDataExport(ctx, stores, issue)+ celVarsList, done, err := buildCELVariablesForDataExport(ctx, stores, issue)+ return celVarsList, 0, done, errcase storepb.Issue_ACCESS_GRANT:- return buildCELVariablesForAccessGrant(ctx, stores, issue)+ celVarsList, done, err := buildCELVariablesForAccessGrant(ctx, stores, issue)+ return celVarsList, 0, done, errdefault:- return nil, false, errors.Errorf("unknown issue type %v", issue.Type)+ return nil, 0, false, errors.Errorf("unknown issue type %v", issue.Type)}}@@ -543,41 +591,113 @@ func buildStatementSummaryResultMap(results []*storepb.PlanCheckRunResult_Resultreturn m}-func isPlanCheckRunPendingApprovalEvaluation(planCheckRun *store.PlanCheckRunMessage) bool {+func isPlanCheckRunCurrentForApprovalInputVersion(planCheckRun *store.PlanCheckRunMessage, planApprovalInputVersion int64) (current bool, pending bool) {if planCheckRun == nil {- return false+ return false, true}- return planCheckRun.Status == store.PlanCheckRunStatusAvailable || planCheckRun.Status == store.PlanCheckRunStatusRunning+ if planCheckRun.Status == store.PlanCheckRunStatusAvailable || planCheckRun.Status == store.PlanCheckRunStatusRunning {+ return false, true+ }+ return planCheckRun.Result.GetApprovalInputVersion() == planApprovalInputVersion, false}// buildCELVariablesForDatabaseChange builds CEL variables for DATABASE_CHANGE issues.// This includes DDL and DML operations.-func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, bool, error) {+func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, int64, bool, error) {if issue.PlanUID == nil {- return nil, false, errors.Errorf("expected plan UID in issue %v", issue.UID)+ return nil, 0, false, errors.Errorf("expected plan UID in issue %v", issue.UID)}plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})if err != nil {- return nil, false, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)+ return nil, 0, false, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)}if plan == nil {- return nil, false, errors.Errorf("plan %v not found", *issue.PlanUID)+ return nil, 0, false, errors.Errorf("plan %v not found", *issue.PlanUID)}+ approvalInputVersion := plan.Config.GetApprovalInputVersion()- planCheckRun, err := stores.GetPlanCheckRun(ctx, issue.ProjectID, plan.UID)+ checksExpected, err := planChecksExpectedForApproval(ctx, stores, nil, plan)if err != nil {- return nil, false, errors.Wrapf(err, "failed to get plan check run for plan %v", plan.UID)+ return nil, approvalInputVersion, false, errors.Wrap(err, "failed to derive plan check targets")}- // Wait for plan check to complete if available or running.- if isPlanCheckRunPendingApprovalEvaluation(planCheckRun) {- return nil, false, nil // Not ready yet, retry later+ planCheckRun, err := stores.GetPlanCheckRun(ctx, issue.ProjectID, plan.UID)+ if err != nil {+ return nil, approvalInputVersion, false, errors.Wrapf(err, "failed to get plan check run for plan %v", plan.UID)+ }++ if checksExpected {+ if planCheckRun == nil {+ // Some plans can reach approval without a durable check row, for example after+ // legacy data import or a previous row creation failure. Waiting without a row+ // leaves no work for the scheduler to claim.+ _, err := stores.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: issue.ProjectID,+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{+ ApprovalInputVersion: approvalInputVersion,+ },+ })+ if err != nil {+ slog.Error("failed to create missing plan check run for approval input version",+ slog.String("project", issue.ProjectID),+ slog.Int64("issue_uid", issue.UID),+ slog.Int64("plan_uid", plan.UID),+ slog.Int64("approval_input_version", approvalInputVersion),+ log.BBError(err))+ return nil, approvalInputVersion, false, errors.Wrap(err, "failed to create missing plan check run for approval input version")+ }+ return nil, approvalInputVersion, false, nil+ }+ if planCheckRun.Status == store.PlanCheckRunStatusAvailable || planCheckRun.Status == store.PlanCheckRunStatusRunning {+ if planCheckRun.Result.GetApprovalInputVersion() == approvalInputVersion {+ return nil, approvalInputVersion, false, nil+ }+ refreshed, err := stores.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: issue.ProjectID,+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{+ ApprovalInputVersion: approvalInputVersion,+ },+ })+ if err != nil {+ slog.Error("failed to refresh active stale plan check run for approval input version",+ slog.String("project", issue.ProjectID),+ slog.Int64("issue_uid", issue.UID),+ slog.Int64("plan_uid", plan.UID),+ slog.Int64("approval_input_version", approvalInputVersion),+ log.BBError(err))+ return nil, approvalInputVersion, false, errors.Wrap(err, "failed to refresh active stale plan check run for approval input version")+ }+ _ = refreshed+ return nil, approvalInputVersion, false, nil+ }+ current, pending := isPlanCheckRunCurrentForApprovalInputVersion(planCheckRun, approvalInputVersion)+ if pending {+ return nil, approvalInputVersion, false, nil+ }+ if !current {+ refreshed, err := stores.RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx, issue.ProjectID, plan.UID, approvalInputVersion)+ if err != nil {+ slog.Error("failed to refresh stale plan check run for approval input version",+ slog.String("project", issue.ProjectID),+ slog.Int64("issue_uid", issue.UID),+ slog.Int64("plan_uid", plan.UID),+ slog.Int64("approval_input_version", approvalInputVersion),+ log.BBError(err))+ return nil, approvalInputVersion, false, errors.Wrap(err, "failed to refresh stale plan check run for approval input version")+ }+ _ = refreshed+ return nil, approvalInputVersion, false, nil+ }+ } else {+ planCheckRun = nil}// Unfold database groups and get all targetstargets, err := unfoldSpecTargets(ctx, stores, plan.Config.GetSpecs(), issue.ProjectID)if err != nil {- return nil, false, errors.Wrap(err, "failed to unfold spec targets")+ return nil, approvalInputVersion, false, errors.Wrap(err, "failed to unfold spec targets")}statementSummaryResults := map[statementSummaryKey]*storepb.PlanCheckRunResult_Result{}@@ -591,10 +711,10 @@ func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.Storeif target.sheetSha256 != "" {sheet, err := stores.GetSheetFull(ctx, target.sheetSha256)if err != nil {- return nil, true, errors.Wrapf(err, "failed to get statement in sheet %v", target.sheetSha256)+ return nil, approvalInputVersion, true, errors.Wrapf(err, "failed to get statement in sheet %v", target.sheetSha256)}if sheet == nil {- return nil, true, errors.Errorf("sheet %v not found", target.sheetSha256)+ return nil, approvalInputVersion, true, errors.Errorf("sheet %v not found", target.sheetSha256)}taskStatement = sheet.Statement}@@ -654,7 +774,79 @@ func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.StorecelVarsList = append(celVarsList, map[string]any{})}- return celVarsList, true, nil+ return celVarsList, approvalInputVersion, true, nil+}++func planChecksExpectedForApproval(ctx context.Context, stores *store.Store, project *store.ProjectMessage, plan *store.PlanMessage) (bool, error) {+ if project == nil {+ var err error+ project, err = stores.GetProjectByResourceID(ctx, plan.ProjectID)+ if err != nil {+ return false, errors.Wrap(err, "failed to get project")+ }+ if project == nil {+ return false, errors.Errorf("project %s not found", plan.ProjectID)+ }+ }++ databaseGroup, err := getDatabaseGroupForPlanApproval(ctx, stores, plan)+ if err != nil {+ return false, err+ }++ targets, err := plancheck.DeriveCheckTargets(ctx, stores, project, plan, databaseGroup)+ if err != nil {+ return false, err+ }+ return len(targets) > 0, nil+}++func getDatabaseGroupForPlanApproval(ctx context.Context, stores *store.Store, plan *store.PlanMessage) (*v1pb.DatabaseGroup, error) {+ for _, spec := range plan.Config.GetSpecs() {+ cfg, ok := spec.Config.(*storepb.PlanConfig_Spec_ChangeDatabaseConfig)+ if !ok {+ continue+ }+ if len(cfg.ChangeDatabaseConfig.Targets) != 1 {+ continue+ }++ target := cfg.ChangeDatabaseConfig.Targets[0]+ _, databaseGroupID, err := common.GetProjectIDDatabaseGroupID(target)+ if err != nil {+ continue+ }++ dbGroup, err := stores.GetDatabaseGroup(ctx, &store.FindDatabaseGroupMessage{+ ResourceID: &databaseGroupID,+ ProjectIDs: []string{plan.ProjectID},+ })+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get database group %q", target)+ }+ if dbGroup == nil {+ return nil, errors.Errorf("database group %q not found", target)+ }++ allDatabases, err := stores.ListDatabases(ctx, &store.FindDatabaseMessage{ProjectID: &plan.ProjectID})+ if err != nil {+ return nil, errors.Wrapf(err, "failed to list databases for project %q", plan.ProjectID)+ }++ matchedDatabases, err := utils.GetMatchedDatabasesInDatabaseGroup(ctx, dbGroup, allDatabases)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get matched databases for group %q", databaseGroupID)+ }++ result := &v1pb.DatabaseGroup{Name: target}+ for _, db := range matchedDatabases {+ result.MatchedDatabases = append(result.MatchedDatabases, &v1pb.DatabaseGroup_Database{+ Name: common.FormatDatabase(db.InstanceID, db.DatabaseName),+ })+ }+ return result, nil+ }+ return nil, nil}// buildCELVariablesForDataExport builds CEL variables for DATABASE_EXPORT issues.diff --git a/backend/runner/approval/runner_test.go b/backend/runner/approval/runner_test.goindex 1b0fcf736e..c008f3993b 100644--- a/backend/runner/approval/runner_test.go+++ b/backend/runner/approval/runner_test.go@@ -1,13 +1,17 @@package approvalimport (+ "context"+ "fmt""testing""github.com/stretchr/testify/require""google.golang.org/genproto/googleapis/type/expr""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/testcontainer"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/migrator""github.com/bytebase/bytebase/backend/store")@@ -235,35 +239,216 @@ func TestBuildStatementSummaryResultMapUsesSheetSHA256(t *testing.T) {}].GetSqlSummaryReport().GetAffectedRows())}-func TestIsPlanCheckRunPendingApprovalEvaluation(t *testing.T) {+func TestPlanChecksExpectedForApproval(t *testing.T) {+ tests := []struct {+ name string+ plan *store.PlanMessage+ want bool+ }{+ {+ name: "create database does not expect checks",+ plan: &store.PlanMessage{+ ProjectID: "project",+ Config: &storepb.PlanConfig{+ Specs: []*storepb.PlanConfig_Spec{{+ Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{+ CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{},+ },+ }},+ },+ },+ },+ {+ name: "release backed change does not expect checks",+ plan: &store.PlanMessage{+ ProjectID: "project",+ Config: &storepb.PlanConfig{+ Specs: []*storepb.PlanConfig_Spec{{+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{+ Release: "projects/project/releases/release",+ },+ },+ }},+ },+ },+ },+ {+ name: "empty expanded targets do not expect checks",+ plan: &store.PlanMessage{+ ProjectID: "project",+ Config: &storepb.PlanConfig{+ Specs: []*storepb.PlanConfig_Spec{{+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{},+ },+ }},+ },+ },+ },+ {+ name: "change target expects checks",+ plan: &store.PlanMessage{+ ProjectID: "project",+ Config: &storepb.PlanConfig{+ Specs: []*storepb.PlanConfig_Spec{{+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{+ Targets: []string{"instances/prod/databases/app"},+ },+ },+ }},+ },+ },+ want: true,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := planChecksExpectedForApproval(context.Background(), nil, &store.ProjectMessage{ResourceID: "project"}, tt.plan)+ require.NoError(t, err)+ require.Equal(t, tt.want, got)+ })+ }+}++func TestIsPlanCheckRunCurrentForApprovalInputVersion(t *testing.T) {tests := []struct {name string+ planVersion int64planCheckRun *store.PlanCheckRunMessage- want bool+ wantCurrent bool+ wantPending bool}{{- name: "nil plan check run is ready to evaluate",+ name: "nil run waits for plan check",+ planVersion: 0,planCheckRun: nil,+ wantPending: true,+ },+ {+ name: "available run is pending",+ planVersion: 2,+ planCheckRun: &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusAvailable,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ },+ wantPending: true,+ },+ {+ name: "running run is pending",+ planVersion: 2,+ planCheckRun: &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusRunning,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ },+ wantPending: true,},{- name: "AVAILABLE is not ready",- planCheckRun: &store.PlanCheckRunMessage{Status: store.PlanCheckRunStatusAvailable},- want: true,+ name: "done matching version is current",+ planVersion: 2,+ planCheckRun: &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusDone,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ },+ wantCurrent: true,},{- name: "RUNNING is not ready",- planCheckRun: &store.PlanCheckRunMessage{Status: store.PlanCheckRunStatusRunning},- want: true,+ name: "done stale version is not current",+ planVersion: 2,+ planCheckRun: &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusDone,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ },},{- name: "DONE is ready",- planCheckRun: &store.PlanCheckRunMessage{Status: store.PlanCheckRunStatusDone},+ name: "missing result defaults to version zero",+ planVersion: 0,+ planCheckRun: &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusDone,+ Result: &storepb.PlanCheckRunResult{},+ },+ wantCurrent: true,},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- require.Equal(t, tt.want, isPlanCheckRunPendingApprovalEvaluation(tt.planCheckRun))+ current, pending := isPlanCheckRunCurrentForApprovalInputVersion(tt.planCheckRun, tt.planVersion)+ require.Equal(t, tt.wantCurrent, current)+ require.Equal(t, tt.wantPending, pending)})}}++func TestBuildCELVariablesForDatabaseChangeCreatesMissingPlanCheckRun(t *testing.T) {+ ctx := context.Background()+ s := setupApprovalRunnerStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{+ ApprovalInputVersion: 2,+ Specs: []*storepb.PlanConfig_Spec{{+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{+ Targets: []string{"instances/prod/databases/app"},+ },+ },+ }},+ },+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{},+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ celVarsList, approvalInputVersion, done, err := buildCELVariablesForDatabaseChange(ctx, s, issue)+ require.NoError(t, err)+ require.False(t, done)+ require.Nil(t, celVarsList)+ require.EqualValues(t, 2, approvalInputVersion)++ planCheckRun, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, planCheckRun)+ require.Equal(t, store.PlanCheckRunStatusAvailable, planCheckRun.Status)+ require.EqualValues(t, 2, planCheckRun.Result.GetApprovalInputVersion())+}++func setupApprovalRunnerStore(ctx context.Context, t *testing.T) *store.Store {+ t.Helper()++ container := testcontainer.GetTestPgContainer(ctx, t)+ t.Cleanup(func() { container.Close(ctx) })++ db := container.GetDB()+ require.NoError(t, migrator.MigrateSchema(ctx, db))++ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ `)+ require.NoError(t, err)++ pgURL := fmt.Sprintf(+ "host=%s port=%s user=postgres password=root-password database=postgres",+ container.GetHost(), container.GetPort(),+ )+ s, err := store.New(ctx, pgURL, false)+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, s.Close()) })+ return s+}diff --git a/backend/runner/notifylistener/listener.go b/backend/runner/notifylistener/listener.goindex 5c794f44bf..445aae7f40 100644--- a/backend/runner/notifylistener/listener.go+++ b/backend/runner/notifylistener/listener.go@@ -90,7 +90,7 @@ func (l *Listener) handleNotification(payload string) {switch signal.Type {case storepb.Signal_CANCEL_PLAN_CHECK_RUN:- ref := bus.PlanCheckRunRef{ProjectID: signal.Project, UID: signal.Uid}+ ref := bus.PlanCheckRunRef{ProjectID: signal.Project, UID: signal.Uid, ApprovalInputVersion: signal.ApprovalInputVersion}if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(ref); ok {cancel.(context.CancelFunc)()}diff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.goindex 45f18c6d03..75cc9378a8 100644--- a/backend/runner/plancheck/scheduler.go+++ b/backend/runner/plancheck/scheduler.go@@ -86,49 +86,60 @@ func (s *Scheduler) runOnce(ctx context.Context) {}for _, c := range claimed {- go s.runPlanCheckRun(ctx, c.ProjectID, c.UID, c.PlanUID)+ go s.runPlanCheckRun(ctx, c.ProjectID, c.UID, c.PlanUID, c.ApprovalInputVersion)}}-func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid int64, planUID int64) {+func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid int64, planUID int64, approvalInputVersion int64) {ctxWithCancel, cancel := context.WithCancel(ctx)defer cancel()- planCheckRef := bus.PlanCheckRunRef{ProjectID: projectID, UID: uid}+ planCheckRef := bus.PlanCheckRunRef{ProjectID: projectID, UID: uid, ApprovalInputVersion: approvalInputVersion}s.bus.RunningPlanCheckRunsCancelFunc.Store(planCheckRef, cancel)defer s.bus.RunningPlanCheckRunsCancelFunc.Delete(planCheckRef)// Fetch plan to derive check targets at runtimeplan, err := s.store.GetPlan(ctxWithCancel, &store.FindPlanMessage{ProjectID: projectID, UID: &planUID})if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())return}if plan == nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, "plan not found")+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, "plan not found")+ return+ }++ if plan.Config.GetApprovalInputVersion() != approvalInputVersion {+ slog.Info("skip stale plan check run",+ slog.String("project", projectID),+ slog.Int64("plan_id", planUID),+ slog.Int64("plan_check_run_id", uid),+ slog.Int64("claimed_approval_input_version", approvalInputVersion),+ slog.Int64("current_approval_input_version", plan.Config.GetApprovalInputVersion()))+ s.markPlanCheckRunCanceled(ctxWithCancel, projectID, uid, approvalInputVersion, "stale plan check run")return}project, err := s.store.GetProjectByResourceID(ctxWithCancel, plan.ProjectID)if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())return}if project == nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, "project not found")+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, "project not found")return}// Get database group if needed (for spec expansion)databaseGroup, err := s.getDatabaseGroupForPlan(ctxWithCancel, plan)if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())return}// Derive check targets from plantargets, err := DeriveCheckTargets(ctxWithCancel, s.store, project, plan, databaseGroup)if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())return}@@ -143,28 +154,39 @@ func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid i}if err != nil {if errors.Is(err, context.Canceled) {- s.markPlanCheckRunCanceled(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunCanceled(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())} else {- s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, approvalInputVersion, err.Error())}} else {- s.markPlanCheckRunDone(ctxWithCancel, projectID, uid, planUID, results)+ s.markPlanCheckRunDone(ctxWithCancel, projectID, uid, planUID, approvalInputVersion, results)}}-func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string, uid int64, planUID int64, results []*storepb.PlanCheckRunResult_Result) {+func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string, uid int64, planUID int64, approvalInputVersion int64, results []*storepb.PlanCheckRunResult_Result) {result := &storepb.PlanCheckRunResult{- Results: results,+ Results: results,+ ApprovalInputVersion: approvalInputVersion,}- if err := s.store.UpdatePlanCheckRun(ctx,+ updated, err := s.store.UpdatePlanCheckRunIfApprovalInputVersion(ctx,projectID,store.PlanCheckRunStatusDone,result,uid,- ); err != nil {+ approvalInputVersion,+ )+ if err != nil {slog.Error("failed to mark plan check run done", log.BBError(err))return}+ if !updated {+ slog.Info("skip stale plan check run done update",+ slog.String("project", projectID),+ slog.Int64("plan_id", planUID),+ slog.Int64("plan_check_run_id", uid),+ slog.Int64("claimed_approval_input_version", approvalInputVersion))+ return+ }// Trigger approval finding after plan checks complete.// The approval runner will trigger rollout creation after it finishes.@@ -181,31 +203,51 @@ func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string,}}-func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string, uid int64, reason string) {+func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string, uid int64, approvalInputVersion int64, reason string) {result := &storepb.PlanCheckRunResult{- Error: reason,+ Error: reason,+ ApprovalInputVersion: approvalInputVersion,}- if err := s.store.UpdatePlanCheckRun(ctx,+ updated, err := s.store.UpdatePlanCheckRunIfApprovalInputVersion(ctx,projectID,store.PlanCheckRunStatusFailed,result,uid,- ); err != nil {+ approvalInputVersion,+ )+ if err != nil {slog.Error("failed to mark plan check run failed", log.BBError(err))+ return+ }+ if !updated {+ slog.Info("skip stale plan check run failed update",+ slog.String("project", projectID),+ slog.Int64("plan_check_run_id", uid),+ slog.Int64("claimed_approval_input_version", approvalInputVersion))}}-func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, projectID string, uid int64, reason string) {+func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, projectID string, uid int64, approvalInputVersion int64, reason string) {result := &storepb.PlanCheckRunResult{- Error: reason,+ Error: reason,+ ApprovalInputVersion: approvalInputVersion,}- if err := s.store.UpdatePlanCheckRun(ctx,+ updated, err := s.store.UpdatePlanCheckRunIfApprovalInputVersion(ctx,projectID,store.PlanCheckRunStatusCanceled,result,uid,- ); err != nil {+ approvalInputVersion,+ )+ if err != nil {slog.Error("failed to mark plan check run canceled", log.BBError(err))+ return+ }+ if !updated {+ slog.Info("skip stale plan check run canceled update",+ slog.String("project", projectID),+ slog.Int64("plan_check_run_id", uid),+ slog.Int64("claimed_approval_input_version", approvalInputVersion))}}diff --git a/backend/runner/taskrun/rollout_creator.go b/backend/runner/taskrun/rollout_creator.goindex e4981b6433..ec9b254fac 100644--- a/backend/runner/taskrun/rollout_creator.go+++ b/backend/runner/taskrun/rollout_creator.go@@ -121,7 +121,7 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, ref bus.PlanRef)}// Check approval status (must be approved)- approved, err := utils.CheckIssueApproved(issue)+ approved, err := utils.CheckIssueApprovedForPlan(issue, plan)if err != nil {slog.Error("failed to check if the issue is approved",slog.String("project", ref.ProjectID),@@ -146,8 +146,8 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, ref bus.PlanRef)return}- // If plan checks exist, they must be DONE with no errors- if planCheckRun != nil {+ // If current plan checks exist, they must be DONE with no errors.+ if planCheckRunBlocksRollout(plan, planCheckRun) {// Check if plan checks are in DONE statusif planCheckRun.Status != store.PlanCheckRunStatusDone {slog.Debug("plan checks not in DONE status, skipping rollout creation",@@ -176,6 +176,12 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, ref bus.PlanRef)// Create rollout and pending tasks// Use issue creator's email since this is auto-rollout for their issueif err := apiv1.CreateRolloutAndPendingTasks(ctx, rc.store, issue.CreatorEmail, plan, issue, project, nil); err != nil {+ if apiv1.IsStaleRolloutApprovalError(err) {+ slog.Info("skip auto-rollout because issue approval is stale",+ slog.String("project", ref.ProjectID),+ slog.Int("plan_id", int(planID)))+ return+ }slog.Error("failed to create rollout and pending tasks",slog.String("project", ref.ProjectID),slog.Int("plan_id", int(planID)),@@ -189,6 +195,13 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, ref bus.PlanRef)slog.Info("successfully auto-created rollout", slog.String("project", ref.ProjectID), slog.Int("plan_id", int(planID)))}+func planCheckRunBlocksRollout(plan *store.PlanMessage, planCheckRun *store.PlanCheckRunMessage) bool {+ if planCheckRun == nil {+ return false+ }+ return planCheckRun.Result.GetApprovalInputVersion() == plan.Config.GetApprovalInputVersion()+}+// hasOnlyChangeDatabaseSpecs checks if every spec in the plan is a change database spec.// Only plans with change database specs support auto-creation of rollouts.func hasOnlyChangeDatabaseSpecs(plan *store.PlanMessage) bool {diff --git a/backend/runner/taskrun/rollout_creator_test.go b/backend/runner/taskrun/rollout_creator_test.gonew file mode 100644index 0000000000..0facc422e0--- /dev/null+++ b/backend/runner/taskrun/rollout_creator_test.go@@ -0,0 +1,26 @@+package taskrun++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store"+)++func TestPlanCheckRunBlocksRolloutIgnoresStaleVersion(t *testing.T) {+ plan := &store.PlanMessage{+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }++ require.False(t, planCheckRunBlocksRollout(plan, nil))+ require.False(t, planCheckRunBlocksRollout(plan, &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusRunning,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ }))+ require.True(t, planCheckRunBlocksRollout(plan, &store.PlanCheckRunMessage{+ Status: store.PlanCheckRunStatusRunning,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ }))+}diff --git a/backend/store/issue.go b/backend/store/issue.goindex c876db6738..d773cde8e0 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -274,6 +274,130 @@ func (s *Store) UpdateIssue(ctx context.Context, projectID string, uid int64, pareturn s.GetIssue(ctx, &FindIssueMessage{ProjectIDs: []string{projectID}, UID: &uid})}+func (s *Store) UpdateIssuePayloadIfPlanApprovalInputVersion(ctx context.Context, projectID string, uid int64, payload *storepb.Issue, approvalInputVersion int64) (bool, error) {+ p, err := protojson.Marshal(payload)+ if err != nil {+ return false, errors.Wrapf(err, "failed to marshal payload")+ }++ q := qb.Q().Space(`+ UPDATE issue+ SET+ updated_at = ?,+ payload = payload || ?+ WHERE project = ?+ AND id = ?+ AND EXISTS (+ SELECT 1+ FROM plan+ WHERE plan.project = issue.project+ AND plan.id = issue.plan_id+ AND COALESCE((plan.config->>'approvalInputVersion')::bigint, 0) = ?+ )`, time.Now(), p, projectID, uid, approvalInputVersion)++ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ result, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, err+ }+ rowsAffected, err := result.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect issue update")+ }+ return rowsAffected > 0, nil+}++// ResetIssueApprovalFindingIfPlanApprovalInputVersion avoids clobbering approval work that raced+// ahead after a new plan check row became visible for the same plan version.+func (s *Store) ResetIssueApprovalFindingIfPlanApprovalInputVersion(ctx context.Context, projectID string, uid int64, approvalInputVersion int64) (bool, error) {+ payload := &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: false,+ ApprovalInputVersion: approvalInputVersion,+ },+ }+ p, err := protojson.Marshal(payload)+ if err != nil {+ return false, errors.Wrapf(err, "failed to marshal payload")+ }++ q := qb.Q().Space(`+ UPDATE issue+ SET+ updated_at = ?,+ payload = payload || ?+ WHERE project = ?+ AND id = ?+ AND EXISTS (+ SELECT 1+ FROM plan+ WHERE plan.project = issue.project+ AND plan.id = issue.plan_id+ AND COALESCE((plan.config->>'approvalInputVersion')::bigint, 0) = ?+ )+ AND NOT (+ COALESCE((payload->'approval'->>'approvalFindingDone')::boolean, false)+ AND COALESCE((payload->'approval'->>'approvalInputVersion')::bigint, 0) = ?+ )`,+ time.Now(), p, projectID, uid, approvalInputVersion, approvalInputVersion)++ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ result, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, err+ }+ rowsAffected, err := result.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect issue approval reset")+ }+ return rowsAffected > 0, nil+}++// UpdateIssuePayloadIfCurrentApprovalInputVersion updates the issue payload only when both the issue approval payload+// and its plan still match the observed approval input version.+func (s *Store) UpdateIssuePayloadIfCurrentApprovalInputVersion(ctx context.Context, projectID string, uid int64, payload *storepb.Issue, approvalInputVersion int64) (bool, error) {+ p, err := protojson.Marshal(payload)+ if err != nil {+ return false, errors.Wrapf(err, "failed to marshal payload")+ }++ q := qb.Q().Space(`+ UPDATE issue+ SET+ updated_at = ?,+ payload = payload || ?+ WHERE project = ?+ AND id = ?+ AND COALESCE((payload->'approval'->>'approvalInputVersion')::bigint, 0) = ?+ AND EXISTS (+ SELECT 1+ FROM plan+ WHERE plan.project = issue.project+ AND plan.id = issue.plan_id+ AND COALESCE((plan.config->>'approvalInputVersion')::bigint, 0) = ?+ )`, time.Now(), p, projectID, uid, approvalInputVersion, approvalInputVersion)++ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ result, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, err+ }+ rowsAffected, err := result.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect issue update")+ }+ return rowsAffected > 0, nil+}+// ListIssues returns the list of issues by find query.func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*IssueMessage, error) {orderByClause := "ORDER BY issue.id DESC"diff --git a/backend/store/plan.go b/backend/store/plan.goindex 50b614b88d..b14e34dc55 100644--- a/backend/store/plan.go+++ b/backend/store/plan.go@@ -2,6 +2,7 @@ package storeimport ("context"+ "database/sql""strings""time"@@ -18,6 +19,9 @@ import (v1pb "github.com/bytebase/bytebase/backend/generated-go/v1")+// ErrPlanHasRollout indicates that a plan update was rejected because rollout already started.+var ErrPlanHasRollout = errors.New("plan has rollout")+// PlanMessage is the message for plan.type PlanMessage struct {ProjectID string@@ -62,9 +66,13 @@ type UpdatePlanMessage struct {// BumpApprovalInputVersion increments config.approvalInputVersion from the// current stored row while applying Config as a full replacement. Use this// only for approval-relevant full config replacements such as spec updates.- // This flag is not a partial JSONB patch mechanism for unrelated config fields.BumpApprovalInputVersion bool- Deleted *bool+ // RequireNoRollout rejects full config replacements once rollout marking has+ // won the race. This is separate from BumpApprovalInputVersion because the+ // user-visible invariant is about spec immutability after rollout, including+ // spec edits that do not otherwise affect approval input.+ RequireNoRollout bool+ Deleted *bool}// CreatePlan creates a new plan.@@ -242,9 +250,14 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) (*Plan}}- q := qb.Q().Space(`UPDATE plan SET ? WHERE id = ? AND project = ?+ where := qb.Q().Space("id = ? AND project = ?", patch.UID, patch.ProjectID)+ if patch.RequireNoRollout {+ where.Space("AND COALESCE((config->>'hasRollout')::boolean, false) = false")+ }++ q := qb.Q().Space(`UPDATE plan SET ? WHERE ?RETURNING id, creator, created_at, updated_at, project, name, description, config, deleted`,- set, patch.UID, patch.ProjectID)+ set, where)query, finalArgs, err := q.ToSQL()if err != nil {@@ -266,6 +279,9 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) (*Plan&config,&plan.Deleted,); err != nil {+ if patch.RequireNoRollout && errors.Is(err, sql.ErrNoRows) {+ return nil, ErrPlanHasRollout+ }return nil, errors.Wrapf(err, "failed to update plan")}if err := common.ProtojsonUnmarshaler.Unmarshal(config, plan.Config); err != nil {diff --git a/backend/store/plan_approval_input_version_test.go b/backend/store/plan_approval_input_version_test.goindex 8016ea2109..dbf7b1db85 100644--- a/backend/store/plan_approval_input_version_test.go+++ b/backend/store/plan_approval_input_version_test.go@@ -6,6 +6,7 @@ import ("testing""github.com/stretchr/testify/require"+ "google.golang.org/protobuf/proto""github.com/bytebase/bytebase/backend/common/testcontainer"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -97,12 +98,423 @@ func TestUpdatePlanBumpsApprovalInputVersionOnlyWhenRequested(t *testing.T) {requirePlanSpecID(t, updated.Config, "spec-e")}+func TestCreateRolloutTasksRequiresMatchingApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{+ ApprovalInputVersion: 2,+ Specs: []*storepb.PlanConfig_Spec{{Id: "spec-a"}},+ },+ }, "[email protected]")+ require.NoError(t, err)++ staleVersion := int64(1)+ updated, createdTasks, err := s.CreateRolloutTasks(ctx, "project-a", plan.UID, &staleVersion, nil)+ require.NoError(t, err)+ require.False(t, updated)+ require.Empty(t, createdTasks)++ got, err := s.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.False(t, got.Config.GetHasRollout())+ require.EqualValues(t, 2, got.Config.GetApprovalInputVersion())+ requirePlanSpecID(t, got.Config, "spec-a")++ currentVersion := int64(2)+ updated, createdTasks, err = s.CreateRolloutTasks(ctx, "project-a", plan.UID, ¤tVersion, nil)+ require.NoError(t, err)+ require.True(t, updated)+ require.Empty(t, createdTasks)++ got, err = s.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.True(t, got.Config.GetHasRollout())+ require.EqualValues(t, 2, got.Config.GetApprovalInputVersion())+ requirePlanSpecID(t, got.Config, "spec-a")+}++func TestCreateRolloutTasksAddsMissingTasksAfterRolloutExists(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ updated, createdTasks, err := s.CreateRolloutTasks(ctx, "project-a", plan.UID, nil, []*store.TaskMessage{+ newTestRolloutTask("instance-a", "database-a", "sheet-a"),+ })+ require.NoError(t, err)+ require.True(t, updated)+ require.Len(t, createdTasks, 1)++ got, err := s.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.True(t, got.Config.GetHasRollout())++ updated, createdTasks, err = s.CreateRolloutTasks(ctx, "project-a", plan.UID, nil, []*store.TaskMessage{+ newTestRolloutTask("instance-a", "database-a", "sheet-a"),+ newTestRolloutTask("instance-a", "database-b", "sheet-b"),+ })+ require.NoError(t, err)+ require.True(t, updated)+ require.Len(t, createdTasks, 1)+ require.Equal(t, "instance-a", createdTasks[0].InstanceID)+ require.Equal(t, "database-b", createdTasks[0].GetDatabaseName())+ require.Equal(t, "sheet-b", createdTasks[0].Payload.GetSheetSha256())+}++func TestUpdatePlanRequireNoRolloutDoesNotOverwriteRollout(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{+ ApprovalInputVersion: 1,+ Specs: []*storepb.PlanConfig_Spec{{Id: "spec-a"}},+ },+ }, "[email protected]")+ require.NoError(t, err)++ staleConfig := proto.CloneOf(plan.Config)+ staleConfig.Specs = []*storepb.PlanConfig_Spec{{Id: "spec-b"}}++ approvalInputVersion := int64(1)+ marked, _, err := s.CreateRolloutTasks(ctx, "project-a", plan.UID, &approvalInputVersion, nil)+ require.NoError(t, err)+ require.True(t, marked)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: "project-a",+ Config: staleConfig,+ BumpApprovalInputVersion: true,+ RequireNoRollout: true,+ })+ require.Error(t, err)++ got, err := s.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.True(t, got.Config.GetHasRollout())+ require.EqualValues(t, 1, got.Config.GetApprovalInputVersion())+ requirePlanSpecID(t, got.Config, "spec-a")+}++func TestUpdatePlanRequireNoRolloutRejectsConfigUpdateAfterRollout(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{+ ApprovalInputVersion: 1,+ Specs: []*storepb.PlanConfig_Spec{{Id: "spec-a"}},+ },+ }, "[email protected]")+ require.NoError(t, err)++ staleConfig := proto.CloneOf(plan.Config)+ staleConfig.Specs = []*storepb.PlanConfig_Spec{{Id: "spec-b"}}++ approvalInputVersion := int64(1)+ marked, _, err := s.CreateRolloutTasks(ctx, "project-a", plan.UID, &approvalInputVersion, nil)+ require.NoError(t, err)+ require.True(t, marked)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: "project-a",+ Config: staleConfig,+ RequireNoRollout: true,+ })+ require.Error(t, err)+ require.ErrorIs(t, err, store.ErrPlanHasRollout)++ got, err := s.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.True(t, got.Config.GetHasRollout())+ require.EqualValues(t, 1, got.Config.GetApprovalInputVersion())+ requirePlanSpecID(t, got.Config, "spec-a")+}++func TestUpdateIssuePayloadIfPlanApprovalInputVersionUpdatesMatchingVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{RiskLevel: storepb.RiskLevel_LOW},+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updated, err := s.UpdateIssuePayloadIfPlanApprovalInputVersion(ctx, "project-a", issue.UID, &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 2,+ },+ }, 2)+ require.NoError(t, err)+ require.True(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_HIGH, got.Payload.GetRiskLevel())+ require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())+ require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())+}++func TestUpdateIssuePayloadIfPlanApprovalInputVersionSkipsStaleVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{RiskLevel: storepb.RiskLevel_LOW},+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updated, err := s.UpdateIssuePayloadIfPlanApprovalInputVersion(ctx, "project-a", issue.UID, &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ }, 1)+ require.NoError(t, err)+ require.False(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_LOW, got.Payload.GetRiskLevel())+ require.Nil(t, got.Payload.GetApproval())+}++func TestUpdateIssuePayloadIfPlanApprovalInputVersionSkipsIssueWithoutPlan(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{RiskLevel: storepb.RiskLevel_LOW},+ })+ require.NoError(t, err)++ updated, err := s.UpdateIssuePayloadIfPlanApprovalInputVersion(ctx, "project-a", issue.UID, &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ },+ }, 0)+ require.NoError(t, err)+ require.False(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_LOW, got.Payload.GetRiskLevel())+ require.Nil(t, got.Payload.GetApproval())+}++func TestResetIssueApprovalFindingIfPlanApprovalInputVersionSkipsCurrentDoneApproval(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 2,+ },+ },+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updated, err := s.ResetIssueApprovalFindingIfPlanApprovalInputVersion(ctx, "project-a", issue.UID, 2)+ require.NoError(t, err)+ require.False(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_HIGH, got.Payload.GetRiskLevel())+ require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())+ require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())+}++func TestUpdateIssuePayloadIfCurrentApprovalInputVersionUpdatesMatchingVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{+ RiskLevel: storepb.RiskLevel_LOW,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 2,+ },+ },+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updated, err := s.UpdateIssuePayloadIfCurrentApprovalInputVersion(ctx, "project-a", issue.UID, &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 2,+ },+ }, 2)+ require.NoError(t, err)+ require.True(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_HIGH, got.Payload.GetRiskLevel())+ require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())+ require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())+}++func TestUpdateIssuePayloadIfCurrentApprovalInputVersionSkipsStaleIssueApprovalVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanApprovalInputVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ issue, err := s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "issue-a",+ Type: storepb.Issue_DATABASE_CHANGE,+ Description: "",+ Payload: &storepb.Issue{+ RiskLevel: storepb.RiskLevel_LOW,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ },+ PlanUID: &plan.UID,+ })+ require.NoError(t, err)++ updated, err := s.UpdateIssuePayloadIfCurrentApprovalInputVersion(ctx, "project-a", issue.UID, &storepb.Issue{+ RiskLevel: storepb.RiskLevel_HIGH,+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ }, 2)+ require.NoError(t, err)+ require.False(t, updated)++ got, err := s.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"}, UID: &issue.UID})+ require.NoError(t, err)+ require.NotNil(t, got)+ require.Equal(t, storepb.RiskLevel_LOW, got.Payload.GetRiskLevel())+ require.EqualValues(t, 1, got.Payload.GetApproval().GetApprovalInputVersion())+}+func requirePlanSpecID(t *testing.T, config *storepb.PlanConfig, id string) {t.Helper()require.Len(t, config.GetSpecs(), 1)require.Equal(t, id, config.GetSpecs()[0].GetId())}+func newTestRolloutTask(instanceID string, databaseName string, sheetSha256 string) *store.TaskMessage {+ return &store.TaskMessage{+ InstanceID: instanceID,+ DatabaseName: &databaseName,+ Type: storepb.Task_DATABASE_MIGRATE,+ Payload: &storepb.Task{+ Source: &storepb.Task_SheetSha256{+ SheetSha256: sheetSha256,+ },+ },+ }+}+func setupPlanApprovalInputVersionStore(ctx context.Context, t *testing.T) *store.Store {t.Helper()@@ -116,6 +528,7 @@ func setupPlanApprovalInputVersionStore(ctx context.Context, t *testing.T) *storINSERT INTO workspace (resource_id) VALUES ('default');INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ INSERT INTO instance (resource_id, workspace) VALUES ('instance-a', 'default');`)require.NoError(t, err)diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.goindex d589311cbc..aa374572a7 100644--- a/backend/store/plan_check_run.go+++ b/backend/store/plan_check_run.go@@ -52,41 +52,61 @@ type FindPlanCheckRunMessage struct {ResultStatus *[]storepb.Advice_Status}-// CreatePlanCheckRun creates or replaces the plan check run for a plan.-// Always creates with AVAILABLE status for HA-safe scheduling.-func (s *Store) CreatePlanCheckRun(ctx context.Context, create *PlanCheckRunMessage) error {+// CreatePlanCheckRun creates or refreshes the plan check run for a plan.+// It skips active same-version rows to avoid resetting checks already queued or running.+func (s *Store) CreatePlanCheckRun(ctx context.Context, create *PlanCheckRunMessage) (bool, error) {result, err := protojson.Marshal(create.Result)if err != nil {- return errors.Wrapf(err, "failed to marshal result")+ return false, errors.Wrapf(err, "failed to marshal result")}tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {- return errors.Wrapf(err, "failed to begin tx")+ return false, errors.Wrapf(err, "failed to begin tx")}defer tx.Rollback()nextID, err := nextProjectID(ctx, tx, "plan_check_run", create.ProjectID)if err != nil {- return err+ return false, err}+ approvalInputVersion := create.Result.GetApprovalInputVersion()query := `INSERT INTO plan_check_run (id, project, plan_id, status, result)- VALUES ($1, $2, $3, $4, $5)+ SELECT $1, $2, $3, $4, $5+ FROM plan+ WHERE plan.project = $2+ AND plan.id = $3+ AND COALESCE((plan.config->>'approvalInputVersion')::bigint, 0) = $6ON CONFLICT (project, plan_id) DO UPDATE SETstatus = EXCLUDED.status,result = EXCLUDED.result,updated_at = now()+ WHERE (plan_check_run.status NOT IN ($7, $8)+ OR COALESCE((plan_check_run.result->>'approvalInputVersion')::bigint, 0) != COALESCE((EXCLUDED.result->>'approvalInputVersion')::bigint, 0)+ )+ AND EXISTS (+ SELECT 1+ FROM plan+ WHERE plan.project = plan_check_run.project+ AND plan.id = plan_check_run.plan_id+ AND COALESCE((plan.config->>'approvalInputVersion')::bigint, 0) = $9+ )`- if _, err := tx.ExecContext(ctx, query, nextID, create.ProjectID, create.PlanUID, PlanCheckRunStatusAvailable, result); err != nil {- return errors.Wrapf(err, "failed to upsert plan check run")+ sqlResult, err := tx.ExecContext(ctx, query, nextID, create.ProjectID, create.PlanUID, PlanCheckRunStatusAvailable, result, approvalInputVersion, PlanCheckRunStatusAvailable, PlanCheckRunStatusRunning, approvalInputVersion)+ if err != nil {+ return false, errors.Wrapf(err, "failed to upsert plan check run")+ }+ rowsAffected, err := sqlResult.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect plan check run upsert")}if err := tx.Commit(); err != nil {- return errors.Wrapf(err, "failed to commit tx")+ return false, errors.Wrapf(err, "failed to commit tx")}- return nil+ return rowsAffected > 0, nil}// ListPlanCheckRuns returns a list of plan check runs based on find.@@ -207,6 +227,80 @@ func (s *Store) UpdatePlanCheckRun(ctx context.Context, projectID string, statusreturn nil}+// UpdatePlanCheckRunIfApprovalInputVersion updates a running plan check run only when it still matches the claimed approval input version.+//+// Plan check workers finish against the row version they claimed. We do not join back to the+// current plan here because plan edits move the row forward separately; this update only prevents+// an old worker from publishing over a row that has already been refreshed.+func (s *Store) UpdatePlanCheckRunIfApprovalInputVersion(ctx context.Context, projectID string, status PlanCheckRunStatus, result *storepb.PlanCheckRunResult, uid int64, approvalInputVersion int64) (bool, error) {+ resultBytes, err := protojson.Marshal(result)+ if err != nil {+ return false, errors.Wrapf(err, "failed to marshal result %v", result)+ }+ q := qb.Q().Space(`+ UPDATE plan_check_run+ SET+ updated_at = ?,+ status = ?,+ result = ?+ WHERE id = ?+ AND project = ?+ AND status = ?+ AND COALESCE((result->>'approvalInputVersion')::bigint, 0) = ?`,+ time.Now(), status, resultBytes, uid, projectID, PlanCheckRunStatusRunning, approvalInputVersion)+ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ sqlResult, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, errors.Wrapf(err, "failed to update plan check run")+ }+ rowsAffected, err := sqlResult.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect plan check run update")+ }+ return rowsAffected > 0, nil+}++// RefreshPlanCheckRunIfStaleApprovalInputVersion refreshes a terminal stale-version plan check run to AVAILABLE.+//+// Approval recompute may observe an older terminal row while another request is already moving+// the plan forward. Only move rows to a newer materialized version; never rewind a row that has+// already advanced beyond the caller's observed plan version.+func (s *Store) RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx context.Context, projectID string, planUID int64, approvalInputVersion int64) (bool, error) {+ result := &storepb.PlanCheckRunResult{ApprovalInputVersion: approvalInputVersion}+ resultBytes, err := protojson.Marshal(result)+ if err != nil {+ return false, errors.Wrapf(err, "failed to marshal result")+ }++ q := qb.Q().Space(`+ UPDATE plan_check_run+ SET+ updated_at = ?,+ status = ?,+ result = ?+ WHERE project = ?+ AND plan_id = ?+ AND status NOT IN (?, ?)+ AND COALESCE((result->>'approvalInputVersion')::bigint, 0) < ?`,+ time.Now(), PlanCheckRunStatusAvailable, resultBytes, projectID, planUID, PlanCheckRunStatusAvailable, PlanCheckRunStatusRunning, approvalInputVersion)+ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ sqlResult, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, errors.Wrapf(err, "failed to refresh stale plan check run")+ }+ rowsAffected, err := sqlResult.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect stale plan check run refresh")+ }+ return rowsAffected > 0, nil+}+// BatchCancelPlanCheckRuns updates the status of planCheckRuns to CANCELED.func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string, planCheckRunUIDs []int64) error {q := qb.Q().Space(`@@ -225,11 +319,39 @@ func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string,return nil}+// CancelPlanCheckRunIfApprovalInputVersion cancels an active plan check run only when it still matches the observed approval input version.+func (s *Store) CancelPlanCheckRunIfApprovalInputVersion(ctx context.Context, projectID string, planCheckRunUID int64, approvalInputVersion int64) (bool, error) {+ q := qb.Q().Space(`+ UPDATE plan_check_run+ SET+ status = ?,+ updated_at = ?+ WHERE id = ?+ AND project = ?+ AND status IN (?, ?)+ AND COALESCE((result->>'approvalInputVersion')::bigint, 0) = ?`,+ PlanCheckRunStatusCanceled, time.Now(), planCheckRunUID, projectID, PlanCheckRunStatusAvailable, PlanCheckRunStatusRunning, approvalInputVersion)+ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }+ result, err := s.GetDB().ExecContext(ctx, query, args...)+ if err != nil {+ return false, err+ }+ rowsAffected, err := result.RowsAffected()+ if err != nil {+ return false, errors.Wrapf(err, "failed to inspect plan check run cancel")+ }+ return rowsAffected > 0, nil+}+// ClaimedPlanCheckRun represents a plan check run that was atomically claimed.type ClaimedPlanCheckRun struct {- UID int64- ProjectID string- PlanUID int64+ UID int64+ ProjectID string+ PlanUID int64+ ApprovalInputVersion int64}// FailStalePlanCheckRuns marks RUNNING plan check runs as FAILED if they have been running@@ -239,7 +361,14 @@ func (s *Store) FailStalePlanCheckRuns(ctx context.Context, timeout time.Duratioq := qb.Q().Space(`UPDATE plan_check_runSET status = ?,- result = '{"results": [{"status": "ERROR", "title": "Plan check run timed out", "content": "The plan check run was abandoned because it exceeded the maximum execution time."}]}',+ result = jsonb_build_object(+ 'approvalInputVersion', COALESCE((result->>'approvalInputVersion')::bigint, 0),+ 'results', jsonb_build_array(jsonb_build_object(+ 'status', 'ERROR',+ 'title', 'Plan check run timed out',+ 'content', 'The plan check run was abandoned because it exceeded the maximum execution time.'+ ))+ ),updated_at = now()WHERE status = ?AND updated_at < now() - ?::INTERVAL@@ -269,7 +398,7 @@ func (s *Store) ClaimAvailablePlanCheckRuns(ctx context.Context) ([]*ClaimedPlanWHERE status = ?FOR UPDATE SKIP LOCKED)- RETURNING id, project, plan_id+ RETURNING id, project, plan_id, COALESCE((result->>'approvalInputVersion')::bigint, 0)`, PlanCheckRunStatusRunning, PlanCheckRunStatusAvailable)query, args, err := q.ToSQL()@@ -286,7 +415,7 @@ func (s *Store) ClaimAvailablePlanCheckRuns(ctx context.Context) ([]*ClaimedPlanvar claimed []*ClaimedPlanCheckRunfor rows.Next() {var c ClaimedPlanCheckRun- if err := rows.Scan(&c.UID, &c.ProjectID, &c.PlanUID); err != nil {+ if err := rows.Scan(&c.UID, &c.ProjectID, &c.PlanUID, &c.ApprovalInputVersion); err != nil {return nil, err}claimed = append(claimed, &c)diff --git a/backend/store/plan_check_run_test.go b/backend/store/plan_check_run_test.gonew file mode 100644index 0000000000..81fe506bc8--- /dev/null+++ b/backend/store/plan_check_run_test.go@@ -0,0 +1,659 @@+package store_test++import (+ "context"+ "fmt"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/migrator"+ "github.com/bytebase/bytebase/backend/store"++ _ "github.com/bytebase/bytebase/backend/plugin/db/pg"+)++func TestClaimAvailablePlanCheckRunsDefaultsMissingApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)+ require.EqualValues(t, 0, claimed[0].ApprovalInputVersion)+}++func TestUpdatePlanCheckRunIfApprovalInputVersionSkipsStaleWorkerOnRefreshedRow(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)+ require.EqualValues(t, 1, claimed[0].ApprovalInputVersion)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ refreshedRun, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, refreshedRun)+ require.EqualValues(t, claimed[0].UID, refreshedRun.UID)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 1,+ Error: "stale result",+ }, claimed[0].UID, 1)+ require.NoError(t, err)+ require.False(t, updated)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+ require.Empty(t, run.Result.GetError())+}++func TestUpdatePlanCheckRunIfApprovalInputVersionAllowsClaimedRowAfterPlanVersionBump(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)+ require.EqualValues(t, 1, claimed[0].ApprovalInputVersion)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 1,+ Results: []*storepb.PlanCheckRunResult_Result{{+ Status: storepb.Advice_SUCCESS,+ Title: "stale result",+ }},+ }, claimed[0].UID, 1)+ require.NoError(t, err)+ require.True(t, updated)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusDone, run.Status)+ require.EqualValues(t, 1, run.Result.GetApprovalInputVersion())+ require.Len(t, run.Result.GetResults(), 1)+}++func TestCreatePlanCheckRunDoesNotResetActiveSameVersionRun(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)+ require.EqualValues(t, 2, claimed[0].ApprovalInputVersion)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.False(t, created)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusRunning, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestCreatePlanCheckRunDoesNotResetActiveSameVersionAvailableRun(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.False(t, created)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestCreatePlanCheckRunAllowsTerminalSameVersionRerun(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 2,+ Results: []*storepb.PlanCheckRunResult_Result{{+ Status: storepb.Advice_SUCCESS,+ Title: "current result",+ }},+ }, claimed[0].UID, 2)+ require.NoError(t, err)+ require.True(t, updated)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestCreatePlanCheckRunSkipsStaleIncomingApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.False(t, created)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestRefreshPlanCheckRunIfStaleApprovalInputVersionDoesNotResetRunningCheck(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)+ require.EqualValues(t, 2, claimed[0].ApprovalInputVersion)++ refreshed, err := s.RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx, "project-a", plan.UID, 2)+ require.NoError(t, err)+ require.False(t, refreshed)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusRunning, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestRefreshPlanCheckRunIfStaleApprovalInputVersionRefreshesTerminalStaleCheck(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 1,+ Results: []*storepb.PlanCheckRunResult_Result{{+ Status: storepb.Advice_SUCCESS,+ Title: "current result",+ }},+ }, claimed[0].UID, 1)+ require.NoError(t, err)+ require.True(t, updated)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ refreshed, err := s.RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx, "project-a", plan.UID, 2)+ require.NoError(t, err)+ require.True(t, refreshed)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+ require.Empty(t, run.Result.GetResults())+}++func TestRefreshPlanCheckRunIfStaleApprovalInputVersionSkipsSameVersionRow(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 1,+ Results: []*storepb.PlanCheckRunResult_Result{{+ Status: storepb.Advice_SUCCESS,+ Title: "stale result",+ }},+ }, claimed[0].UID, 1)+ require.NoError(t, err)+ require.True(t, updated)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ refreshed, err := s.RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx, "project-a", plan.UID, 1)+ require.NoError(t, err)+ require.False(t, refreshed)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusDone, run.Status)+ require.EqualValues(t, 1, run.Result.GetApprovalInputVersion())+}++func TestRefreshPlanCheckRunIfStaleApprovalInputVersionDoesNotRewindNewerRow(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 3},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 3},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ updated, err := s.UpdatePlanCheckRunIfApprovalInputVersion(ctx, "project-a", store.PlanCheckRunStatusDone, &storepb.PlanCheckRunResult{+ ApprovalInputVersion: 3,+ Results: []*storepb.PlanCheckRunResult_Result{{+ Status: storepb.Advice_SUCCESS,+ Title: "newer result",+ }},+ }, claimed[0].UID, 3)+ require.NoError(t, err)+ require.True(t, updated)++ refreshed, err := s.RefreshPlanCheckRunIfStaleApprovalInputVersion(ctx, "project-a", plan.UID, 2)+ require.NoError(t, err)+ require.False(t, refreshed)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusDone, run.Status)+ require.EqualValues(t, 3, run.Result.GetApprovalInputVersion())+ require.Len(t, run.Result.GetResults(), 1)+}++func TestCancelPlanCheckRunIfApprovalInputVersionSkipsRefreshedRow(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ created, err = s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)+ require.True(t, created)++ canceled, err := s.CancelPlanCheckRunIfApprovalInputVersion(ctx, "project-a", claimed[0].UID, 1)+ require.NoError(t, err)+ require.False(t, canceled)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusAvailable, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())++ canceled, err = s.CancelPlanCheckRunIfApprovalInputVersion(ctx, "project-a", claimed[0].UID, 2)+ require.NoError(t, err)+ require.True(t, canceled)++ run, err = s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.Equal(t, store.PlanCheckRunStatusCanceled, run.Status)+ require.EqualValues(t, 2, run.Result.GetApprovalInputVersion())+}++func TestCancelPlanCheckRunIfApprovalInputVersionAllowsObservedStaleRow(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 1},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 1},+ })+ require.NoError(t, err)+ require.True(t, created)++ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ _, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: &storepb.PlanConfig{ApprovalInputVersion: 2},+ })+ require.NoError(t, err)++ canceled, err := s.CancelPlanCheckRunIfApprovalInputVersion(ctx, "project-a", claimed[0].UID, 1)+ require.NoError(t, err)+ require.True(t, canceled)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.NotNil(t, run)+ require.Equal(t, store.PlanCheckRunStatusCanceled, run.Status)+ require.EqualValues(t, 1, run.Result.GetApprovalInputVersion())+}++func TestFailStalePlanCheckRunsPreservesApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ s := setupPlanCheckRunVersionStore(ctx, t)++ plan, err := s.CreatePlan(ctx, &store.PlanMessage{+ ProjectID: "project-a",+ Name: "plan-a",+ Description: "",+ Config: &storepb.PlanConfig{ApprovalInputVersion: 7},+ }, "[email protected]")+ require.NoError(t, err)++ created, err := s.CreatePlanCheckRun(ctx, &store.PlanCheckRunMessage{+ ProjectID: "project-a",+ PlanUID: plan.UID,+ Result: &storepb.PlanCheckRunResult{ApprovalInputVersion: 7},+ })+ require.NoError(t, err)+ require.True(t, created)+ claimed, err := s.ClaimAvailablePlanCheckRuns(ctx)+ require.NoError(t, err)+ require.Len(t, claimed, 1)++ rowsAffected, err := s.FailStalePlanCheckRuns(ctx, 0)+ require.NoError(t, err)+ require.EqualValues(t, 1, rowsAffected)++ run, err := s.GetPlanCheckRun(ctx, "project-a", plan.UID)+ require.NoError(t, err)+ require.Equal(t, store.PlanCheckRunStatusFailed, run.Status)+ require.EqualValues(t, 7, run.Result.GetApprovalInputVersion())+ require.NotEmpty(t, run.Result.GetResults())+}++func setupPlanCheckRunVersionStore(ctx context.Context, t *testing.T) *store.Store {+ t.Helper()++ container := testcontainer.GetTestPgContainer(ctx, t)+ t.Cleanup(func() { container.Close(ctx) })++ db := container.GetDB()+ require.NoError(t, migrator.MigrateSchema(ctx, db))++ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ `)+ require.NoError(t, err)++ pgURL := fmt.Sprintf(+ "host=%s port=%s user=postgres password=root-password database=postgres",+ container.GetHost(), container.GetPort(),+ )+ s, err := store.New(ctx, pgURL, false)+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, s.Close()) })+ return s+}diff --git a/backend/store/signal.go b/backend/store/signal.goindex a86819eca5..2644f0141a 100644--- a/backend/store/signal.go+++ b/backend/store/signal.go@@ -13,12 +13,16 @@ import (const SignalChannel = "bytebase_signal"// SendSignal sends a notification to the bytebase_signal channel.-func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, projectID string, uid int64) error {- payload, err := protojson.Marshal(&storepb.Signal{+func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, projectID string, uid int64, approvalInputVersion ...int64) error {+ signal := &storepb.Signal{Type: signalType,Uid: uid,Project: projectID,- })+ }+ if len(approvalInputVersion) > 0 {+ signal.ApprovalInputVersion = approvalInputVersion[0]+ }+ payload, err := protojson.Marshal(signal)if err != nil {return errors.Wrap(err, "failed to marshal signal payload")}diff --git a/backend/store/task.go b/backend/store/task.goindex 018f2434a5..3153a71961 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -503,6 +503,65 @@ func (s *Store) CreateTasks(ctx context.Context, projectID string, planUID int64}defer tx.Rollback()+ tasks, err = s.createTasksTxDedup(ctx, tx, projectID, planUID, tasks)+ if err != nil {+ return nil, err+ }+ if err := tx.Commit(); err != nil {+ return nil, errors.Wrapf(err, "failed to commit tx")+ }++ return tasks, nil+}++// CreateRolloutTasks marks the plan as having rollout and creates tasks in one transaction.+// If approvalInputVersion is set, the transaction creates no tasks unless the plan+// still has that approval input version.+func (s *Store) CreateRolloutTasks(ctx context.Context, projectID string, planUID int64, approvalInputVersion *int64, tasks []*TaskMessage) (bool, []*TaskMessage, error) {+ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return false, nil, errors.Wrapf(err, "failed to begin tx")+ }+ defer tx.Rollback()++ var updatedPlanUID int64+ if err := tx.QueryRowContext(ctx, `+ UPDATE plan+ SET+ updated_at = CASE+ WHEN COALESCE((config->>'hasRollout')::boolean, false) = false THEN $1+ ELSE updated_at+ END,+ config = CASE+ WHEN COALESCE((config->>'hasRollout')::boolean, false) = false THEN jsonb_set(config, '{hasRollout}', 'true'::jsonb, true)+ ELSE config+ END+ WHERE project = $2+ AND id = $3+ AND (+ $4::bigint IS NULL+ OR COALESCE((config->>'approvalInputVersion')::bigint, 0) = $4+ )+ RETURNING id`,+ time.Now(), projectID, planUID, approvalInputVersion).Scan(&updatedPlanUID); err != nil {+ if errors.Is(err, sql.ErrNoRows) {+ return false, nil, nil+ }+ return false, nil, errors.Wrapf(err, "failed to mark plan has rollout")+ }++ tasks, err = s.createTasksTxDedup(ctx, tx, projectID, planUID, tasks)+ if err != nil {+ return false, nil, err+ }+ if err := tx.Commit(); err != nil {+ return false, nil, errors.Wrapf(err, "failed to commit tx")+ }++ return true, tasks, nil+}++func (s *Store) createTasksTxDedup(ctx context.Context, tx *sql.Tx, projectID string, planUID int64, tasks []*TaskMessage) ([]*TaskMessage, error) {// Check existing tasks to avoid duplicatesexistingTasks, err := s.listTasksImpl(ctx, tx, &TaskFind{ProjectID: projectID,@@ -554,15 +613,8 @@ func (s *Store) CreateTasks(ctx context.Context, projectID string, planUID int64if err != nil {return nil, errors.Wrap(err, "failed to create tasks")}- if err := tx.Commit(); err != nil {- return nil, errors.Wrapf(err, "failed to commit tx")- }return tasks, nil}- if err := tx.Commit(); err != nil {- return nil, errors.Wrapf(err, "failed to commit tx")- }-return []*TaskMessage{}, nil}diff --git a/backend/tests/approval_test.go b/backend/tests/approval_test.goindex f9afce1ec6..9707d386c7 100644--- a/backend/tests/approval_test.go+++ b/backend/tests/approval_test.go@@ -11,9 +11,13 @@ import ("github.com/google/uuid""github.com/stretchr/testify/require""google.golang.org/genproto/googleapis/type/expr"+ "google.golang.org/protobuf/proto""google.golang.org/protobuf/types/known/fieldmaskpb"+ "github.com/bytebase/bytebase/backend/common"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store")// TestDirectApprovalRuleMatching tests the new direct approval rule API@@ -150,6 +154,344 @@ func TestDirectApprovalRuleMatching(t *testing.T) {a.Equal("Prod Change Database Approval", issue.GetApprovalTemplate().GetTitle(), "Correct approval template should be applied")}+func TestApprovalFindingRerunsPlanCheckForStaleApprovalInputVersion(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}++ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ t.Cleanup(func() { _ = ctl.Close(ctx) })++ instanceDir := t.TempDir()+ instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{+ InstanceId: generateRandomString("inst"),+ Instance: &v1pb.Instance{+ Title: "Approval Input Version Instance",+ Engine: v1pb.Engine_SQLITE,+ Environment: new("environments/prod"),+ Activation: true,+ DataSources: []*v1pb.DataSource{{+ Type: v1pb.DataSourceType_ADMIN,+ Host: instanceDir,+ Id: "admin",+ }},+ },+ }))+ a.NoError(err)++ dbName := generateRandomString("db")+ err = ctl.createDatabase(ctx, ctl.project, instanceResp.Msg, nil, dbName, "")+ a.NoError(err)+ databaseName := fmt.Sprintf("%s/databases/%s", instanceResp.Msg.Name, dbName)++ _, err = ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{+ AllowMissing: true,+ Setting: &v1pb.Setting{+ Name: "settings/WORKSPACE_APPROVAL",+ Value: &v1pb.SettingValue{+ Value: &v1pb.SettingValue_WorkspaceApproval{+ WorkspaceApproval: &v1pb.WorkspaceApprovalSetting{+ Rules: []*v1pb.WorkspaceApprovalSetting_Rule{+ {+ Source: v1pb.WorkspaceApprovalSetting_Rule_CHANGE_DATABASE,+ Condition: &expr.Expr{+ Expression: `statement.text.contains("stale_approval_b")`,+ },+ Template: &v1pb.ApprovalTemplate{+ Title: "SQLite change approval B",+ Flow: &v1pb.ApprovalFlow{+ Roles: []string{"roles/workspaceOwner"},+ },+ },+ },+ {+ Source: v1pb.WorkspaceApprovalSetting_Rule_CHANGE_DATABASE,+ Condition: &expr.Expr{+ Expression: `statement.text.contains("stale_approval_a")`,+ },+ Template: &v1pb.ApprovalTemplate{+ Title: "SQLite change approval A",+ Flow: &v1pb.ApprovalFlow{+ Roles: []string{"roles/workspaceOwner"},+ },+ },+ },+ },+ },+ },+ },+ },+ }))+ a.NoError(err)++ sheet1, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{+ Parent: ctl.project.Name,+ Sheet: &v1pb.Sheet{+ Content: []byte("CREATE TABLE stale_approval_a (id INTEGER PRIMARY KEY);"),+ },+ }))+ a.NoError(err)+ sheet2, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{+ Parent: ctl.project.Name,+ Sheet: &v1pb.Sheet{+ Content: []byte("CREATE TABLE stale_approval_b (id INTEGER PRIMARY KEY);"),+ },+ }))+ a.NoError(err)++ specID := uuid.NewString()+ planResp, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{+ Parent: ctl.project.Name,+ Plan: &v1pb.Plan{+ Title: "Approval input version plan",+ Specs: []*v1pb.Plan_Spec{{+ Id: specID,+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Targets: []string{databaseName},+ Sheet: sheet1.Msg.Name,+ },+ },+ }},+ },+ }))+ a.NoError(err)++ issueResp, err := ctl.issueServiceClient.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{+ Parent: ctl.project.Name,+ Issue: &v1pb.Issue{+ Title: "Approval input version issue",+ Type: v1pb.Issue_DATABASE_CHANGE,+ Description: "Testing approval input version rerun",+ Plan: planResp.Msg.Name,+ },+ }))+ a.NoError(err)++ waitForApprovalFindingDone(ctx, t, ctl, issueResp.Msg)++ gotIssueResp, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{Name: issueResp.Msg.Name}))+ a.NoError(err)+ a.NotEqual(v1pb.ApprovalStatus_CHECKING, gotIssueResp.Msg.ApprovalStatus)+ a.Equal("SQLite change approval A", gotIssueResp.Msg.GetApprovalTemplate().GetTitle())++ updatedPlanResp, err := ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{+ Plan: &v1pb.Plan{+ Name: planResp.Msg.Name,+ Specs: []*v1pb.Plan_Spec{{+ Id: specID,+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Targets: []string{databaseName},+ Sheet: sheet2.Msg.Name,+ },+ },+ }},+ },+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"specs"}},+ }))+ a.NoError(err)++ waitForApprovalFindingDone(ctx, t, ctl, issueResp.Msg)++ gotIssueResp, err = ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{Name: issueResp.Msg.Name}))+ a.NoError(err)+ a.NotEqual(v1pb.ApprovalStatus_CHECKING, gotIssueResp.Msg.ApprovalStatus)+ a.Equal("SQLite change approval B", gotIssueResp.Msg.GetApprovalTemplate().GetTitle())++ checkResp, err := ctl.planServiceClient.GetPlanCheckRun(ctx, connect.NewRequest(&v1pb.GetPlanCheckRunRequest{+ Name: fmt.Sprintf("%s/planCheckRun", updatedPlanResp.Msg.Name),+ }))+ a.NoError(err)+ a.Equal(v1pb.PlanCheckRun_DONE, checkResp.Msg.Status)+}++func TestApprovalActionRejectsStaleApprovalInputVersion(t *testing.T) {+ ctx := context.Background()+ ctl := &controller{}++ ctx, err := ctl.StartServerWithExternalPg(ctx)+ require.NoError(t, err)+ t.Cleanup(func() { _ = ctl.Close(ctx) })++ stores := getStore(t, ctl.server)++ t.Run("ApproveIssue", func(t *testing.T) {+ issue, staleApproval, approver := createStaleApprovalInputVersionIssue(ctx, t, ctl, stores, "stale-approve")++ withImpersonation(ctx, t, ctl, approver, func() {+ _, err := ctl.issueServiceClient.ApproveIssue(ctx, connect.NewRequest(&v1pb.ApproveIssueRequest{+ Name: issue.Name,+ Comment: "approve stale approval",+ }))+ require.Error(t, err)+ require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))+ })++ got, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{Name: issue.Name}))+ require.NoError(t, err)+ require.Equal(t, v1pb.ApprovalStatus_PENDING, got.Msg.ApprovalStatus)+ requireApprovalPayloadApproverCount(ctx, t, stores, issue.Name, len(staleApproval.Approvers))+ })++ t.Run("RejectIssue", func(t *testing.T) {+ issue, staleApproval, approver := createStaleApprovalInputVersionIssue(ctx, t, ctl, stores, "stale-reject")++ withImpersonation(ctx, t, ctl, approver, func() {+ _, err := ctl.issueServiceClient.RejectIssue(ctx, connect.NewRequest(&v1pb.RejectIssueRequest{+ Name: issue.Name,+ Comment: "reject stale approval",+ }))+ require.Error(t, err)+ require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))+ })++ got, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{Name: issue.Name}))+ require.NoError(t, err)+ require.Equal(t, v1pb.ApprovalStatus_PENDING, got.Msg.ApprovalStatus)+ requireApprovalPayloadApproverCount(ctx, t, stores, issue.Name, len(staleApproval.Approvers))+ })++ t.Run("RequestIssue", func(t *testing.T) {+ issue, staleApproval, approver := createStaleApprovalInputVersionIssue(ctx, t, ctl, stores, "stale-request")+ staleApproval.Approvers = append(staleApproval.Approvers, &storepb.IssuePayloadApproval_Approver{+ Status: storepb.IssuePayloadApproval_Approver_REJECTED,+ Principal: common.FormatUserEmail(approver.Email),+ })+ writeIssueApprovalPayload(ctx, t, stores, issue.Name, staleApproval)++ _, err := ctl.issueServiceClient.RequestIssue(ctx, connect.NewRequest(&v1pb.RequestIssueRequest{+ Name: issue.Name,+ Comment: "request stale approval",+ }))+ require.Error(t, err)+ require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))++ got, err := ctl.issueServiceClient.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{Name: issue.Name}))+ require.NoError(t, err)+ require.Equal(t, v1pb.ApprovalStatus_REJECTED, got.Msg.ApprovalStatus)+ requireApprovalPayloadApproverCount(ctx, t, stores, issue.Name, len(staleApproval.Approvers))+ })+}++func createStaleApprovalInputVersionIssue(ctx context.Context, t *testing.T, ctl *controller, stores *store.Store, suffix string) (*v1pb.Issue, *storepb.IssuePayloadApproval, testApprover) {+ t.Helper()+ project := ctl.createTestProject(ctx, t, suffix)++ instanceDir := t.TempDir()+ instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{+ InstanceId: generateRandomString("inst"),+ Instance: &v1pb.Instance{+ Title: "Stale Approval Instance",+ Engine: v1pb.Engine_SQLITE,+ Environment: new("environments/prod"),+ Activation: true,+ DataSources: []*v1pb.DataSource{{+ Type: v1pb.DataSourceType_ADMIN,+ Host: instanceDir,+ Id: "admin",+ }},+ },+ }))+ require.NoError(t, err)++ dbName := generateRandomString("db")+ require.NoError(t, ctl.createDatabase(ctx, project, instanceResp.Msg, nil, dbName, ""))+ target := dbTargetName(instanceResp.Msg, dbName)++ disableSelfApproval(ctx, t, ctl, project)+ installWorkspaceApprovalRule(ctx, t, ctl, project, []string{"roles/projectOwner"})+ approver := provisionApprover(ctx, t, ctl, project, suffix, "roles/projectOwner")++ sheet1 := createSheetWithContent(ctx, t, ctl, project, []byte(fmt.Sprintf("CREATE TABLE %s_a (id INTEGER PRIMARY KEY);", strings.ReplaceAll(suffix, "-", "_"))))++ specID := uuid.NewString()+ planResp, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{+ Parent: project.Name,+ Plan: &v1pb.Plan{+ Title: "Stale approval input version plan " + suffix,+ Specs: []*v1pb.Plan_Spec{{+ Id: specID,+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Targets: []string{target},+ Sheet: sheet1,+ },+ },+ }},+ },+ }))+ require.NoError(t, err)++ issue := createIssueForPlan(ctx, t, ctl, project, planResp.Msg, "stale approval input version "+suffix)+ waitForIssuePending(ctx, t, ctl, issue)++ _, issueUID, err := common.GetProjectIDIssueUID(issue.Name)+ require.NoError(t, err)+ projectID, err := common.GetProjectID(project.Name)+ require.NoError(t, err)+ storeIssue, err := stores.GetIssue(ctx, &store.FindIssueMessage{+ ProjectIDs: []string{projectID},+ UID: &issueUID,+ })+ require.NoError(t, err)+ require.NotNil(t, storeIssue)+ staleApproval := proto.CloneOf(storeIssue.Payload.Approval)++ _, planUID, err := common.GetProjectIDPlanID(planResp.Msg.Name)+ require.NoError(t, err)+ plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: projectID, UID: &planUID})+ require.NoError(t, err)+ require.NotNil(t, plan)+ _, err = stores.UpdatePlan(ctx, &store.UpdatePlanMessage{+ UID: plan.UID,+ ProjectID: plan.ProjectID,+ Config: proto.CloneOf(plan.Config),+ BumpApprovalInputVersion: true,+ })+ require.NoError(t, err)++ writeIssueApprovalPayload(ctx, t, stores, issue.Name, staleApproval)+ return issue, staleApproval, approver+}++func createSheetWithContent(ctx context.Context, t *testing.T, ctl *controller, project *v1pb.Project, content []byte) string {+ t.Helper()+ resp, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{+ Parent: project.Name,+ Sheet: &v1pb.Sheet{Content: content},+ }))+ require.NoError(t, err)+ return resp.Msg.Name+}++func writeIssueApprovalPayload(ctx context.Context, t *testing.T, stores *store.Store, issueName string, approval *storepb.IssuePayloadApproval) {+ t.Helper()+ projectID, issueUID, err := common.GetProjectIDIssueUID(issueName)+ require.NoError(t, err)+ _, err = stores.UpdateIssue(ctx, projectID, issueUID, &store.UpdateIssueMessage{+ PayloadUpsert: &storepb.Issue{+ Approval: approval,+ },+ })+ require.NoError(t, err)+}++func requireApprovalPayloadApproverCount(ctx context.Context, t *testing.T, stores *store.Store, issueName string, count int) {+ t.Helper()+ projectID, issueUID, err := common.GetProjectIDIssueUID(issueName)+ require.NoError(t, err)+ issue, err := stores.GetIssue(ctx, &store.FindIssueMessage{+ ProjectIDs: []string{projectID},+ UID: &issueUID,+ })+ require.NoError(t, err)+ require.NotNil(t, issue)+ require.Len(t, issue.Payload.GetApproval().GetApprovers(), count)+}+// TestApprovalRuleFirstMatchWins tests that the first matching approval rule is applied// when multiple rules could potentially match.func TestApprovalRuleFirstMatchWins(t *testing.T) {diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex d298f269d7..dfe99ba594 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -77,6 +77,20 @@ func CheckIssueApproved(issue *store.IssueMessage) (bool, error) {return CheckApprovalApproved(issue.Payload.Approval)}+// CheckIssueApprovedForPlan checks if the issue is approved for the current plan approval input version.+func CheckIssueApprovedForPlan(issue *store.IssueMessage, plan *store.PlanMessage) (bool, error) {+ if issue.Type == storepb.Issue_DATABASE_CHANGE && plan != nil {+ var approvalInputVersion int64+ if plan.Config != nil {+ approvalInputVersion = plan.Config.GetApprovalInputVersion()+ }+ if issue.Payload.GetApproval().GetApprovalInputVersion() != approvalInputVersion {+ return false, nil+ }+ }+ return CheckIssueApproved(issue)+}+// UpdateProjectPolicyFromRoleGrantIssue updates the project policy from a role grant issue.func UpdateProjectPolicyFromRoleGrantIssue(ctx context.Context, stores *store.Store, workspaceID string, issue *store.IssueMessage, roleGrant *storepb.RoleGrant) error {policyMessage, err := stores.GetProjectIamPolicy(ctx, workspaceID, issue.ProjectID)diff --git a/backend/utils/utils_test.go b/backend/utils/utils_test.goindex 05b393abd5..08345daf66 100644--- a/backend/utils/utils_test.go+++ b/backend/utils/utils_test.go@@ -55,3 +55,72 @@ func TestCheckDatabaseGroupMatch(t *testing.T) {assert.Equal(t, test.match, match)}}++func TestCheckIssueApprovedForPlan(t *testing.T) {+ tests := []struct {+ name string+ issueType storepb.Issue_Type+ issue *storepb.Issue+ plan *storepb.PlanConfig+ want bool+ }{+ {+ name: "database change approved for matching plan version",+ issueType: storepb.Issue_DATABASE_CHANGE,+ issue: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 2,+ },+ },+ plan: &storepb.PlanConfig{ApprovalInputVersion: 2},+ want: true,+ },+ {+ name: "database change stale approval is not approved",+ issueType: storepb.Issue_DATABASE_CHANGE,+ issue: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ },+ plan: &storepb.PlanConfig{ApprovalInputVersion: 2},+ },+ {+ name: "database change matching version still requires completed approval",+ issueType: storepb.Issue_DATABASE_CHANGE,+ issue: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalInputVersion: 2,+ },+ },+ plan: &storepb.PlanConfig{ApprovalInputVersion: 2},+ },+ {+ name: "non database change keeps existing approval behavior",+ issueType: storepb.Issue_ROLE_GRANT,+ issue: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalInputVersion: 1,+ },+ },+ plan: &storepb.PlanConfig{ApprovalInputVersion: 2},+ want: true,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := CheckIssueApprovedForPlan(&store.IssueMessage{+ Type: tt.issueType,+ Payload: tt.issue,+ }, &store.PlanMessage{+ Config: tt.plan,+ })+ assert.NoError(t, err)+ assert.Equal(t, tt.want, got)+ })+ }+}diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 5ebc5fe130..8c5bf2142b 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -4884,6 +4884,7 @@ Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coord| type | [Signal.Type](#bytebase-store-Signal-Type) | | || uid | [int64](#int64) | | || project | [string](#string) | | |+| approval_input_version | [int64](#int64) | | |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex f1d1e12b3b..7c14b4b0cb 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -13374,6 +13374,13 @@ Default to this mode.</p></td><td><p> </p></td></tr>+ <tr>+ <td>approval_input_version</td>+ <td><a href="#int64">int64</a></td>+ <td></td>+ <td><p> </p></td>+ </tr>+</tbody></table>diff --git a/proto/store/store/signal.proto b/proto/store/store/signal.protoindex b91fb671b1..a9a20996d8 100644--- a/proto/store/store/signal.proto+++ b/proto/store/store/signal.proto@@ -16,4 +16,5 @@ message Signal {Type type = 1;int64 uid = 2;string project = 3;+ int64 approval_input_version = 4;}