Skip to content

Commit 062db2a

Browse files
committed
chore(history): populate process instance treepath
related to #431
1 parent fd46537 commit 062db2a

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
@@ -25,6 +25,7 @@
2525
import io.camunda.migration.data.impl.logging.HistoryMigratorLogs;
2626
import io.camunda.search.entities.ProcessInstanceEntity;
2727
import java.util.Date;
28+
import java.util.Objects;
2829
import org.camunda.bpm.engine.history.HistoricActivityInstance;
2930
import org.camunda.bpm.engine.history.HistoricProcessInstance;
3031
import org.springframework.stereotype.Service;
@@ -113,7 +114,8 @@ public Long migrateTransactionally(HistoricProcessInstance c7ProcessInstance) {
113114

114115
builder
115116
.historyCleanupDate(c8HistoryCleanupDate)
116-
.endDate(c8EndTime);
117+
.endDate(c8EndTime)
118+
.treePath(generateTreepath(builder.build().rootProcessInstanceKey(), processInstanceKey));
117119

118120
ProcessInstanceDbModel dbModel = convert(C7Entity.of(c7ProcessInstance), builder);
119121

@@ -166,5 +168,20 @@ protected void resolveParentFlowNodeInstanceKey(
166168
}
167169
}
168170

171+
/**
172+
* Generates a tree path for process instances in the format:
173+
* PI_rootProcessInstanceKey/processInstanceKey or
174+
* PI_processInstanceKey if this instance is the root of the hierarchy
175+
*
176+
* @param rootProcessInstanceKey the root process instance key
177+
* @param processInstanceKey the process instance key
178+
* @return the tree path string
179+
*/
180+
public static String generateTreepath(Long rootProcessInstanceKey, Long processInstanceKey) {
181+
return Objects.equals(rootProcessInstanceKey, processInstanceKey) ?
182+
"PI_" + processInstanceKey :
183+
"PI_" + rootProcessInstanceKey + "/PI_" + processInstanceKey;
184+
}
185+
169186
}
170187

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
@@ -175,6 +175,33 @@ public void shouldCheckCalledProcessParentElementKey() {
175175
assertThat(searchHistoricProcessInstances("calledProcessInstanceId")).isEmpty();
176176
}
177177

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

233+
206234
// Verify that flow nodes also have rootProcessInstanceKey
207235
List<FlowNodeInstanceEntity> subFlowNodes = searchHistoricFlowNodes(sub.processInstanceKey());
208236
assertThat(subFlowNodes).isNotEmpty();
@@ -321,7 +349,13 @@ protected void verifyProcessInstanceFields(ProcessInstanceEntity processInstance
321349
}
322350

323351
assertThat(processInstance.hasIncident()).isEqualTo(hasIncidents);
324-
assertThat(processInstance.treePath()).isNull();
352+
353+
assertThat(processInstance.treePath()).isNotNull();
354+
if (hasParent) {
355+
assertThat(processInstance.treePath()).matches("PI_\\d+/PI_" + processInstance.processInstanceKey());
356+
} else {
357+
assertThat(processInstance.treePath()).isEqualTo("PI_" + processInstance.processInstanceKey());
358+
}
325359
}
326360

327361
@Test

0 commit comments

Comments
 (0)