boolean isUpgradeNeeded = false;
int matchingVersionIndex = -1;
if (isEngineTablePresent()) {
PropertyEntity dbVersionProperty = selectById(PropertyEntity.class,"schema.version");
String dbVersion = dbVersionProperty.getValue();
// Determine index in the sequence of Activiti releases
int index = 0;
while (matchingVersionIndex < 0 && index < ACTIVITI_VERSIONS.size()) {
if (ACTIVITI_VERSIONS.get(index).matches(dbVersion)) {
matchingVersionIndex = index;
} else {
index++;
}
}
// Exception when no match was found: unknown/unsupported version
if (matchingVersionIndex < 0) {
throw new ActivitiException(
"Could not update Activiti database schema: unknown version from database: '"
+ dbVersion + "'");
}
isUpgradeNeeded = (matchingVersionIndex != (ACTIVITI_VERSIONS.size() - 1));
if (isUpgradeNeeded) {
dbVersionProperty.setValue(ProcessEngine.VERSION);
PropertyEntity dbHistoryProperty;
if ("5.0".equals(dbVersion)) {
dbHistoryProperty = new PropertyEntity("schema.history", "create(5.0)");
insert(dbHistoryProperty);
} else {
dbHistoryProperty = selectById(PropertyEntity.class, "schema.history");
}
// Set upgrade history
String dbHistoryValue = dbHistoryProperty.getValue() + " upgrade(" + dbVersion + "->" + ProcessEngine.VERSION + ")";
dbHistoryProperty.setValue(dbHistoryValue);
// Engine upgrade
dbSchemaUpgrade("engine", matchingVersionIndex);
feedback = "upgraded Activiti from " + dbVersion + " to "+ ProcessEngine.VERSION;
}