diff --git a/CHANGELOG.md b/CHANGELOG.md index db0ae7c2..0eb482ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,13 @@ + 1.0.3 (unreleased) ---- -* commons compress 1.24.0 (used by testcontainers) updated for a CVE. -* testcontainers 1.19.0 +* commons compress 1.26.0 (used by testcontainers) updated for a CVE. +* testcontainers 1.19.6 +* 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 ----- diff --git a/README.md b/README.md index d677db8a..873d8bed 100644 --- a/README.md +++ b/README.md @@ -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): @@ -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 @@ -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 @@ -245,4 +251,4 @@ for our users. for many. ---- -Copyright (C) 2017-2022 OpenTable, Inc +Copyright (C) 2017-2024 OpenTable, Inc diff --git a/pom.xml b/pom.xml index 6b856784..3c97ae61 100644 --- a/pom.xml +++ b/pom.xml @@ -48,12 +48,12 @@ 11 ${project.build.targetJdk} ${project.build.targetJdk} - 1.19.0 + 1.19.6 42.7.2 4.23.1 1.7.36 - 8.5.13 - 3.12.0 + 9.16.3 + 3.14.0 1.26.0 4.13.2 5.8.2 diff --git a/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java b/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java index d7e720e7..f3b6945a 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java +++ b/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java @@ -13,14 +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 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 @@ -33,34 +38,62 @@ public final class FlywayPreparer implements DatabasePreparer { private final List locations; + private final Map flywayConfiguration; public static FlywayPreparer forClasspathLocation(String... locations) { - return new FlywayPreparer(Arrays.asList(locations)); + return new FlywayPreparer(Arrays.asList(locations), new HashMap<>()); } - private FlywayPreparer(List locations) { + public static FlywayPreparer forClasspathLocation(Map flywayConfiguration, String... locations) { + return new FlywayPreparer(Arrays.asList(locations), flywayConfiguration); + } + + private FlywayPreparer(List locations, Map 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 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(fromClassPath) + .envVars() + .configuration(this.flywayConfiguration) .locations(locations.toArray(new String[0])) .dataSource(ds) .load() .migrate(); } + public List getLocations() { + return locations; + } + @Override - public boolean equals(Object obj) { - if (! (obj instanceof FlywayPreparer)) { + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { return false; } - return Objects.equals(locations, ((FlywayPreparer) obj).locations); + FlywayPreparer that = (FlywayPreparer) o; + return Objects.equals(locations, that.locations) && Objects.equals(flywayConfiguration, that.flywayConfiguration); } @Override public int hashCode() { - return Objects.hashCode(locations); + return Objects.hash(locations, flywayConfiguration); } } diff --git a/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java b/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java index 1519f1ab..4644e479 100644 --- a/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java +++ b/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java @@ -19,7 +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.Map; import java.util.Set; import org.junit.Rule; @@ -29,8 +31,14 @@ import com.opentable.db.postgres.junit.PreparedDbRule; public class FlywayPreparerTest { + private static final Map flywayConfiguration = new HashMap<>(); + static { + flywayConfiguration.put("flyway.postgresql.transactional.lock", "false"); + } @Rule - public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.forClasspathLocation("db/testing")); + public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase( + FlywayPreparer.forClasspathLocation(flywayConfiguration, "db/testing") + ); @Test public void testTablesMade() throws Exception { diff --git a/src/test/resources/db/testing/V3__add_index.sql b/src/test/resources/db/testing/V3__add_index.sql new file mode 100644 index 00000000..7c07b152 --- /dev/null +++ b/src/test/resources/db/testing/V3__add_index.sql @@ -0,0 +1,15 @@ +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +CREATE INDEX CONCURRENTLY ON foo (test);