KEYCLOAK-3588: DB up-to-date check should not modify DB

The DB up-to-date check uses Liquibase.listUnrunChangeSets() that in
its available variants unconditionally creates a DatabaseChangeLog
tables. Until the variant of listUnrunChangeSets() that suppresses this
behaviour is made public [1] (currently it is protected), workaround
has been implemented that invokes less invasive variant of
listUnrunChangeSets() via reflection.

[1] https://liquibase.jira.com/browse/CORE-2919
This commit is contained in:
Hynek Mlnarik 2016-10-14 14:09:44 +02:00
parent 1f3d29964f
commit f256e2b102

View file

@ -96,7 +96,7 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
protected void updateChangeSet(Liquibase liquibase, String changelog, File exportFile) throws LiquibaseException, IOException { protected void updateChangeSet(Liquibase liquibase, String changelog, File exportFile) throws LiquibaseException, IOException {
List<ChangeSet> changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression()); List<ChangeSet> changeSets = getChangeSets(liquibase);
if (!changeSets.isEmpty()) { if (!changeSets.isEmpty()) {
List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList(); List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
if (ranChangeSets.isEmpty()) { if (ranChangeSets.isEmpty()) {
@ -160,7 +160,8 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
} }
protected Status validateChangeSet(Liquibase liquibase, String changelog) throws LiquibaseException { protected Status validateChangeSet(Liquibase liquibase, String changelog) throws LiquibaseException {
List<ChangeSet> changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression()); List<ChangeSet> changeSets = getChangeSets(liquibase);
if (!changeSets.isEmpty()) { if (!changeSets.isEmpty()) {
if (changeSets.size() == liquibase.getDatabaseChangeLog().getChangeSets().size()) { if (changeSets.size() == liquibase.getDatabaseChangeLog().getChangeSets().size()) {
return Status.EMPTY; return Status.EMPTY;
@ -174,6 +175,14 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
} }
} }
@SuppressWarnings("unchecked")
private List<ChangeSet> getChangeSets(Liquibase liquibase) {
// TODO: When https://liquibase.jira.com/browse/CORE-2919 is resolved, replace the following two lines with:
// List<ChangeSet> changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression(), false);
Method listUnrunChangeSets = Reflections.findDeclaredMethod(Liquibase.class, "listUnrunChangeSets", Contexts.class, LabelExpression.class, boolean.class);
return Reflections.invokeMethod(true, listUnrunChangeSets, List.class, liquibase, (Contexts) null, new LabelExpression(), false);
}
private Liquibase getLiquibaseForKeycloakUpdate(Connection connection, String defaultSchema) throws LiquibaseException { private Liquibase getLiquibaseForKeycloakUpdate(Connection connection, String defaultSchema) throws LiquibaseException {
LiquibaseConnectionProvider liquibaseProvider = session.getProvider(LiquibaseConnectionProvider.class); LiquibaseConnectionProvider liquibaseProvider = session.getProvider(LiquibaseConnectionProvider.class);
return liquibaseProvider.getLiquibase(connection, defaultSchema); return liquibaseProvider.getLiquibase(connection, defaultSchema);