Skip to content

Commit f9109f8

Browse files
committed
chore(history): populate process instance treepath
related to #431
1 parent 84ff0b6 commit f9109f8

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

data-migrator/core/src/main/java/io/camunda/migration/data/impl/history/migrator/ProcessInstanceMigrator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.BiConsumer;
3030
import java.util.function.Consumer;
3131
import java.util.function.Function;
32+
import java.util.Objects;
3233
import org.camunda.bpm.engine.history.HistoricActivityInstance;
3334
import org.camunda.bpm.engine.history.HistoricProcessInstance;
3435
import org.springframework.stereotype.Service;
@@ -123,7 +124,8 @@ public Long migrateTransactionally(HistoricProcessInstance c7ProcessInstance) {
123124

124125
builder
125126
.historyCleanupDate(c8HistoryCleanupDate)
126-
.endDate(c8EndTime);
127+
.endDate(c8EndTime)
128+
.treePath(generateTreepath(builder.build().rootProcessInstanceKey(), processInstanceKey));
127129

128130
ProcessInstanceDbModel dbModel = convert(C7Entity.of(c7ProcessInstance), builder);
129131

@@ -176,5 +178,20 @@ protected void resolveParentFlowNodeInstanceKey(
176178
}
177179
}
178180

181+
/**
182+
* Generates a tree path for process instances in the format:
183+
* PI_rootProcessInstanceKey/processInstanceKey or
184+
* PI_processInstanceKey if this instance is the root of the hierarchy
185+
*
186+
* @param rootProcessInstanceKey the root process instance key
187+
* @param processInstanceKey the process instance key
188+
* @return the tree path string
189+
*/
190+
public static String generateTreepath(Long rootProcessInstanceKey, Long processInstanceKey) {
191+
return Objects.equals(rootProcessInstanceKey, processInstanceKey) ?
192+
"PI_" + processInstanceKey :
193+
"PI_" + rootProcessInstanceKey + "/PI_" + processInstanceKey;
194+
}
195+
179196
}
180197

data-migrator/core/src/main/java/io/camunda/migration/data/impl/interceptor/history/entity/ProcessInstanceTransformer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public void execute(HistoricProcessInstance entity, ProcessInstanceDbModelBuilde
4444
.tenantId(getTenantId(entity.getTenantId()))
4545
.version(entity.getProcessDefinitionVersion())
4646
.tags(getDefaultTags(entity))
47-
.treePath(null)
4847
.numIncidents(0)
4948
.partitionId(C7_HISTORY_PARTITION_ID);
5049
}

data-migrator/qa/integration-tests/src/test/java/io/camunda/migration/data/qa/history/entity/HistoryProcessInstanceTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,33 @@ public void shouldCheckCalledProcessParentElementKey() {
172172
assertThat(searchHistoricProcessInstances("calledProcessInstanceId")).isEmpty();
173173
}
174174

175+
@Test
176+
public void shouldPopulateTreePathForCallActivity() {
177+
// given
178+
deployer.deployCamunda7Process("callActivityProcess.bpmn");
179+
deployer.deployCamunda7Process("calledActivitySubprocess.bpmn");
180+
runtimeService.startProcessInstanceByKey("callingProcessId");
181+
182+
// when
183+
historyMigrator.migrate();
184+
historyMigrator.setMode(MigratorMode.RETRY_SKIPPED);
185+
historyMigrator.migrate();
186+
187+
// then
188+
List<ProcessInstanceEntity> parentProcessInstances = searchHistoricProcessInstances("callingProcessId");
189+
List<ProcessInstanceEntity> subProcessInstances = searchHistoricProcessInstances("calledProcessInstanceId");
190+
assertThat(parentProcessInstances).hasSize(1);
191+
assertThat(subProcessInstances).hasSize(1);
192+
193+
ProcessInstanceEntity parent = parentProcessInstances.getFirst();
194+
ProcessInstanceEntity sub = subProcessInstances.getFirst();
195+
196+
assertThat(parent.treePath()).isNotNull().isEqualTo("PI_" + parent.processInstanceKey());
197+
198+
assertThat(sub.treePath()).isNotNull()
199+
.isEqualTo("PI_" + parent.processInstanceKey() + "/PI_" + sub.processInstanceKey());
200+
}
201+
175202
@Test
176203
public void shouldPopulateRootProcessInstanceKeyForCallActivity() {
177204
// given
@@ -197,6 +224,7 @@ public void shouldPopulateRootProcessInstanceKeyForCallActivity() {
197224
// Sub process should have rootProcessInstanceKey pointing to the parent
198225
assertThat(sub.rootProcessInstanceKey()).isEqualTo(parent.processInstanceKey());
199226

227+
200228
// Verify that flow nodes also have rootProcessInstanceKey
201229
List<FlowNodeInstanceEntity> subFlowNodes = searchHistoricFlowNodes(sub.processInstanceKey());
202230
assertThat(subFlowNodes).isNotEmpty();
@@ -310,7 +338,13 @@ protected void verifyProcessInstanceFields(ProcessInstanceEntity processInstance
310338
}
311339

312340
assertThat(processInstance.hasIncident()).isEqualTo(hasIncidents);
313-
assertThat(processInstance.treePath()).isNull();
341+
342+
assertThat(processInstance.treePath()).isNotNull();
343+
if (hasParent) {
344+
assertThat(processInstance.treePath()).matches("PI_\\d+/PI_" + processInstance.processInstanceKey());
345+
} else {
346+
assertThat(processInstance.treePath()).isEqualTo("PI_" + processInstance.processInstanceKey());
347+
}
314348
}
315349

316350
@Test

0 commit comments

Comments
 (0)