Skip to content

Commit 796acd7

Browse files
authored
fix: rollback changes with targetSystem context (#716)
1 parent 81b11a2 commit 796acd7

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

core/flamingock-core/src/main/java/io/flamingock/internal/core/targets/AbstractTargetSystem.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ public final <T> T applyChange(Function<ExecutionRuntime, T> changeApplier, Exec
9999
return changeApplier.apply(executionRuntime);
100100
}
101101

102+
/**
103+
* Rolls back (reverts) a previously applied change with session-scoped dependency injection.
104+
* <p>
105+
* This method is the entry point for non-transactional rollback execution.
106+
* It calls {@link #enhanceExecutionRuntime(ExecutionRuntime, boolean)} to allow
107+
* subclasses to inject session-scoped dependencies before executing the rollback.
108+
*
109+
* @param <T> the return type of the rollback operation
110+
* @param changeRollbacker the function that executes the actual rollback
111+
* @param executionRuntime the runtime context for dependency resolution
112+
* @return the result of the rollback operation
113+
*/
114+
public final <T> T rollbackChange(Function<ExecutionRuntime, T> changeRollbacker, ExecutionRuntime executionRuntime) {
115+
enhanceExecutionRuntime(executionRuntime, false);
116+
return changeRollbacker.apply(executionRuntime);
117+
}
118+
119+
102120
/**
103121
* Hook for injecting session-scoped dependencies into the execution runtime.
104122
* <p>

core/flamingock-core/src/main/java/io/flamingock/internal/core/targets/operations/TargetSystemOps.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,23 @@ public interface TargetSystemOps extends TargetSystem {
5656
*/
5757
<T> T applyChange(Function<ExecutionRuntime, T> changeApplier, ExecutionRuntime executionRuntime);
5858

59+
/**
60+
* Rolls back (reverts) a previously applied change in this target system.
61+
* <p>
62+
* This method coordinates the execution of a rollback by:
63+
* <ul>
64+
* <li>Injecting session-scoped dependencies via the target system</li>
65+
* <li>Executing the rollback function with the enhanced runtime</li>
66+
* </ul>
67+
* <p>
68+
* Implementations should strive for idempotency: invoking the rollback more than
69+
* once should not produce side effects beyond the first successful revert.
70+
*
71+
* @param <T> the return type of the rollback operation
72+
* @param changeRollbacker the function that executes the actual rollback
73+
* @param executionRuntime the runtime context for dependency resolution
74+
* @return the result of the rollback operation
75+
*/
76+
<T> T rollbackChange(Function<ExecutionRuntime, T> changeRollbacker, ExecutionRuntime executionRuntime);
77+
5978
}

core/flamingock-core/src/main/java/io/flamingock/internal/core/targets/operations/TargetSystemOpsImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public final <T> T applyChange(Function<ExecutionRuntime, T> changeApplier, Exec
4141
return targetSystem.applyChange(changeApplier, executionRuntime);
4242
}
4343

44+
@Override
45+
public final <T> T rollbackChange(Function<ExecutionRuntime, T> changeRollbacker, ExecutionRuntime executionRuntime) {
46+
executionRuntime.addContextLayer(targetSystem.getContext());
47+
return targetSystem.rollbackChange(changeRollbacker, executionRuntime);
48+
}
49+
4450
@Override
4551
public String getId() {
4652
return targetSystem.getId();

core/flamingock-core/src/main/java/io/flamingock/internal/core/task/navigation/navigator/strategy/NonTxChangeProcessStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected ChangeProcessResult doApplyChange() {
111111

112112
private void rollbackActualChangeAndChain(FailedAfterExecutionAuditStep rollableFailedStep, ExecutionContext executionContext) {
113113
rollableFailedStep.getRollbackSteps().forEach(rollableStep -> {
114-
ManualRolledBackStep rolledBack = rollableStep.rollback(buildExecutionRuntime());
114+
ManualRolledBackStep rolledBack = targetSystemOps.rollbackChange(rollableStep::rollback, buildExecutionRuntime());
115115
stepLogger.logManualRollbackResult(rolledBack);
116116
summarizer.add(rolledBack);
117117
auditAndLogManualRollback(rolledBack, executionContext);

core/flamingock-core/src/main/java/io/flamingock/internal/core/task/navigation/navigator/strategy/SharedTxChangeProcessStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private void rollbackChain(RollableFailedStep rollableFailedStep, ExecutionConte
142142
rollableFailedStep.getRollbackSteps()
143143
.stream().skip(1)
144144
.forEach(rollableStep -> {
145-
ManualRolledBackStep rolledBack = rollableStep.rollback(buildExecutionRuntime());
145+
ManualRolledBackStep rolledBack = targetSystemOps.rollbackChange(rollableStep::rollback, buildExecutionRuntime());
146146
stepLogger.logManualRollbackResult(rolledBack);
147147
summarizer.add(rolledBack);
148148
auditAndLogManualRollback(rolledBack, executionContext);

core/flamingock-core/src/main/java/io/flamingock/internal/core/task/navigation/navigator/strategy/SimpleTxChangeProcessStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void rollbackChain(RollableFailedStep rollableFailedStep, ExecutionConte
138138
rollableFailedStep.getRollbackSteps()
139139
.stream().skip(1)
140140
.forEach(rollableStep -> {
141-
ManualRolledBackStep rolledBack = rollableStep.rollback(buildExecutionRuntime());
141+
ManualRolledBackStep rolledBack = targetSystemOps.rollbackChange(rollableStep::rollback, buildExecutionRuntime());
142142
stepLogger.logManualRollbackResult(rolledBack);
143143
summarizer.add(rolledBack);
144144
auditAndLogManualRollback(rolledBack, executionContext);

0 commit comments

Comments
 (0)