Skip to content

Commit

Permalink
Extract more common logic in the EngineSqlScriptBasedDbSchemaManager
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Aug 20, 2024
1 parent e9977ef commit 91d8aec
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.flowable.app.engine.AppEngine;
import org.flowable.app.engine.impl.util.CommandContextUtil;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.FlowableVersion;
import org.flowable.common.engine.impl.FlowableVersions;
import org.flowable.common.engine.impl.db.EngineSqlScriptBasedDbSchemaManager;

public class AppDbSchemaManager extends EngineSqlScriptBasedDbSchemaManager {
Expand Down Expand Up @@ -72,6 +74,11 @@ protected String getDbVersionForChangelogVersion(String changeLogVersion) {
return "6.3.0.1";
}

@Override
protected FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion) {
return FlowableVersions.getVersionStateForDbVersion(dbVersion);
}

@Override
protected AbstractEngineConfiguration getEngineConfiguration() {
return CommandContextUtil.getAppEngineConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.flowable.cmmn.engine.CmmnEngine;
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.FlowableVersion;
import org.flowable.common.engine.impl.FlowableVersions;
import org.flowable.common.engine.impl.db.EngineSqlScriptBasedDbSchemaManager;

public class CmmnDbSchemaManager extends EngineSqlScriptBasedDbSchemaManager {
Expand Down Expand Up @@ -89,6 +91,11 @@ protected String getDbVersionForChangelogVersion(String changeLogVersion) {
return "6.1.2.0";
}

@Override
protected FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion) {
return FlowableVersions.getVersionStateForDbVersion(dbVersion);
}

@Override
protected AbstractEngineConfiguration getEngineConfiguration() {
return CommandContextUtil.getCmmnEngineConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.FlowableVersion;
import org.flowable.common.engine.impl.FlowableVersions;
import org.flowable.common.engine.impl.db.EngineSqlScriptBasedDbSchemaManager;
import org.flowable.dmn.engine.DmnEngine;
import org.flowable.dmn.engine.impl.util.CommandContextUtil;
Expand Down Expand Up @@ -80,6 +82,11 @@ protected String getDbVersionForChangelogVersion(String changeLogVersion) {
return "5.99.0.0";
}

@Override
protected FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion) {
return FlowableVersions.getVersionStateForDbVersion(dbVersion);
}

@Override
protected AbstractEngineConfiguration getEngineConfiguration() {
return CommandContextUtil.getDmnEngineConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ public boolean equals(Object obj) {
}
}

public record VersionState(FlowableVersion currentVersion, List<FlowableVersion> nextVersions) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ public static int getFlowableVersionIndexForDbVersion(String dbVersion) {
return matchingVersionIndex;
}

public static FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion) {
int dbVersionIndex = FlowableVersions.getFlowableVersionIndexForDbVersion(dbVersion);
FlowableVersion currentVersion = FlowableVersions.FLOWABLE_VERSIONS.get(dbVersionIndex);
return new FlowableVersion.VersionState(currentVersion, FlowableVersions.FLOWABLE_VERSIONS.subList(dbVersionIndex, FlowableVersions.FLOWABLE_VERSIONS.size()));

}

