Skip to content

Commit b6636ba

Browse files
committed
Support for executing plan from a specified step
Signed-off-by: Sergen Yalçın <[email protected]>
1 parent 0b08007 commit b6636ba

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pkg/migration/plan_executor.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ const (
2626
// PlanExecutor drives the execution of a plan's steps and
2727
// uses the configured `executors` to execute those steps.
2828
type PlanExecutor struct {
29-
executors []Executor
30-
plan Plan
31-
callback ExecutorCallback
29+
executors []Executor
30+
plan Plan
31+
callback ExecutorCallback
32+
LastSuccessfulStep int
33+
StartIndex int
3234
}
3335

3436
// Action represents an action to be taken by the PlanExecutor.
@@ -69,6 +71,12 @@ func WithExecutorCallback(cb ExecutorCallback) PlanExecutorOption {
6971
}
7072
}
7173

74+
func WithStartIndex(startIndex int) PlanExecutorOption {
75+
return func(pe *PlanExecutor) {
76+
pe.StartIndex = startIndex
77+
}
78+
}
79+
7280
// ExecutorCallback is the interface for the callback implementations
7381
// to be notified while executing each Step of a migration Plan.
7482
type ExecutorCallback interface {
@@ -103,14 +111,15 @@ func NewPlanExecutor(plan Plan, executors []Executor, opts ...PlanExecutorOption
103111

104112
func (pe *PlanExecutor) Execute() error { //nolint:gocyclo // easier to follow this way
105113
ctx := make(map[string]any)
106-
for i := 0; i < len(pe.plan.Spec.Steps); i++ {
114+
for i := pe.StartIndex; i < len(pe.plan.Spec.Steps); i++ {
107115
var r CallbackResult
108116
if pe.callback != nil {
109117
r = pe.callback.StepToExecute(pe.plan.Spec.Steps[i], i)
110118
switch r.Action {
111119
case ActionCancel:
112120
return nil
113121
case ActionSkip:
122+
pe.LastSuccessfulStep = i
114123
continue
115124
case ActionContinue, ActionRepeat:
116125
}
@@ -124,6 +133,7 @@ func (pe *PlanExecutor) Execute() error { //nolint:gocyclo // easier to follow t
124133
}
125134
} else if pe.callback != nil {
126135
r = pe.callback.StepSucceeded(pe.plan.Spec.Steps[i], i, diag)
136+
pe.LastSuccessfulStep = i
127137
}
128138

129139
switch r.Action {

pkg/migration/plan_generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestGeneratePlan(t *testing.T) {
293293
if err != nil {
294294
t.Fatalf("Failed to load plan file from path %s: %v", tt.want.migrationPlanPath, err)
295295
}
296-
if diff := cmp.Diff(p, &pg.Plan, cmpopts.IgnoreUnexported(Spec{})); diff != "" {
296+
if diff := cmp.Diff(p, &pg.Plan, cmpopts.IgnoreUnexported(Plan{}, Spec{})); diff != "" {
297297
t.Errorf("GeneratePlan(): -wantPlan, +gotPlan: %s", diff)
298298
}
299299
// compare generated migration files with the expected ones

0 commit comments

Comments
 (0)