commit
351a17f68d
4 changed files with 52 additions and 51 deletions
|
@ -51,14 +51,14 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(KeycloakSession session, Connection connection) {
|
public void update(KeycloakSession session, Connection connection, String defaultSchema) {
|
||||||
logger.debug("Starting database update");
|
logger.debug("Starting database update");
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Liquibase liquibase = getLiquibase(connection);
|
Liquibase liquibase = getLiquibase(connection, defaultSchema);
|
||||||
|
|
||||||
List<ChangeSet> changeSets = liquibase.listUnrunChangeSets((Contexts) null);
|
List<ChangeSet> changeSets = liquibase.listUnrunChangeSets((Contexts) null);
|
||||||
if (!changeSets.isEmpty()) {
|
if (!changeSets.isEmpty()) {
|
||||||
|
@ -93,9 +93,9 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(Connection connection) {
|
public void validate(Connection connection, String defaultSchema) {
|
||||||
try {
|
try {
|
||||||
Liquibase liquibase = getLiquibase(connection);
|
Liquibase liquibase = getLiquibase(connection, defaultSchema);
|
||||||
|
|
||||||
liquibase.validate();
|
liquibase.validate();
|
||||||
} catch (Exception e) {
|
} 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();
|
ServiceLocator sl = ServiceLocator.getInstance();
|
||||||
|
|
||||||
if (!System.getProperties().containsKey("liquibase.scan.packages")) {
|
if (!System.getProperties().containsKey("liquibase.scan.packages")) {
|
||||||
|
@ -125,6 +125,9 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
|
||||||
|
|
||||||
LogFactory.setInstance(new LogWrapper());
|
LogFactory.setInstance(new LogWrapper());
|
||||||
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
|
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
|
||||||
|
if (defaultSchema != null) {
|
||||||
|
database.setDefaultSchemaName(defaultSchema);
|
||||||
|
}
|
||||||
return new Liquibase(CHANGELOG, new ClassLoaderResourceAccessor(getClass().getClassLoader()), database);
|
return new Liquibase(CHANGELOG, new ClassLoaderResourceAccessor(getClass().getClassLoader()), database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,55 +70,56 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
|
||||||
|
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
|
|
||||||
String unitName = config.get("unitName");
|
|
||||||
String databaseSchema = config.get("databaseSchema");
|
String databaseSchema = config.get("databaseSchema");
|
||||||
|
|
||||||
Map<String, Object> properties = new HashMap<String, Object>();
|
Map<String, Object> properties = new HashMap<String, Object>();
|
||||||
|
|
||||||
// Only load config from keycloak-server.json if unitName is not specified
|
String unitName = "keycloak-default";
|
||||||
if (unitName == null) {
|
|
||||||
unitName = "keycloak-default";
|
|
||||||
|
|
||||||
String dataSource = config.get("dataSource");
|
String dataSource = config.get("dataSource");
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
if (config.getBoolean("jta", false)) {
|
if (config.getBoolean("jta", false)) {
|
||||||
properties.put(AvailableSettings.JTA_DATASOURCE, dataSource);
|
properties.put(AvailableSettings.JTA_DATASOURCE, dataSource);
|
||||||
} else {
|
|
||||||
properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
properties.put(AvailableSettings.JDBC_URL, config.get("url"));
|
properties.put(AvailableSettings.NON_JTA_DATASOURCE, dataSource);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
properties.put(AvailableSettings.JDBC_URL, config.get("url"));
|
||||||
|
properties.put(AvailableSettings.JDBC_DRIVER, config.get("driver"));
|
||||||
|
|
||||||
String driverDialect = config.get("driverDialect");
|
String user = config.get("user");
|
||||||
if (driverDialect != null && driverDialect.length() > 0) {
|
if (user != null) {
|
||||||
properties.put("hibernate.dialect", driverDialect);
|
properties.put(AvailableSettings.JDBC_USER, user);
|
||||||
}
|
}
|
||||||
|
String password = config.get("password");
|
||||||
if (databaseSchema != null) {
|
if (password != null) {
|
||||||
if (databaseSchema.equals("development-update")) {
|
properties.put(AvailableSettings.JDBC_PASSWORD, password);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
if (databaseSchema != null) {
|
||||||
logger.trace("Updating database");
|
logger.trace("Updating database");
|
||||||
|
|
||||||
|
@ -140,12 +141,12 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) {
|
if (currentVersion == null || !JpaUpdaterProvider.LAST_VERSION.equals(currentVersion)) {
|
||||||
updater.update(session, connection);
|
updater.update(session, connection, schema);
|
||||||
} else {
|
} else {
|
||||||
logger.debug("Database is up to date");
|
logger.debug("Database is up to date");
|
||||||
}
|
}
|
||||||
} else if (databaseSchema.equals("validate")) {
|
} else if (databaseSchema.equals("validate")) {
|
||||||
updater.validate(connection);
|
updater.validate(connection, schema);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema);
|
throw new RuntimeException("Invalid value for databaseSchema: " + databaseSchema);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ public interface JpaUpdaterProvider extends Provider {
|
||||||
|
|
||||||
public String getCurrentVersionSql();
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,17 +193,14 @@
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term>unitName</term>
|
<term>schema</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Allow you to specify name of persistence unit if you want to provide your own persistence.xml file for JPA configuration.
|
Specify the default database schema to use
|
||||||
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.
|
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
</variablelist>
|
</variablelist>
|
||||||
For more info about Hibernate properties, see <ulink url="http://hibernate.org/orm/documentation/">Hibernate and JPA documentation</ulink> .
|
|
||||||
</para>
|
</para>
|
||||||
<section>
|
<section>
|
||||||
<title>Tested databases</title>
|
<title>Tested databases</title>
|
||||||
|
|
Loading…
Reference in a new issue