Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve flywaypreparer configurations #190

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
<project.build.targetJdk>11</project.build.targetJdk>
<maven.compiler.target>${project.build.targetJdk}</maven.compiler.target>
<project.build.systemJdk>${project.build.targetJdk}</project.build.systemJdk>
<dep.testcontainers.version>1.19.0</dep.testcontainers.version>
<dep.testcontainers.version>1.19.6</dep.testcontainers.version>
<dep.postgres-jdbc.version>42.7.2</dep.postgres-jdbc.version>
<dep.liquibase.version>4.23.1</dep.liquibase.version>
<dep.slf4j.version>1.7.36</dep.slf4j.version>
<dep.flyway.version>8.5.13</dep.flyway.version>
<dep.commons-lang.version>3.12.0</dep.commons-lang.version>
<dep.flyway.version>9.16.3</dep.flyway.version>
<dep.commons-lang.version>3.14.0</dep.commons-lang.version>
<dep.commons-compress.version>1.26.0</dep.commons-compress.version>
<dep.junit.version>4.13.2</dep.junit.version>
<dep.junit5.version>5.8.2</dep.junit5.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,34 +38,62 @@
public final class FlywayPreparer implements DatabasePreparer {

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

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

private FlywayPreparer(List<String> locations) {
public static FlywayPreparer forClasspathLocation(Map<String, String> flywayConfiguration, String... locations) {
return new FlywayPreparer(Arrays.asList(locations), 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(fromClassPath)
.envVars()
.configuration(this.flywayConfiguration)
.locations(locations.toArray(new String[0]))
.dataSource(ds)
.load()
.migrate();
}

public List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +31,14 @@
import com.opentable.db.postgres.junit.PreparedDbRule;

public class FlywayPreparerTest {
private static final Map<String, String> 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 {
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/db/testing/V3__add_index.sql
Original file line number Diff line number Diff line change
@@ -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);
Loading