Skip to content

Commit

Permalink
Switch to Spring's flyway and r2dbc configuration
Browse files Browse the repository at this point in the history
Add protocol support for R2DBC configuration
#deploy-levende-arbeidsforhold-ansettelse

Replaced custom R2DBC configurations with Spring's built-in settings in application YAML files and annotated out manual configuration in Java. This streamlines database connectivity and simplifies future maintenance.
  • Loading branch information
krharum committed Oct 21, 2024
1 parent 636975b commit 2d4bbfe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
package no.nav.testnav.levendearbeidsforholdansettelse.config;

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.libs.reactivecore.config.CoreConfig;
import no.nav.testnav.libs.reactivesecurity.config.SecureOAuth2ServerToServerConfiguration;
import no.nav.testnav.libs.standalone.servletsecurity.config.InsecureJwtServerToServerConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.r2dbc.config.EnableR2dbcAuditing;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.reactive.config.EnableWebFlux;

import static io.r2dbc.spi.ConnectionFactoryOptions.DATABASE;
import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER;
import static io.r2dbc.spi.ConnectionFactoryOptions.HOST;
import static io.r2dbc.spi.ConnectionFactoryOptions.PASSWORD;
import static io.r2dbc.spi.ConnectionFactoryOptions.PORT;
import static io.r2dbc.spi.ConnectionFactoryOptions.PROTOCOL;
import static io.r2dbc.spi.ConnectionFactoryOptions.USER;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Configuration
@Import({
Expand All @@ -38,46 +24,46 @@
@EnableR2dbcRepositories
public class ApplicationConfig {

@Value("${config.r2dbc.driver}")
private String driver;

@Value("${config.r2dbc.host:null}")
private String host;

@Value("${config.r2dbc.protocol:null}")
private String protocol;

@Value("${config.r2dbc.port}")
private int port;

@Value("${config.r2dbc.username}")
private String username;

@Value("${config.r2dbc.password}")
private String password;

@Value("${config.r2dbc.database}")
private String database;


@Bean
ConnectionFactory connectionFactory() {

var factory = ConnectionFactoryOptions.builder()
.option(DRIVER, driver)
.option(PORT, port)
.option(USER, username)
.option(PASSWORD, password)
.option(DATABASE, database);

if (isNotBlank(host)) {
factory.option(HOST, host);
}
if (isNotBlank(protocol)) {
factory.option(PROTOCOL, protocol);
}

log.info("ConnectionFactory: {}", factory.build());
return ConnectionFactories.get(factory.build());
}
// @Value("${config.r2dbc.driver}")
// private String driver;
//
// @Value("${config.r2dbc.host:null}")
// private String host;
//
// @Value("${config.r2dbc.protocol:null}")
// private String protocol;
//
// @Value("${config.r2dbc.port}")
// private int port;
//
// @Value("${config.r2dbc.username}")
// private String username;
//
// @Value("${config.r2dbc.password}")
// private String password;
//
// @Value("${config.r2dbc.database}")
// private String database;
//
//
// @Bean
// ConnectionFactory connectionFactory() {
//
// var factory = ConnectionFactoryOptions.builder()
// .option(DRIVER, driver)
// .option(PORT, port)
// .option(USER, username)
// .option(PASSWORD, password)
// .option(DATABASE, database);
//
// if (isNotBlank(host)) {
// factory.option(HOST, host);
// }
// if (isNotBlank(protocol)) {
// factory.option(PROTOCOL, protocol);
// }
//
// log.info("ConnectionFactory: {}", factory.build());
// return ConnectionFactories.get(factory.build());
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ spring:
mode: always
data-locations: classpath:/db/dev/h2-default-config.sql

main:
allow-bean-definition-overriding: true

config:
r2dbc:
driver: h2
protocol: mem
port: 5432
database: testdb
username: sa
password:
# main:
# allow-bean-definition-overriding: true
#
#config:
# r2dbc:
# driver: h2
# protocol: mem
# port: 5432
# database: testdb
# username: sa
# password:

consumers:
testnav-kodeverk-service:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@

spring:
# r2dbc:
# host: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_HOST}
# port: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PORT}
# url: r2dbc:postgresql://${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_HOST}:${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PORT}
# username: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_USERNAME}
# password: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PASSWORD}
# name: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_DATABASE}
# pool:
# validation-query: select 1
r2dbc:
url: r2dbc:${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_URL}
pool:
validation-query: select 1
flyway:
url: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_JDBC_URL}
username: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_USERNAME}
password: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PASSWORD}

config:
r2dbc:
driver: postgresql
host: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_HOST}
port: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PORT}
database: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_DATABASE}
username: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_USERNAME}
password: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PASSWORD}
#config:
# r2dbc:
# driver: postgresql
# host: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_HOST}
# port: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PORT}
# database: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_DATABASE}
# username: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_USERNAME}
# password: ${NAIS_DATABASE_TESTNAV_LEVENDE_ARBEIDSFORHOLD_TESTNAV_LEVENDE_ARBEIDSFORHOLD_PASSWORD}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

config:
r2dbc:
driver: h2
protocol: mem
port: 5432
database: testdb
spring:
flyway:
url: jdbc:h2:mem:testdb
username: sa
password:
password:
driverClassName: org.h2.Driver

0 comments on commit 2d4bbfe

Please sign in to comment.