locations) {
+ /**
+ * Creates a new instance of the preparer with the specified configuration properties.
+ *
+ * Example of use:
+ *
{@code
+ * FlywayPreparer preparer = FlywayPreparer.fromConfiguration(Map.of(
+ * "flyway.locations", "db/migration",
+ * "flyway.postgresql.transactional.lock", "false"));
+ * }
+ *
+ * A list of all available configuration properties can be found here.
+ */
+ public static FlywayPreparer fromConfiguration(Map configuration) {
+ FluentConfiguration config = Flyway.configure().configuration(configuration);
+ return new FlywayPreparer(config, null, new HashMap<>(configuration));
+ }
+
+ private FlywayPreparer(FluentConfiguration configuration, List locations, Map properties) {
this.configuration = configuration;
this.locations = locations;
+ this.properties = properties;
}
@Override
@@ -55,15 +74,15 @@ public void prepare(DataSource ds) throws SQLException {
}
@Override
- public boolean equals(Object obj) {
- if (! (obj instanceof FlywayPreparer)) {
- return false;
- }
- return Objects.equals(locations, ((FlywayPreparer) obj).locations);
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ FlywayPreparer that = (FlywayPreparer) o;
+ return Objects.equals(locations, that.locations) && Objects.equals(properties, that.properties);
}
@Override
public int hashCode() {
- return Objects.hashCode(locations);
+ return Objects.hash(locations, properties);
}
}
diff --git a/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java b/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java
index 7d0c2de3..d611681e 100644
--- a/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java
+++ b/src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java
@@ -19,8 +19,11 @@
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
+import liquibase.resource.FileSystemResourceAccessor;
+import liquibase.resource.ResourceAccessor;
import javax.sql.DataSource;
+import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
@@ -30,45 +33,60 @@
public final class LiquibasePreparer implements DatabasePreparer {
private final String location;
+ private final ResourceAccessor accessor;
private final Contexts contexts;
public static LiquibasePreparer forClasspathLocation(String location) {
- return new LiquibasePreparer(location, new Contexts());
+ return forClasspathLocation(location, null);
}
+
public static LiquibasePreparer forClasspathLocation(String location, Contexts contexts) {
- return new LiquibasePreparer(location, contexts);
+ return new LiquibasePreparer(location, new ClassLoaderResourceAccessor(), contexts);
+ }
+
+ public static LiquibasePreparer forFile(File file) {
+ return forFile(file, null);
+ }
+
+ public static LiquibasePreparer forFile(File file, Contexts contexts) {
+ if (file == null)
+ throw new IllegalArgumentException("Missing file");
+ File dir = file.getParentFile();
+ if (dir == null)
+ throw new IllegalArgumentException("Cannot get parent dir from file");
+
+ return new LiquibasePreparer(file.getName(), new FileSystemResourceAccessor(dir), contexts);
}
- private LiquibasePreparer(String location, Contexts contexts) {
+ private LiquibasePreparer(String location, ResourceAccessor accessor, Contexts contexts) {
this.location = location;
- this.contexts = contexts;
+ this.accessor = accessor;
+ this.contexts = contexts != null ? contexts : new Contexts();
}
@Override
public void prepare(DataSource ds) throws SQLException {
- Connection connection = null;
- try {
- connection = ds.getConnection();
+ try (Connection connection = ds.getConnection()) {
Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
- Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database);
+ Liquibase liquibase = new Liquibase(location, accessor, database);
liquibase.update(contexts);
} catch (LiquibaseException e) {
throw new SQLException(e);
- } finally {
- if (connection != null) {
- connection.rollback();
- connection.close();
- }
}
}
@Override
- public boolean equals(Object obj) {
- return obj instanceof LiquibasePreparer && Objects.equals(location, ((LiquibasePreparer) obj).location);
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ LiquibasePreparer that = (LiquibasePreparer) o;
+ return Objects.equals(location, that.location)
+ && Objects.equals(accessor, that.accessor)
+ && Objects.equals(contexts.getContexts(), that.contexts.getContexts());
}
@Override
public int hashCode() {
- return Objects.hashCode(location);
+ return Objects.hash(location, accessor, contexts.getContexts());
}
}
diff --git a/src/main/resources/sh/detect_linux_distribution.sh b/src/main/resources/sh/detect_linux_distribution.sh
index 2ef4f015..8304880a 100644
--- a/src/main/resources/sh/detect_linux_distribution.sh
+++ b/src/main/resources/sh/detect_linux_distribution.sh
@@ -12,7 +12,7 @@ detect_linux_distribution() {
else
DISTRO=''
fi
- echo $DISTRO
+ echo "$DISTRO"
}
-echo $(detect_linux_distribution)
\ No newline at end of file
+detect_linux_distribution
diff --git a/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java
index ff765eef..cebb9376 100644
--- a/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java
+++ b/src/test/java/io/zonky/test/db/postgres/junit/FlywayPreparerTest.java
@@ -18,6 +18,7 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
+import java.util.Collections;
import io.zonky.test.db.postgres.embedded.FlywayPreparer;
import org.junit.Rule;
@@ -25,7 +26,8 @@
public class FlywayPreparerTest {
@Rule
- public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.forClasspathLocation("db/testing"));
+ public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(FlywayPreparer.fromConfiguration(
+ Collections.singletonMap("flyway.locations", "db/testing")));
@Test
public void testTablesMade() throws Exception {
diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java
similarity index 96%
rename from src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java
rename to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java
index d9b9908f..647a514d 100644
--- a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java
+++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java
@@ -24,7 +24,7 @@
import static org.junit.Assert.assertEquals;
-public class LiquibasePreparerContextTest {
+public class LiquibasePreparerClasspathContextTest {
@Rule
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test")));
diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java
similarity index 96%
rename from src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java
rename to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java
index 4c646ca5..07156b36 100644
--- a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java
+++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java
@@ -23,7 +23,7 @@
import static org.junit.Assert.assertEquals;
-public class LiquibasePreparerTest {
+public class LiquibasePreparerClasspathTest {
@Rule
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java
new file mode 100644
index 00000000..f1b38523
--- /dev/null
+++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileContextTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+package io.zonky.test.db.postgres.junit;
+
+import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
+import liquibase.Contexts;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import static org.junit.Assert.assertEquals;
+
+public class LiquibasePreparerFileContextTest {
+
+ @Rule
+ public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test")));
+
+ @Test
+ public void testEmptyTables() throws Exception {
+ try (Connection c = db.getTestDatabase().getConnection();
+ Statement s = c.createStatement()) {
+ ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo");
+ rs.next();
+ assertEquals(0, rs.getInt(1));
+ }
+ }
+}
diff --git a/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java
new file mode 100644
index 00000000..233bda84
--- /dev/null
+++ b/src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerFileTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package io.zonky.test.db.postgres.junit;
+
+import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import static org.junit.Assert.assertEquals;
+
+public class LiquibasePreparerFileTest {
+
+ @Rule
+ public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml")));
+
+ @Test
+ public void testTablesMade() throws Exception {
+ try (Connection c = db.getTestDatabase().getConnection();
+ Statement s = c.createStatement()) {
+ ResultSet rs = s.executeQuery("SELECT * FROM foo");
+ rs.next();
+ assertEquals("bar", rs.getString(1));
+ }
+ }
+}
diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java
similarity index 96%
rename from src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java
rename to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java
index bd229daa..436b64e8 100644
--- a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java
+++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java
@@ -24,7 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
-public class LiquibasePreparerContextTest {
+public class LiquibasePreparerClasspathContextTest {
@RegisterExtension
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test")));
diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java
similarity index 96%
rename from src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java
rename to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java
index 8653d488..1319ee81 100644
--- a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java
+++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java
@@ -23,7 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
-public class LiquibasePreparerTest {
+public class LiquibasePreparerClasspathTest {
@RegisterExtension
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java
new file mode 100644
index 00000000..837792bd
--- /dev/null
+++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileContextTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+package io.zonky.test.db.postgres.junit5;
+
+import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
+import liquibase.Contexts;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class LiquibasePreparerFileContextTest {
+
+ @RegisterExtension
+ public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test")));
+
+ @Test
+ public void testEmptyTables() throws Exception {
+ try (Connection c = db.getTestDatabase().getConnection();
+ Statement s = c.createStatement()) {
+ ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo");
+ rs.next();
+ assertEquals(0, rs.getInt(1));
+ }
+ }
+}
diff --git a/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java
new file mode 100644
index 00000000..8c0c5378
--- /dev/null
+++ b/src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerFileTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package io.zonky.test.db.postgres.junit5;
+
+import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class LiquibasePreparerFileTest {
+
+ @RegisterExtension
+ public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml")));
+
+ @Test
+ public void testTablesMade() throws Exception {
+ try (Connection c = db.getTestDatabase().getConnection();
+ Statement s = c.createStatement()) {
+ ResultSet rs = s.executeQuery("SELECT * FROM foo");
+ rs.next();
+ assertEquals("bar", rs.getString(1));
+ }
+ }
+}
diff --git a/src/test/resources/liqui/master-test.xml b/src/test/resources/liqui/master-test.xml
index 94fe4963..3670a29b 100644
--- a/src/test/resources/liqui/master-test.xml
+++ b/src/test/resources/liqui/master-test.xml
@@ -17,7 +17,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
-
+
Delete from `foo` table
@@ -29,4 +29,4 @@
INSERT INTO foo VALUES('bar');
-
\ No newline at end of file
+