Skip to content

Commit

Permalink
HSEARCH-4674 Allow passing custom Database connection info
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Oct 30, 2023
1 parent 0bd50b9 commit 0da07c6
Showing 1 changed file with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,32 @@ private DatabaseContainer() {
}

public static Configuration configuration() {
if ( !SupportedDatabase.H2.equals( DATABASE ) ) {
DATABASE_CONTAINER.start();
// Let's see if an external DB connection was provided:
String url = System.getProperty( "jdbc.url" );
if ( url != null && !url.trim().isEmpty() ) {
// -Dhibernate.dialect=${db.dialect}
// -Dhibernate.connection.driver_class=${jdbc.driver}
// -Dhibernate.connection.url=${jdbc.url}
// -Dhibernate.connection.username=${jdbc.user}
// -Dhibernate.connection.password=${jdbc.pass}
// -Dhibernate.connection.isolation=${jdbc.isolation}
return DATABASE.configuration( url, DATABASE_CONTAINER )
.withDialect( System.getProperty( "db.dialect" ) )
.withDriver( System.getProperty( "jdbc.driver" ) )
.withUser( System.getProperty( "jdbc.user" ) )
.withPass( System.getProperty( "jdbc.pass" ) )
.withIsolation( System.getProperty( "jdbc.isolation" ) );
}
Configuration configuration = DATABASE.configuration( DATABASE_CONTAINER );

if ( DATABASE_CONTAINER != null && !DATABASE_CONTAINER.isRunning() ) {
synchronized (DATABASE_CONTAINER) {
if ( !DATABASE_CONTAINER.isRunning() ) {
DATABASE_CONTAINER.start();
else {
if ( DATABASE_CONTAINER != null && !DATABASE_CONTAINER.isRunning() ) {
synchronized (DATABASE_CONTAINER) {
if ( !DATABASE_CONTAINER.isRunning() ) {
DATABASE_CONTAINER.start();
}
}
}
return DATABASE.configuration( DATABASE_CONTAINER );
}

return configuration;
}

private enum SupportedDatabase {
Expand All @@ -86,6 +98,11 @@ Configuration configuration(JdbcDatabaseContainer<?> container) {
);
}

@Override
Configuration configuration(String jdbcUrl, JdbcDatabaseContainer<?> container) {
return configuration( container ).withUrl( jdbcUrl );
}

@Override
HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name) {
return null;
Expand Down Expand Up @@ -274,6 +291,17 @@ Configuration configuration(JdbcDatabaseContainer<?> container) {
);
}

Configuration configuration(String jdbcUrl, JdbcDatabaseContainer<?> container) {
return new Configuration(
dialect(),
container.getDriverClassName(),
jdbcUrl,
container.getUsername(),
container.getPassword(),
""
);
}

abstract String dialect();

abstract HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name);
Expand Down Expand Up @@ -375,7 +403,7 @@ public static class Configuration {
private final String pass;
private final String isolation;

public Configuration(String dialect, String driver, String url, String user, String pass, String isolation) {
private Configuration(String dialect, String driver, String url, String user, String pass, String isolation) {
this.dialect = dialect;
this.driver = driver;
this.url = url;
Expand Down Expand Up @@ -424,5 +452,47 @@ public void addAsSpring(BiConsumer<String, String> consumer) {
consumer.accept( "spring.datasource.username", this.user );
consumer.accept( "spring.datasource.password", this.pass );
}

private Configuration withDialect(String dialect) {
if ( dialect == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}

private Configuration withDriver(String driver) {
if ( driver == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}

private Configuration withUrl(String url) {
if ( url == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}

private Configuration withUser(String user) {
if ( user == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}

private Configuration withPass(String pass) {
if ( pass == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}

private Configuration withIsolation(String isolation) {
if ( isolation == null ) {
return this;
}
return new Configuration( dialect, driver, url, user, pass, isolation );
}
}
}

0 comments on commit 0da07c6

Please sign in to comment.