KEYCLOAK-19824: Use Config variables instead of system variables to change the default jdbc url

This commit is contained in:
Dominik Guhr 2021-11-16 16:42:37 +01:00 committed by Pedro Igor
parent e14e56e0f3
commit 4fd29759ad
2 changed files with 37 additions and 4 deletions

View file

@ -47,12 +47,25 @@ final class DatabasePropertyMappers {
.to("quarkus.datasource.jdbc.url")
.mapFrom("db")
.transformer((value, context) -> Database.getDefaultUrl(value).orElse(value))
.description("The database JDBC URL. If not provided, a default URL is set based on the selected database vendor. " +
"For instance, if using 'postgres', the JDBC URL would be 'jdbc:postgresql://localhost/keycloak'. " +
"The host, database and properties can be overridden by setting the following system properties," +
" respectively: -Dkc.db.url.host, -Dkc.db.url.database, -Dkc.db.url.properties.")
.description("The full database JDBC URL. If not provided, a default URL is set based on the selected database vendor. " +
"For instance, if using 'postgres', the default JDBC URL would be 'jdbc:postgresql://localhost/keycloak'. ")
.paramLabel("jdbc-url")
.build(),
builder().from("db.url.host")
.to("kc.db.url.host")
.description("Sets the hostname of the default JDBC URL of the chosen vendor. If the `db-url` option is set, this option is ignored.")
.paramLabel("hostname")
.build(),
builder().from("db.url.database")
.to("kc.db.url.database")
.description("Sets the database name of the default JDBC URL of the chosen vendor. If the `db-url` option is set, this option is ignored.")
.paramLabel("dbname")
.build(),
builder().from("db.url.properties")
.to("kc.db.url.properties")
.description("Sets the properties of the default JDBC URL of the chosen vendor. If the `db-url` option is set, this option is ignored.")
.paramLabel("properties")
.build(),
builder().from("db.username")
.to("quarkus.datasource.username")
.description("The username of the database user.")

View file

@ -253,6 +253,26 @@ public class ConfigurationTest {
assertEquals("postgresql", config.getConfigValue("quarkus.datasource.db-kind").getValue());
}
@Test
public void testDefaultDbPropertiesGetApplied() {
System.setProperty(CLI_ARGS, "--db=postgres" + ARG_SEPARATOR + "--db-url-host=myhost" + ARG_SEPARATOR + "--db-url-database=kcdb" + ARG_SEPARATOR + "--db-url-properties=?foo=bar");
SmallRyeConfig config = createConfig();
assertEquals("io.quarkus.hibernate.orm.runtime.dialect.QuarkusPostgreSQL10Dialect",
config.getConfigValue("quarkus.hibernate-orm.dialect").getValue());
assertEquals("jdbc:postgresql://myhost/kcdb?foo=bar", config.getConfigValue("quarkus.datasource.jdbc.url").getValue());
assertEquals("postgresql", config.getConfigValue("quarkus.datasource.db-kind").getValue());
}
@Test
public void testSetDbUrlOverridesDefaultDataSource() {
System.setProperty(CLI_ARGS, "--db=mariadb" + ARG_SEPARATOR + "--db-url-host=myhost" + ARG_SEPARATOR + "--db-url=jdbc:mariadb://localhost/keycloak");
SmallRyeConfig config = createConfig();
assertEquals("org.hibernate.dialect.MariaDBDialect",
config.getConfigValue("quarkus.hibernate-orm.dialect").getValue());
assertEquals("jdbc:mariadb://localhost/keycloak", config.getConfigValue("quarkus.datasource.jdbc.url").getValue());
assertEquals("mariadb", config.getConfigValue("quarkus.datasource.db-kind").getValue());
}
@Test
public void testDatabaseProperties() {
System.setProperty("kc.db.url.properties", ";;test=test;test1=test1");