From f256e2b10272dbf5b68e0d00b471e3b203b8506d Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Fri, 14 Oct 2016 14:09:44 +0200 Subject: [PATCH] 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 --- .../liquibase/LiquibaseJpaUpdaterProvider.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 957c79a328..8ecba4f51c 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 @@ -96,7 +96,7 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { protected void updateChangeSet(Liquibase liquibase, String changelog, File exportFile) throws LiquibaseException, IOException { - List changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression()); + List changeSets = getChangeSets(liquibase); if (!changeSets.isEmpty()) { List ranChangeSets = liquibase.getDatabase().getRanChangeSetList(); if (ranChangeSets.isEmpty()) { @@ -160,7 +160,8 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } protected Status validateChangeSet(Liquibase liquibase, String changelog) throws LiquibaseException { - List changeSets = liquibase.listUnrunChangeSets((Contexts) null, new LabelExpression()); + List changeSets = getChangeSets(liquibase); + if (!changeSets.isEmpty()) { if (changeSets.size() == liquibase.getDatabaseChangeLog().getChangeSets().size()) { return Status.EMPTY; @@ -174,6 +175,14 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } } + @SuppressWarnings("unchecked") + private List getChangeSets(Liquibase liquibase) { + // 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); + 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 { LiquibaseConnectionProvider liquibaseProvider = session.getProvider(LiquibaseConnectionProvider.class); return liquibaseProvider.getLiquibase(connection, defaultSchema);