Skip to content

Commit

Permalink
added a migration method for deprecated log entries (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Dec 25, 2024
1 parent 7385df7 commit 1a2a84c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,10 @@ private ItemCollection createVersion(ItemCollection sourceItemCollection) throws
private ItemCollection logEvent(int taskID, int eventID, int targetTaskID, final ItemCollection workitem) {
ItemCollection documentResult = workitem;

List<String> existingLogEntries = (List<String>) workitem.getItemValue(EVENTLOG);

// Migration check for deprecated log entries.
migrateOldEventLogFormat(workitem);
migrateDeprecatedEventLogFormat(workitem);

List<String> existingLogEntries = (List<String>) workitem.getItemValue(EVENTLOG);

// Create new log entry...
StringBuilder logEntry = new StringBuilder();
Expand Down Expand Up @@ -1092,7 +1092,7 @@ private ItemCollection logEvent(int taskID, int eventID, int targetTaskID, final
* @param workitem - the workitem to be migrated
*/
@SuppressWarnings("unchecked")
private void migrateOldEventLogFormat(ItemCollection workitem) {
private void migrateDeprecatedEventLogFormat(ItemCollection workitem) {
if (workitem.hasItem("$eventlogdeprecated")) {
// already migrated
return;
Expand All @@ -1113,7 +1113,57 @@ private void migrateOldEventLogFormat(ItemCollection workitem) {
&& parts[2].contains(".")) {
// backup old format
workitem.setItemValue("$eventlogdeprecated", logEntries);

// Create new list with migrated entries
List<String> migratedEntries = new ArrayList<>();
for (String oldEntry : logEntries) {
String migratedEntry = migrateDeprecatedLogEntry(oldEntry);
migratedEntries.add(migratedEntry);
}
// Update eventlog with migrated entries
workitem.replaceItemValue(EVENTLOG, migratedEntries);
}
}

/**
* Converts a single log entry from old to new format
* Old: timestamp|model-version|1000.10|1000|comment
* New:
* timestamp+timezone|model-version|sourcetask|eventid|targettask|actor|comment
*/
private String migrateDeprecatedLogEntry(String oldEntry) {
String[] parts = oldEntry.split("\\|");
StringBuilder newEntry = new StringBuilder();

// Convert timestamp to timezone format
try {
LocalDateTime oldDateTime = LocalDateTime.parse(parts[0]);
ZonedDateTime zonedDateTime = oldDateTime.atZone(ZoneId.systemDefault());
newEntry.append(zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
} catch (Exception e) {
// If parsing fails, keep original timestamp
newEntry.append(parts[0]);
}

newEntry.append("|")
.append(parts[1]) // model-version
.append("|");

// Split task.event into separate fields
String[] taskEvent = parts[2].split("\\.");
newEntry.append(taskEvent[0]) // sourcetask
.append("|")
.append(taskEvent[1]) // eventid
.append("|")
.append(parts[3]) // targettask
.append("||"); // empty actor field

// Append comment if exists
if (parts.length > 4 && !parts[4].isEmpty()) {
newEntry.append(parts[4]);
}

return newEntry.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -473,6 +474,75 @@ public void testActivityLog() {
assertEquals("comment", parts[6]); // comment
}

/**
* This test verifies the migration of old eventlog entries into the new format
*
* @See Issue 890
*/
@Test
public void testEventLogMigration() {
ItemCollection workitem = new ItemCollection();
workitem.model("1.0.0")
.task(1000);
workitem.replaceItemValue("txtTitel", "Hello");

// Create old format log entries
List<String> oldLogEntries = new ArrayList<>();
oldLogEntries.add("2024-08-27T12:04:20.469|1.0.0|1000.10|1000|");
oldLogEntries.add("2024-08-27T12:05:30.127|1.0.0|1000.20|1100|comment");
workitem.replaceItemValue("$eventlog", oldLogEntries);

// Add a new log entry which should trigger the migration
try {
workitem.replaceItemValue("$editor", "john");
workitem.event(10);
workitem = workflowEngine.getWorkflowKernel().process(workitem);
} catch (Exception e) {
fail();
e.printStackTrace();
}

// Test if old entries were backed up
assertTrue(workitem.hasItem("$eventlogdeprecated"));
List oldBackupLog = workitem.getItemValue("$eventlogdeprecated");
assertEquals(2, oldBackupLog.size());
assertEquals(oldLogEntries.get(0), oldBackupLog.get(0));
assertEquals(oldLogEntries.get(1), oldBackupLog.get(1));

// Verify the new eventlog format
List newLog = workitem.getItemValue("$eventlog");
// We expect all entries to be in new format
assertNotNull(newLog);

// Test new log entry format
String logEntry = (String) newLog.get(0);
String[] parts = logEntry.split("\\|", -1);
assertEquals(7, parts.length);

try {
ZonedDateTime.parse(parts[0]);
assertTrue(true);
} catch (DateTimeParseException e) {
fail("Invalid timestamp format: " + parts[0]);
}

// Test if a second migration attempt has no effect
int currentSize = newLog.size();
try {
workitem.event(10);
workitem = workflowEngine.getWorkflowKernel().process(workitem);
} catch (Exception e) {
fail();
e.printStackTrace();
}
List finalLog = workitem.getItemValue("$eventlog");
assertEquals(currentSize + 1, finalLog.size());

// Test if backup remained unchanged
List finalBackupLog = workitem.getItemValue("$eventlogdeprecated");
assertEquals(oldBackupLog, finalBackupLog);
}

/**
* test generated UUID
*
Expand Down

0 comments on commit 1a2a84c

Please sign in to comment.