public static boolean hasCamMigrationVersion(String version) {
int index = findMatchingCamMigrationIndex(version);
if (index >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableException;
Expand Down Expand Up @@ -81,12 +82,17 @@ protected void dbSchemaUpgradeUntil6120(final String component, final int curren

protected void dbSchemaUpgrade(final String component, final int currentDatabaseVersionsIndex, final String engineDbVersion) {
FlowableVersion version = FlowableVersions.FLOWABLE_VERSIONS.get(currentDatabaseVersionsIndex);
String currentVersion = version.getMainVersion();
logger.info("upgrading flowable {} schema from {} to {}", component, currentVersion, FlowableVersions.CURRENT_VERSION);
dbSchemaUpgrade(component, new FlowableVersion.VersionState(version, FlowableVersions.FLOWABLE_VERSIONS.subList(currentDatabaseVersionsIndex + 1, FlowableVersions.FLOWABLE_VERSIONS.size())), engineDbVersion);
}

protected void dbSchemaUpgrade(String component, FlowableVersion.VersionState versionState, String engineDbVersion) {
String currentVersion = versionState.currentVersion().getMainVersion();
List<FlowableVersion> nextVersions = versionState.nextVersions();
logger.info("upgrading flowable {} schema from {} to {}", component, currentVersion, nextVersions.get(nextVersions.size() - 1).getMainVersion());

// Actual execution of schema DDL SQL
for (int i = currentDatabaseVersionsIndex + 1; i < FlowableVersions.FLOWABLE_VERSIONS.size(); i++) {
String nextVersion = FlowableVersions.FLOWABLE_VERSIONS.get(i).getMainVersion();
for (FlowableVersion version : nextVersions) {
String nextVersion = version.getMainVersion();

// Taking care of -SNAPSHOT version in development
if (nextVersion.endsWith("-SNAPSHOT")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import org.flowable.common.engine.api.FlowableWrongDbException;
import org.flowable.common.engine.api.lock.LockManager;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.FlowableVersions;
import org.flowable.common.engine.impl.persistence.entity.PropertyEntity;
import org.flowable.common.engine.impl.persistence.entity.PropertyEntityImpl;
import org.flowable.common.engine.impl.FlowableVersion;

public abstract class EngineSqlScriptBasedDbSchemaManager extends AbstractSqlScriptBasedDbSchemaManager {

Expand All @@ -44,6 +42,8 @@ protected EngineSqlScriptBasedDbSchemaManager(String context) {

protected abstract String getDbVersionForChangelogVersion(String changeLogVersion);

protected abstract FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion);

protected abstract AbstractEngineConfiguration getEngineConfiguration();

@Override
Expand Down Expand Up @@ -127,21 +127,15 @@ public void schemaDrop() {
@Override
public String schemaUpdate() {

PropertyEntity dbVersionProperty = null;
String feedback = null;
boolean isUpgradeNeeded = false;
int matchingVersionIndex = -1;

DbSqlSession dbSqlSession = getDbSqlSession();
boolean isEngineTablePresent = isEngineTablePresent();

ChangeLogVersion changeLogVersion = null;
String dbVersion = null;
if (isEngineTablePresent) {
dbVersionProperty = dbSqlSession.selectById(PropertyEntityImpl.class, getSchemaVersionPropertyName());
if (dbVersionProperty != null) {
dbVersion = dbVersionProperty.getValue();
} else {
dbVersion = getDbVersion();
if (dbVersion == null) {
changeLogVersion = getChangeLogVersion();
dbVersion = changeLogVersion.dbVersion();
}
Expand All @@ -157,14 +151,14 @@ public String schemaUpdate() {
}

try {
FlowableVersion.VersionState versionState = null;
if (isEngineTablePresent) {
matchingVersionIndex = FlowableVersions.getFlowableVersionIndexForDbVersion(dbVersion);
isUpgradeNeeded = (matchingVersionIndex != (FlowableVersions.FLOWABLE_VERSIONS.size() - 1));
versionState = getVersionStateForDbVersion(dbVersion);
}

if (isUpgradeNeeded) {
if (versionState != null && !versionState.nextVersions().isEmpty()) {
// Engine upgrade
dbSchemaUpgrade(context, matchingVersionIndex, dbVersion);
dbSchemaUpgrade(context, versionState, dbVersion);
dbSchemaUpgraded(changeLogVersion);

feedback = "upgraded Flowable from " + dbVersion + " to " + getEngineVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.FlowableVersion;
import org.flowable.common.engine.impl.FlowableVersions;
import org.flowable.common.engine.impl.db.EngineSqlScriptBasedDbSchemaManager;
import org.flowable.eventregistry.impl.EventRegistryEngine;
import org.flowable.eventregistry.impl.cmd.UpdateChannelDefinitionTypeAndImplementationForAllChannelDefinitionsCmd;
Expand Down Expand Up @@ -75,6 +77,11 @@ protected String getDbVersionForChangelogVersion(String changeLogVersion) {
return "6.5.0.0";
}

@Override
protected FlowableVersion.VersionState getVersionStateForDbVersion(String dbVersion) {
return FlowableVersions.getVersionStateForDbVersion(dbVersion);
}

@Override
protected AbstractEngineConfiguration getEngineConfiguration() {
return CommandContextUtil.getEventRegistryConfiguration();
Expand Down

0 comments on commit 91d8aec

Please sign in to comment.