Skip to content

Commit

Permalink
1669 - parsing additional variables in DB urls for lookup tables DB c…
Browse files Browse the repository at this point in the history
…onfiguration (#1670)
  • Loading branch information
Vladysl authored Apr 30, 2024
1 parent 38375ce commit 530463e
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import java.net.URI;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
Expand All @@ -20,7 +22,8 @@

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

@Bean(destroyMethod = "dispose")
@Primary
Expand Down Expand Up @@ -56,13 +59,12 @@ public ConnectionPool databaseClientForCustomSchema(
@Value("${spring.custom-datasource.password:}") final String password,
final DataSourceProperties dataSourceProperties,
final R2dbcProperties properties) {
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()
final ConnectionFactory factory =
ConnectionFactories.get(
ConnectionFactoryOptions.parse(getCustomSchemaDBUrl(url, dataSourceProperties.getUrl())).mutate()
.option(ConnectionFactoryOptions.PROTOCOL, "postgresql")
.option(ConnectionFactoryOptions.USER, finalDBUsername)
.option(ConnectionFactoryOptions.PASSWORD, finalDBPassword)
Expand Down Expand Up @@ -107,4 +109,13 @@ public ReactiveTransactionManager reactiveCustomTransactionManager(
@Qualifier("customConnectionPool") final ConnectionFactory connectionFactory) {
return new R2dbcTransactionManager(connectionFactory);
}

private String getCustomSchemaDBUrl(final String customUrl, final String dataSourceUrl) {
final String dbUrl = (StringUtils.isNotBlank(customUrl) ? customUrl : dataSourceUrl);
final URIBuilder uri = new URIBuilder(URI.create(dbUrl.substring("jdbc:".length())));

uri.addParameter(SCHEMA_PART_FOR_CUSTOM_DB_URL, VALUE_PART_FOR_CUSTOM_DB_URL);

return "r2dbc:" + uri;
}
}

0 comments on commit 530463e

Please sign in to comment.