diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java index 207cef848c..48c41fddf2 100755 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java @@ -114,7 +114,7 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } protected void updateChangeSet(Liquibase liquibase, String changelog, Writer exportWriter) throws LiquibaseException, IOException { - List changeSets = getChangeSets(liquibase); + List changeSets = getLiquibaseUnrunChangeSets(liquibase); if (!changeSets.isEmpty()) { List ranChangeSets = liquibase.getDatabase().getRanChangeSetList(); if (ranChangeSets.isEmpty()) { @@ -139,11 +139,11 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { logger.debugv("Completed database update for changelog {0}", changelog); } else { logger.debugv("Database is up to date for changelog {0}", changelog); - - // Needs to restart liquibase services to clear changeLogHistory. - Method resetServices = Reflections.findDeclaredMethod(Liquibase.class, "resetServices"); - Reflections.invokeMethod(true, resetServices, liquibase); } + + // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance(). + // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here + resetLiquibaseServices(liquibase); } private void outputChangeLogTableCreationScript(Liquibase liquibase, final Writer exportWriter) throws DatabaseException { @@ -202,23 +202,35 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } protected Status validateChangeSet(Liquibase liquibase, String changelog) throws LiquibaseException { - List changeSets = getChangeSets(liquibase); + final Status result; + List changeSets = getLiquibaseUnrunChangeSets(liquibase); if (!changeSets.isEmpty()) { if (changeSets.size() == liquibase.getDatabaseChangeLog().getChangeSets().size()) { - return Status.EMPTY; + result = Status.EMPTY; } else { logger.debugf("Validation failed. Database is not up-to-date for changelog %s", changelog); - return Status.OUTDATED; + result = Status.OUTDATED; } } else { logger.debugf("Validation passed. Database is up-to-date for changelog %s", changelog); - return Status.VALID; + result = Status.VALID; } + + // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance(). + // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here + resetLiquibaseServices(liquibase); + + return result; + } + + private void resetLiquibaseServices(Liquibase liquibase) { + Method resetServices = Reflections.findDeclaredMethod(Liquibase.class, "resetServices"); + Reflections.invokeMethod(true, resetServices, liquibase); } @SuppressWarnings("unchecked") - private List getChangeSets(Liquibase liquibase) { + private List getLiquibaseUnrunChangeSets(Liquibase liquibase) { // TODO tracked as: https://issues.jboss.org/browse/KEYCLOAK-3730 // TODO: When https://liquibase.jira.com/browse/CORE-2919 is resolved, replace the following two lines with: // List changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression(), false);