diff --git a/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java b/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java index a611c3cd2c..65e09dafd8 100644 --- a/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java +++ b/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/LiquibaseJpaUpdaterProvider.java @@ -51,14 +51,14 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } @Override - public void update(KeycloakSession session, Connection connection) { + public void update(KeycloakSession session, Connection connection, String defaultSchema) { logger.debug("Starting database update"); // Need ThreadLocal as liquibase doesn't seem to have API to inject custom objects into tasks ThreadLocalSessionContext.setCurrentSession(session); try { - Liquibase liquibase = getLiquibase(connection); + Liquibase liquibase = getLiquibase(connection, defaultSchema); List changeSets = liquibase.listUnrunChangeSets((Contexts) null); if (!changeSets.isEmpty()) { @@ -93,9 +93,9 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } @Override - public void validate(Connection connection) { + public void validate(Connection connection, String defaultSchema) { try { - Liquibase liquibase = getLiquibase(connection); + Liquibase liquibase = getLiquibase(connection, defaultSchema); liquibase.validate(); } catch (Exception e) { @@ -103,7 +103,7 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { } } - private Liquibase getLiquibase(Connection connection) throws Exception { + private Liquibase getLiquibase(Connection connection, String defaultSchema) throws Exception { ServiceLocator sl = ServiceLocator.getInstance(); if (!System.getProperties().containsKey("liquibase.scan.packages")) { @@ -125,6 +125,9 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider { LogFactory.setInstance(new LogWrapper()); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); + if (defaultSchema != null) { + database.setDefaultSchemaName(defaultSchema); + } return new Liquibase(CHANGELOG, new ClassLoaderResourceAccessor(getClass().getClassLoader()), database); } diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index e66ad65970..06c1bdca5a 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -70,55 +70,56 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide Connection connection = null; - String unitName = config.get("unitName"); String databaseSchema = config.get("databaseSchema"); Map properties = new HashMap(); - // Only load config from keycloak-server.json if unitName is not specified - if (unitName == null) { - unitName = "keycloak-default"; + String unitName = "keycloak-default"; - String dataSource = config.get("dataSource"); - if (dataSource != null) { - if (config.getBoolean("jta", false)) { - properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); - } else { - properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); - } + String dataSource = config.get("dataSource"); + if (dataSource != null) { + if (config.getBoolean("jta", false)) { + properties.put(AvailableSettings.JTA_DATASOURCE, dataSource); } else { - properties.put(AvailableSettings.JDBC_URL, config.get("url")); - properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); - - String user = config.get("user"); - if (user != null) { - properties.put(AvailableSettings.JDBC_USER, user); - } - String password = config.get("password"); - if (password != null) { - properties.put(AvailableSettings.JDBC_PASSWORD, password); - } + properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource); } + } else { + properties.put(AvailableSettings.JDBC_URL, config.get("url")); + properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver")); - String driverDialect = config.get("driverDialect"); - if (driverDialect != null && driverDialect.length() > 0) { - properties.put("hibernate.dialect", driverDialect); + String user = config.get("user"); + if (user != null) { + properties.put(AvailableSettings.JDBC_USER, user); } - - if (databaseSchema != null) { - if (databaseSchema.equals("development-update")) { - properties.put("hibernate.hbm2ddl.auto", "update"); - databaseSchema = null; - } else if (databaseSchema.equals("development-validate")) { - properties.put("hibernate.hbm2ddl.auto", "validate"); - databaseSchema = null; - } + String password = config.get("password"); + if (password != null) { + properties.put(AvailableSettings.JDBC_PASSWORD, password); } - - properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); - properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); } + String driverDialect = config.get("driverDialect"); + if (driverDialect != null && driverDialect.length() > 0) { + properties.put("hibernate.dialect", driverDialect); + } + + String schema = config.get("schema"); + if (schema != null) { + properties.put("hibernate.default_schema", schema); + } + + if (databaseSchema != null) { + if (databaseSchema.equals("development-update")) { + properties.put("hibernate.hbm2ddl.auto", "update"); + databaseSchema = null; + } else if (databaseSchema.equals("development-validate")) { + properties.put("hibernate.hbm2ddl.auto", "validate"); + databaseSchema = null; + } + } + + properties.put("hibernate.show_sql", config.getBoolean("showSql", false)); + properties.put("hibernate.format_sql", config.getBoolean("formatSql", true)); + if (databaseSchema != null) { logger.trace("Updating database"); @@ -140,12 +141,12 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide } if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) { - updater.update(session, connection); + updater.update(session, connection, schema); } else { logger.debug("Database is up to date"); } } else if (databaseSchema.equals("validate")) { - updater.validate(connection); + updater.validate(connection, schema); } else { throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema); } diff --git a/connections/jpa/src/main/java/org/keycloak/connections/jpa/updater/JpaUpdaterProvider.java b/connections/jpa/src/main/java/org/keycloak/connections/jpa/updater/JpaUpdaterProvider.java index 809f2f3aa4..4c67adad55 100755 --- a/connections/jpa/src/main/java/org/keycloak/connections/jpa/updater/JpaUpdaterProvider.java +++ b/connections/jpa/src/main/java/org/keycloak/connections/jpa/updater/JpaUpdaterProvider.java @@ -16,8 +16,8 @@ public interface JpaUpdaterProvider extends Provider { public String getCurrentVersionSql(); - public void update(KeycloakSession session, Connection connection); + public void update(KeycloakSession session, Connection connection, String defaultSchema); - public void validate(Connection connection); + public void validate(Connection connection, String defaultSchema); } diff --git a/docbook/reference/en/en-US/modules/server-installation.xml b/docbook/reference/en/en-US/modules/server-installation.xml index 214d88af49..fd75be0067 100755 --- a/docbook/reference/en/en-US/modules/server-installation.xml +++ b/docbook/reference/en/en-US/modules/server-installation.xml @@ -193,17 +193,14 @@ - unitName + schema - Allow you to specify name of persistence unit if you want to provide your own persistence.xml file for JPA configuration. - If this option is used, then all other configuration options are ignored as you are expected to configure - all JPA/DB properties in your own persistence.xml file. Hence you can remove properties "dataSource" and "databaseSchema" in this case. + Specify the default database schema to use - For more info about Hibernate properties, see Hibernate and JPA documentation .
Tested databases