Skip to content

Commit 9e19ab7

Browse files
amumursttomix26
authored andcommitted
Expose contexts for LiquibasePreparer (#106)
1 parent 736c427 commit 9e19ab7

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
public final class LiquibasePreparer implements DatabasePreparer {
3131

3232
private final String location;
33+
private final Contexts contexts;
3334

3435
public static LiquibasePreparer forClasspathLocation(String location) {
35-
return new LiquibasePreparer(location);
36+
return new LiquibasePreparer(location, new Contexts());
37+
}
38+
public static LiquibasePreparer forClasspathLocation(String location, Contexts contexts) {
39+
return new LiquibasePreparer(location, contexts);
3640
}
3741

38-
private LiquibasePreparer(String location) {
42+
private LiquibasePreparer(String location, Contexts contexts) {
3943
this.location = location;
44+
this.contexts = contexts;
4045
}
4146

4247
@Override
@@ -46,7 +51,7 @@ public void prepare(DataSource ds) throws SQLException {
4651
connection = ds.getConnection();
4752
Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
4853
Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database);
49-
liquibase.update(new Contexts());
54+
liquibase.update(contexts);
5055
} catch (LiquibaseException e) {
5156
throw new SQLException(e);
5257
} finally {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 liquibase.Contexts;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
22+
import java.sql.Connection;
23+
import java.sql.ResultSet;
24+
import java.sql.Statement;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
public class LiquibasePreparerContextTest {
29+
30+
@Rule
31+
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test")));
32+
33+
@Test
34+
public void testEmptyTables() throws Exception {
35+
try (Connection c = db.getTestDatabase().getConnection();
36+
Statement s = c.createStatement()) {
37+
ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo");
38+
rs.next();
39+
assertEquals(0, rs.getInt(1));
40+
}
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
<include file="liqui/master.xml"/>
21+
22+
<changeSet id="deleteAll" author="foo.bar" context="test">
23+
<comment>Delete from `foo` table</comment>
24+
<delete tableName="foo"/>
25+
</changeSet>
26+
27+
<changeSet id="reInsertBar" author="foo.bar" context="local">
28+
<comment>Fill `foo` table</comment>
29+
<sql>INSERT INTO foo VALUES('bar');</sql>
30+
</changeSet>
31+
32+
</databaseChangeLog>

0 commit comments

Comments
 (0)