Skip to content

Commit

Permalink
properties explicitly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebell90 committed Mar 29, 2024
1 parent d6878cf commit 8ffbe1c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

1.0.3 (unreleased)
----
* commons compress 1.24.0 (used by testcontainers) updated for a CVE.
* commons compress 1.26.0 (used by testcontainers) updated for a CVE.
* testcontainers 1.19.6
* More flexible Flyway Preparer so you can inject properties.
* Additional options for configuring Flyway Preparer. In order of precedence (first is highest)
1. FlywayPreparer.fromClassPath has an optional first argument to specify a Map of Flyway configurations.
2. Environmental Variables. These are upper cases and periods converted to underscores.
3. Class Path - a file named `flyway.properties` in the class path.
* Using more recently Flyway in tests.

1.0.2
-----
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ as Windows depend primarily on community support. We simply don't have the time

See "Alternatives Considered" as well if this library doesn't appear to fit your needs.


## Basic Usage

In your JUnit test just add (for JUnit 5 example see **Using JUnit5** below):
Expand Down Expand Up @@ -77,6 +76,10 @@ You can easily integrate Flyway or Liquibase database schema migration:
public PreparedDbRule db =
EmbeddedPostgresRules.preparedDatabase(
FlywayPreparer.forClasspathLocation("db/my-db-schema"));
Please note: Recent versions of FLyway will probably hang if you have concurrent indexing. Use
the features described in the 1.0.3 changelog to disable the broken lock feature. See the FlywarePreparerTest
```

##### Liquibase
Expand Down Expand Up @@ -182,6 +185,9 @@ class DaoTestUsingJunit5 {
}
```




## Yes, Junit4 is a compile time dependency

This is because TestContainers has a long outstanding bug to remove this -https://github.com/testcontainers/testcontainers-java/issues/970
Expand Down Expand Up @@ -245,4 +251,4 @@ for our users.
for many.

----
Copyright (C) 2017-2022 OpenTable, Inc
Copyright (C) 2017-2024 OpenTable, Inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
*/
package com.opentable.db.postgres.embedded;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import javax.sql.DataSource;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.internal.configuration.ConfigUtils;

// TODO: Detect missing migration files.
// cf. https://github.com/flyway/flyway/issues/1496
Expand All @@ -34,25 +38,38 @@
public final class FlywayPreparer implements DatabasePreparer {

private final List<String> locations;
private final Properties flywayConfiguration;
private final Map<String, String> flywayConfiguration;

public static FlywayPreparer forClasspathLocation(String... locations) {
return new FlywayPreparer(Arrays.asList(locations), new Properties());
return new FlywayPreparer(Arrays.asList(locations), new HashMap<>());
}

public static FlywayPreparer forClasspathLocation(Properties flywayConfiguration, String... locations) {
public static FlywayPreparer forClasspathLocation(Map<String, String> flywayConfiguration, String... locations) {
return new FlywayPreparer(Arrays.asList(locations), flywayConfiguration);
}

private FlywayPreparer(List<String> locations, Properties flywayConfiguration) {
private FlywayPreparer(List<String> locations, Map<String, String> flywayConfiguration) {
this.locations = locations;
this.flywayConfiguration = flywayConfiguration;
}

@Override
public void prepare(DataSource ds) throws SQLException {
// Precedence:
// 1. Method set FlywayPreparer Map.
// 2. Env vars
// 3. Class path
Map<String, String> fromClassPath;
try (InputStream inputStream = FlywayPreparer.class.getResourceAsStream("/flyway.properties")) {
fromClassPath = ConfigUtils.loadConfigurationFromInputStream(inputStream);
} catch (IOException e) {
throw new SQLException(e);
}
flywayConfiguration.putAll(this.flywayConfiguration);
Flyway.configure()
.configuration(flywayConfiguration)
.configuration(fromClassPath)
.envVars()
.configuration(this.flywayConfiguration)
.locations(locations.toArray(new String[0]))
.dataSource(ds)
.load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Map;
import java.util.Set;

import org.junit.Rule;
Expand All @@ -30,13 +31,13 @@
import com.opentable.db.postgres.junit.PreparedDbRule;

public class FlywayPreparerTest {
private static final Properties flywayConfiguration = new Properties();
private static final Map<String, String> flywayConfiguration = new HashMap<>();
static {
flywayConfiguration.setProperty("flyway.postgresql.transactional.lock", "false");
flywayConfiguration.put("flyway.postgresql.transactional.lock", "false");
}
@Rule
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(
FlywayPreparer.forClasspathLocation( flywayConfiguration, "db/testing")
FlywayPreparer.forClasspathLocation(flywayConfiguration, "db/testing")
);

@Test
Expand Down

0 comments on commit 8ffbe1c

Please sign in to comment.