From d6878cf7f351ae95dde0291a8be055fa8b9c1053 Mon Sep 17 00:00:00 2001 From: Michael Bell Date: Fri, 29 Mar 2024 11:55:44 -0700 Subject: [PATCH 1/2] properties explicitly configured --- CHANGELOG.md | 4 ++- pom.xml | 6 ++-- .../db/postgres/embedded/FlywayPreparer.java | 28 +++++++++++++++---- .../postgres/embedded/FlywayPreparerTest.java | 9 +++++- .../resources/db/testing/V3__add_index.sql | 15 ++++++++++ 5 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 src/test/resources/db/testing/V3__add_index.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index db0ae7c2..43a65bcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ + 1.0.3 (unreleased) ---- * commons compress 1.24.0 (used by testcontainers) updated for a CVE. -* testcontainers 1.19.0 +* testcontainers 1.19.6 +* More flexible Flyway Preparer so you can inject properties. 1.0.2 ----- 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..2f01490b 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java +++ b/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.Properties; import javax.sql.DataSource; @@ -33,34 +34,49 @@ public final class FlywayPreparer implements DatabasePreparer { private final List locations; + private final Properties flywayConfiguration; public static FlywayPreparer forClasspathLocation(String... locations) { - return new FlywayPreparer(Arrays.asList(locations)); + return new FlywayPreparer(Arrays.asList(locations), new Properties()); } - private FlywayPreparer(List locations) { + public static FlywayPreparer forClasspathLocation(Properties flywayConfiguration, String... locations) { + return new FlywayPreparer(Arrays.asList(locations), flywayConfiguration); + } + + private FlywayPreparer(List locations, Properties flywayConfiguration) { this.locations = locations; + this.flywayConfiguration = flywayConfiguration; } @Override public void prepare(DataSource ds) throws SQLException { Flyway.configure() + .configuration(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..d9613e9c 100644 --- a/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java +++ b/src/test/java/com/opentable/db/postgres/embedded/FlywayPreparerTest.java @@ -20,6 +20,7 @@ import java.sql.ResultSet; import java.sql.Statement; import java.util.HashSet; +import java.util.Properties; import java.util.Set; import org.junit.Rule; @@ -29,8 +30,14 @@ import com.opentable.db.postgres.junit.PreparedDbRule; public class FlywayPreparerTest { + private static final Properties flywayConfiguration = new Properties(); + static { + flywayConfiguration.setProperty("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); From 8ffbe1c77fcd816fddca6c770d4da3abb198129e Mon Sep 17 00:00:00 2001 From: Michael Bell Date: Fri, 29 Mar 2024 14:26:46 -0700 Subject: [PATCH 2/2] properties explicitly configured --- CHANGELOG.md | 8 +++-- README.md | 10 +++++-- .../db/postgres/embedded/FlywayPreparer.java | 29 +++++++++++++++---- .../postgres/embedded/FlywayPreparerTest.java | 9 +++--- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a65bcc..0eb482ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- 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/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java b/src/main/java/com/opentable/db/postgres/embedded/FlywayPreparer.java index 2f01490b..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,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 @@ -34,25 +38,38 @@ public final class FlywayPreparer implements DatabasePreparer { private final List locations; - private final Properties flywayConfiguration; + private final Map 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 flywayConfiguration, String... locations) { return new FlywayPreparer(Arrays.asList(locations), flywayConfiguration); } - private FlywayPreparer(List locations, Properties 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(flywayConfiguration) + .configuration(fromClassPath) + .envVars() + .configuration(this.flywayConfiguration) .locations(locations.toArray(new String[0])) .dataSource(ds) .load() 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 d9613e9c..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,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; @@ -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 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