Skip to content

Commit edfbd98

Browse files
malltshiktomix26
authored andcommitted
#32 Support for Liquibase (#97)
* #32 Support for Liquibase Added once more DatabasePreparer implementation - LiquibasePrepare, which support liquibase migrations * #32 Support for Liquibase - Add missed license headers * #32 Support for Liquibase - fix some build problems(versions,imports,etc.)
1 parent d2e9f4e commit edfbd98

File tree

5 files changed

+164
-4
lines changed

5 files changed

+164
-4
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,25 @@ Additionally you may use the [`EmbeddedPostgres`](src/main/java/io/zonky/test/db
4646

4747
Default username/password is: postgres/postgres and the default database is 'postgres'
4848

49-
## Flyway Migrator
50-
51-
You can easily integrate Flyway database schema migration:
49+
## Migrators (Flyway or Liquibase)
5250

51+
You can easily integrate Flyway or Liquibase database schema migration:
52+
##### Flyway
5353
```java
54-
@Rule
54+
@Rule
5555
public PreparedDbRule db =
5656
EmbeddedPostgresRules.preparedDatabase(
5757
FlywayPreparer.forClasspathLocation("db/my-db-schema"));
5858
```
5959

60+
##### Liquibase
61+
```java
62+
@Rule
63+
public PreparedDbRule db =
64+
EmbeddedPostgresRules.preparedDatabase(
65+
LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
66+
```
67+
6068
This will create an independent database for every test with the given schema loaded from the classpath.
6169
Database templates are used so the time cost is relatively small, given the superior isolation truly
6270
independent databases gives you.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@
137137
<version>5.0.7</version>
138138
<optional>true</optional>
139139
</dependency>
140+
<dependency>
141+
<groupId>org.liquibase</groupId>
142+
<artifactId>liquibase-core</artifactId>
143+
<version>3.6.3</version>
144+
<optional>true</optional>
145+
</dependency>
140146
<dependency>
141147
<groupId>org.postgresql</groupId>
142148
<artifactId>postgresql</artifactId>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.embedded;
15+
16+
import liquibase.Contexts;
17+
import liquibase.Liquibase;
18+
import liquibase.database.Database;
19+
import liquibase.database.jvm.JdbcConnection;
20+
import liquibase.exception.LiquibaseException;
21+
import liquibase.resource.ClassLoaderResourceAccessor;
22+
23+
import javax.sql.DataSource;
24+
import java.sql.Connection;
25+
import java.sql.SQLException;
26+
import java.util.Objects;
27+
28+
import static liquibase.database.DatabaseFactory.getInstance;
29+
30+
public final class LiquibasePreparer implements DatabasePreparer {
31+
32+
private final String location;
33+
34+
public static LiquibasePreparer forClasspathLocation(String location) {
35+
return new LiquibasePreparer(location);
36+
}
37+
38+
private LiquibasePreparer(String location) {
39+
this.location = location;
40+
}
41+
42+
@Override
43+
public void prepare(DataSource ds) throws SQLException {
44+
Connection connection = null;
45+
try {
46+
connection = ds.getConnection();
47+
Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
48+
Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database);
49+
liquibase.update(new Contexts());
50+
} catch (LiquibaseException e) {
51+
throw new SQLException(e);
52+
} finally {
53+
if (connection != null) {
54+
connection.rollback();
55+
connection.close();
56+
}
57+
}
58+
}
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
return obj instanceof LiquibasePreparer && Objects.equals(location, ((LiquibasePreparer) obj).location);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hashCode(location);
68+
}
69+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.embedded;
15+
16+
import io.zonky.test.db.postgres.junit.EmbeddedPostgresRules;
17+
import io.zonky.test.db.postgres.junit.PreparedDbRule;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import java.sql.Connection;
22+
import java.sql.ResultSet;
23+
import java.sql.Statement;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
public class LiquibasePreparerTest {
28+
29+
@Rule
30+
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
31+
32+
@Test
33+
public void testTablesMade() throws Exception {
34+
try (Connection c = db.getTestDatabase().getConnection();
35+
Statement s = c.createStatement()) {
36+
ResultSet rs = s.executeQuery("SELECT * FROM foo");
37+
rs.next();
38+
assertEquals("bar", rs.getString(1));
39+
}
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed under the Apache License, Version 2.0 (the "License");
4+
~ you may not use this file except in compliance with the License.
5+
~ You may obtain a copy of the License at
6+
~
7+
~ http://www.apache.org/licenses/LICENSE-2.0
8+
~
9+
~ Unless required by applicable law or agreed to in writing, software
10+
~ distributed under the License is distributed on an "AS IS" BASIS,
11+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
~ See the License for the specific language governing permissions and
13+
~ limitations under the License.
14+
-->
15+
<databaseChangeLog
16+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
19+
20+
<preConditions>
21+
<dbms type="postgresql"/>
22+
</preConditions>
23+
24+
<changeSet id="11122018140410" author="artem.gavrilov">
25+
<comment>Create table `foo`</comment>
26+
<sql>
27+
CREATE TABLE foo (test VARCHAR);
28+
</sql>
29+
</changeSet>
30+
31+
<changeSet id="11122018140810" author="artem.gavrilov">
32+
<comment>Fill `foo` table</comment>
33+
<sql>INSERT INTO foo VALUES('bar');</sql>
34+
</changeSet>
35+
36+
</databaseChangeLog>

0 commit comments

Comments
 (0)