Skip to content

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

refactor: make UpdatePlan() return the updated plan (#18641)

Danny Xu116ab938f066
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index 78d0e4655e..60c697e03b 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -397,19 +397,9 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[v
}
}
- if err := s.store.UpdatePlan(ctx, planUpdate); err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update plan %q: %v", req.Plan.Name, err))
- }
-
- updatedPlan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{
- UID: &oldPlan.UID,
- ProjectID: &oldPlan.ProjectID,
- })
+ updatedPlan, err := s.store.UpdatePlan(ctx, planUpdate)
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get updated plan %q: %v", req.Plan.Name, err))
- }
- if updatedPlan == nil {
- return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("updated plan %q not found", req.Plan.Name))
+ return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update plan %q: %v", req.Plan.Name, err))
}
if planCheckRunsTrigger {
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index f04bb64305..6124f9f606 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -307,25 +307,12 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request
// Update plan to set hasRollout to true
config := proto.CloneOf(plan.Config)
config.HasRollout = true
- if err := s.store.UpdatePlan(ctx, &store.UpdatePlanMessage{
+ rollout, err := s.store.UpdatePlan(ctx, &store.UpdatePlanMessage{
UID: planID,
Config: config,
- }); err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update plan hasRollout, error: %v", err))
- }
-
- // getRolloutWithTasks inlined
- // re-fetch plan to get latest state (e.g. hasRollout config)
- rollout, err := s.store.GetPlan(ctx, &store.FindPlanMessage{
- UID: &planID, // planID is already int64
- ProjectID: &projectID,
})
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get plan, error: %v", err))
- }
- if rollout == nil {
- // Should not happen as we just created tasks for it
- return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("rollout %d not found in project %s", planID, projectID))
+ return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update plan hasRollout, error: %v", err))
}
tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID})
diff --git a/backend/store/plan.go b/backend/store/plan.go
index f9f87d8c72..99617ecaab 100644
--- a/backend/store/plan.go
+++ b/backend/store/plan.go
@@ -221,8 +221,8 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMe
return plans, nil
}
-// UpdatePlan updates an existing plan.
-func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) error {
+// UpdatePlan updates an existing plan and returns the updated plan.
+func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) (*PlanMessage, error) {
set := []string{"updated_at = ?"}
args := []any{time.Now()}
@@ -241,35 +241,52 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) error
if v := patch.Config; v != nil {
config, err := protojson.Marshal(v)
if err != nil {
- return errors.Wrapf(err, "failed to marshal plan config")
+ return nil, errors.Wrapf(err, "failed to marshal plan config")
}
set = append(set, "config = ?")
args = append(args, config)
}
args = append(args, patch.UID)
- q := qb.Q().Space(fmt.Sprintf("UPDATE plan SET %s WHERE id = ?", strings.Join(set, ", ")), args...)
+ q := qb.Q().Space(fmt.Sprintf("UPDATE plan SET %s WHERE id = ? RETURNING id, creator, created_at, updated_at, project, name, description, config, deleted", strings.Join(set, ", ")), args...)
query, finalArgs, err := q.ToSQL()
if err != nil {
- return errors.Wrapf(err, "failed to build sql")
+ return nil, errors.Wrapf(err, "failed to build sql")
}
tx, err := s.GetDB().BeginTx(ctx, nil)
if err != nil {
- return errors.Wrapf(err, "failed to begin transaction")
+ return nil, errors.Wrapf(err, "failed to begin transaction")
}
defer tx.Rollback()
- if _, err := tx.ExecContext(ctx, query, finalArgs...); err != nil {
- return errors.Wrapf(err, "failed to update plan")
+ plan := PlanMessage{
+ Config: &storepb.PlanConfig{},
+ }
+ var config []byte
+ if err := tx.QueryRowContext(ctx, query, finalArgs...).Scan(
+ &plan.UID,
+ &plan.Creator,
+ &plan.CreatedAt,
+ &plan.UpdatedAt,
+ &plan.ProjectID,
+ &plan.Name,
+ &plan.Description,
+ &config,
+ &plan.Deleted,
+ ); err != nil {
+ return nil, errors.Wrapf(err, "failed to update plan")
+ }
+ if err := common.ProtojsonUnmarshaler.Unmarshal(config, plan.Config); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal plan config")
}
if err := tx.Commit(); err != nil {
- return errors.Wrapf(err, "failed to commit transaction")
+ return nil, errors.Wrapf(err, "failed to commit transaction")
}
- return nil
+ return &plan, nil
}
// GetListPlanFilter parses a CEL filter expression into a query builder query for listing plans.