Skip to content

Commit

Permalink
1584 - spring.custom-datasource now optional parameters. By default, …
Browse files Browse the repository at this point in the history
…will be used spring.datasource parameters. (#1600)
  • Loading branch information
Vladysl authored Jan 31, 2024
1 parent 285e2d9 commit deec532
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions docker/demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ services:
- SPRING_DATASOURCE_URL=jdbc:postgresql://database:5432/${POSTGRES_DATABASE}
- SPRING_DATASOURCE_USERNAME=${POSTGRES_USER}
- SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
- SPRING_CUSTOM-DATASOURCE_URL=jdbc:postgresql://database:5432/${POSTGRES_DATABASE}?schema=lookup_tables_schema
- SPRING_CUSTOM-DATASOURCE_USERNAME=${POSTGRES_USER}
- SPRING_CUSTOM-DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
depends_on:
- database
ports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
Expand All @@ -19,6 +20,7 @@

@Configuration
public class R2DBCConfiguration {
public static final String SCHEMA_PART_FOR_DB_URL = "?schema=lookup_tables_schema";

@Bean(destroyMethod = "dispose")
@Primary
Expand Down Expand Up @@ -49,15 +51,21 @@ public ConnectionPool connectionFactory(final DataSourceProperties dataSourcePro

@Bean(name = "customConnectionPool", destroyMethod = "dispose")
public ConnectionPool databaseClientForCustomSchema(
@Value("${spring.custom-datasource.url}") final String url,
@Value("${spring.custom-datasource.username}") final String username,
@Value("${spring.custom-datasource.password}") final String password,
@Value("${spring.custom-datasource.url:}") final String url,
@Value("${spring.custom-datasource.username:}") final String username,
@Value("${spring.custom-datasource.password:}") final String password,
final DataSourceProperties dataSourceProperties,
final R2dbcProperties properties) {
final String r2dbcUrl = url.replace("jdbc", "r2dbc");
final String finalDBUrl =
(StringUtils.isBlank(url) ? dataSourceProperties.getUrl() : url) + SCHEMA_PART_FOR_DB_URL;
final String finalDBUsername = StringUtils.isBlank(username) ? dataSourceProperties.getUsername() : username;
final String finalDBPassword = StringUtils.isBlank(password) ? dataSourceProperties.getPassword() : password;

final String r2dbcUrl = finalDBUrl.replace("jdbc", "r2dbc");
final ConnectionFactory factory = ConnectionFactories.get(ConnectionFactoryOptions.parse(r2dbcUrl).mutate()
.option(ConnectionFactoryOptions.PROTOCOL, "postgresql")
.option(ConnectionFactoryOptions.USER, username)
.option(ConnectionFactoryOptions.PASSWORD, password)
.option(ConnectionFactoryOptions.USER, finalDBUsername)
.option(ConnectionFactoryOptions.PASSWORD, finalDBPassword)
.build());

final R2dbcProperties.Pool pool = properties.getPool();
Expand Down
8 changes: 4 additions & 4 deletions odd-platform-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ spring:
url: jdbc:postgresql://127.0.0.1:5432/odd-platform
username: odd-platform
password: odd-platform-password
custom-datasource:
url: jdbc:postgresql://127.0.0.1:5432/odd-platform?schema=lookup_tables_schema
username: odd-platform
password: odd-platform-password
# custom-datasource:
# url: jdbc:postgresql://127.0.0.1:5432/odd-platform
# username: odd-platform
# password: odd-platform-password
jooq:
sql-dialect: postgres
codec:
Expand Down

0 comments on commit deec532

Please sign in to comment.