Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1669 - parsing additional variables in DB urls for lookup tables DB configuration #1670

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading