Merge pull request #3346 from hmlnarik/KEYCLOAK-3588

KEYCLOAK-3698 Make manual update scripts play nicely with custom JpaEntityProviders
This commit is contained in:
Stian Thorgersen 2016-10-18 19:41:02 +02:00 committed by GitHub
commit a87c08416d

View file

@ -79,10 +79,19 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
// Need ThreadLocal as liquibase doesn't seem to have API to inject custom objects into tasks // Need ThreadLocal as liquibase doesn't seem to have API to inject custom objects into tasks
ThreadLocalSessionContext.setCurrentSession(session); ThreadLocalSessionContext.setCurrentSession(session);
Writer exportWriter = null;
try { try {
// Run update with keycloak master changelog first // Run update with keycloak master changelog first
Liquibase liquibase = getLiquibaseForKeycloakUpdate(connection, defaultSchema); Liquibase liquibase = getLiquibaseForKeycloakUpdate(connection, defaultSchema);
updateChangeSet(liquibase, liquibase.getChangeLogFile(), file); if (file != null) {
exportWriter = new FileWriter(file);
List<ChangeSet> changeSets = getChangeSets(liquibase);
if (! changeSets.isEmpty() && liquibase.getDatabase().getRanChangeSetList().isEmpty()) {
outputChangeLogTableCreationScript(liquibase, exportWriter);
}
}
updateChangeSet(liquibase, liquibase.getChangeLogFile(), exportWriter);
// Run update for each custom JpaEntityProvider // Run update for each custom JpaEntityProvider
Set<JpaEntityProvider> jpaProviders = session.getAllProviders(JpaEntityProvider.class); Set<JpaEntityProvider> jpaProviders = session.getAllProviders(JpaEntityProvider.class);
@ -92,18 +101,25 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
String factoryId = jpaProvider.getFactoryId(); String factoryId = jpaProvider.getFactoryId();
String changelogTableName = JpaUtils.getCustomChangelogTableName(factoryId); String changelogTableName = JpaUtils.getCustomChangelogTableName(factoryId);
liquibase = getLiquibaseForCustomProviderUpdate(connection, defaultSchema, customChangelog, jpaProvider.getClass().getClassLoader(), changelogTableName); liquibase = getLiquibaseForCustomProviderUpdate(connection, defaultSchema, customChangelog, jpaProvider.getClass().getClassLoader(), changelogTableName);
updateChangeSet(liquibase, liquibase.getChangeLogFile(), file); updateChangeSet(liquibase, liquibase.getChangeLogFile(), exportWriter);
} }
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Failed to update database", e); throw new RuntimeException("Failed to update database", e);
} finally { } finally {
ThreadLocalSessionContext.removeCurrentSession(); ThreadLocalSessionContext.removeCurrentSession();
if (exportWriter != null) {
try {
exportWriter.close();
} catch (IOException ioe) {
// ignore
}
}
} }
} }
protected void updateChangeSet(Liquibase liquibase, String changelog, File exportFile) throws LiquibaseException, IOException { protected void updateChangeSet(Liquibase liquibase, String changelog, Writer exportWriter) throws LiquibaseException, IOException {
List<ChangeSet> changeSets = getChangeSets(liquibase); List<ChangeSet> changeSets = getChangeSets(liquibase);
if (!changeSets.isEmpty()) { if (!changeSets.isEmpty()) {
List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList(); List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
@ -117,13 +133,8 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
} }
} }
if (exportFile != null) { if (exportWriter != null) {
try (Writer exportWriter = new FileWriter(exportFile)) { liquibase.update((Contexts) null, new LabelExpression(), exportWriter, false);
if (ranChangeSets.isEmpty()) {
outputChangeLogTableCreationScript(liquibase, exportWriter);
}
liquibase.update((Contexts) null, new LabelExpression(), exportWriter, false);
}
} else { } else {
liquibase.update((Contexts) null); liquibase.update((Contexts) null);
} }