Skip to content

Commit b5c3b96

Browse files
committed
Improved testing
1 parent 075d136 commit b5c3b96

File tree

8 files changed

+198
-23
lines changed

8 files changed

+198
-23
lines changed

flyway-database-questdb/src/test/java/org/flywaydb/community/database/questdb/QuestDBTest.java

Lines changed: 156 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
package org.flywaydb.community.database.questdb;
2121

2222
import org.flywaydb.core.Flyway;
23-
import org.junit.ClassRule;
23+
import org.flywaydb.core.api.output.MigrateResult;
24+
import org.junit.Rule;
2425
import org.junit.Test;
2526
import org.testcontainers.containers.GenericContainer;
2627
import org.testcontainers.utility.DockerImageName;
@@ -33,37 +34,172 @@ public class QuestDBTest {
3334
private static final DockerImageName QUESTDB_IMAGE = DockerImageName.parse("questdb/questdb:nightly");
3435
private static final int HTTP_PORT = 9000;
3536
private static final int PG_PORT = 8812;
37+
private static final String LOCATION = "classpath:questdb/migration";
38+
private static final String USER = "admin";
39+
private static final String PWD = "quest";
3640

37-
@ClassRule
38-
public static GenericContainer<?> questdb = new GenericContainer<>(QUESTDB_IMAGE)
41+
@Rule
42+
public GenericContainer<?> questdb = new GenericContainer<>(QUESTDB_IMAGE)
3943
.withExposedPorts(HTTP_PORT, PG_PORT)
4044
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()));
41-
45+
46+
@Test
47+
public void testMigration1_CreateTable() throws SQLException {
48+
assertMigration(
49+
"1",
50+
"select table_name, designatedTimestamp, partitionBy,walEnabled from tables()",
51+
"table_name\tdesignatedTimestamp\tpartitionBy\twalEnabled\n" +
52+
"trades\tts\tDAY\tt\n" +
53+
"flyway_schema_history\tinstalled_on\tDAY\tt\n"
54+
);
55+
}
56+
57+
@Test
58+
public void testMigration2_InsertData() throws SQLException {
59+
assertMigration(
60+
"2",
61+
"trades",
62+
"instrument\tside\tqty\tprice\tts\n" +
63+
"SYM1\tBUY\t100.0\t12.56\t2025-05-09 00:01:00.000000\n" +
64+
"SYM2\tBUY\t120.0\t10.11\t2025-05-09 00:02:00.000000\n" +
65+
"SYM1\tSELL\t50.0\t12.44\t2025-05-09 00:03:00.000000\n"
66+
);
67+
}
68+
69+
@Test
70+
public void testMigration3_RenameTable() throws SQLException {
71+
assertMigration(
72+
"3",
73+
"trades_table",
74+
"instrument\tside\tqty\tprice\tts\n" +
75+
"SYM1\tBUY\t100.0\t12.56\t2025-05-09 00:01:00.000000\n" +
76+
"SYM2\tBUY\t120.0\t10.11\t2025-05-09 00:02:00.000000\n" +
77+
"SYM1\tSELL\t50.0\t12.44\t2025-05-09 00:03:00.000000\n"
78+
);
79+
}
80+
81+
@Test
82+
public void testMigration4_AddColumn() throws SQLException {
83+
assertMigration(
84+
"4",
85+
"show create table trades",
86+
"ddl\n" +
87+
"CREATE TABLE 'trades' ( \n" +
88+
"\tinstrument SYMBOL CAPACITY 256 CACHE,\n" +
89+
"\tside SYMBOL CAPACITY 256 CACHE,\n" +
90+
"\tqty DOUBLE,\n" +
91+
"\tprice DOUBLE,\n" +
92+
"\tts TIMESTAMP,\n" +
93+
"\tvenue VARCHAR\n" +
94+
") timestamp(ts) PARTITION BY DAY WAL\n" +
95+
"WITH maxUncommittedRows=500000, o3MaxLag=600000000us;\n"
96+
);
97+
}
98+
99+
@Test
100+
public void testMigration5_AlterColumnType() throws SQLException {
101+
assertMigration(
102+
"5",
103+
"show create table trades",
104+
"ddl\n" +
105+
"CREATE TABLE 'trades' ( \n" +
106+
"\tinstrument SYMBOL CAPACITY 256 CACHE,\n" +
107+
"\tside SYMBOL CAPACITY 256 CACHE,\n" +
108+
"\tqty DOUBLE,\n" +
109+
"\tprice DOUBLE,\n" +
110+
"\tts TIMESTAMP,\n" +
111+
"\tvenue SYMBOL CAPACITY 256 CACHE\n" +
112+
") timestamp(ts) PARTITION BY DAY WAL\n" +
113+
"WITH maxUncommittedRows=500000, o3MaxLag=600000000us;\n"
114+
);
115+
}
116+
117+
@Test
118+
public void testMigration6_DropColumn() throws SQLException {
119+
assertMigration(
120+
"6",
121+
"show create table trades",
122+
"ddl\n" +
123+
"CREATE TABLE 'trades' ( \n" +
124+
"\tinstrument SYMBOL CAPACITY 256 CACHE,\n" +
125+
"\tside SYMBOL CAPACITY 256 CACHE,\n" +
126+
"\tqty DOUBLE,\n" +
127+
"\tprice DOUBLE,\n" +
128+
"\tts TIMESTAMP\n" +
129+
") timestamp(ts) PARTITION BY DAY WAL\n" +
130+
"WITH maxUncommittedRows=500000, o3MaxLag=600000000us;\n"
131+
);
132+
}
133+
42134
@Test
43-
public void testQuestDBContainer() throws SQLException {
135+
public void testMigration7_DropTable() throws SQLException {
136+
assertMigration(
137+
"7",
138+
"select table_name, designatedTimestamp, partitionBy,walEnabled from tables()",
139+
"table_name\tdesignatedTimestamp\tpartitionBy\twalEnabled\n" +
140+
"flyway_schema_history\tinstalled_on\tDAY\tt\n"
141+
);
142+
}
143+
144+
private void assertMigration(String version, String query, String expectedResult) throws SQLException {
145+
final String jdbcUrl = jdbcUrl();
146+
147+
final Flyway flyway = flyway(jdbcUrl, version);
148+
final MigrateResult migrateResult = flyway.migrate();
149+
assertEquals(0, migrateResult.getFailedMigrations().size());
150+
151+
assertQuery(jdbcUrl, query, expectedResult);
152+
}
153+
154+
private String jdbcUrl() {
44155
final String host = questdb.getHost();
45156
final int port = questdb.getMappedPort(PG_PORT);
157+
return "jdbc:postgresql://" + host + ":" + port + "/default";
158+
}
159+
160+
private static Flyway flyway(String jdbcUrl, String version) {
161+
return Flyway
162+
.configure()
163+
.locations(LOCATION)
164+
.dataSource(jdbcUrl, USER, PWD)
165+
.target(version)
166+
.load();
167+
}
46168

47-
try (Connection connection = DriverManager.getConnection(
48-
"jdbc:postgresql://" + host + ":" + port + "/qdb", "admin", "quest"
49-
)) {
169+
private static void assertQuery(String jdbcUrl, String query, String expectedResult) throws SQLException {
170+
try (Connection connection = DriverManager.getConnection(jdbcUrl, USER, PWD)) {
50171
try (Statement statement = connection.createStatement();
51-
ResultSet resultSet = statement.executeQuery("select 1")) {
52-
resultSet.next();
53-
assertEquals(1, resultSet.getInt(1));
172+
ResultSet resultSet = statement.executeQuery(query)) {
173+
assertEquals(expectedResult, resultSetToString(resultSet));
54174
}
55175
}
56176
}
57-
58-
@Test
59-
public void testQuestDBFlyway() {
60-
final String host = questdb.getHost();
61-
final int port = questdb.getMappedPort(PG_PORT);
62177

63-
final Flyway flyway = Flyway.configure().dataSource(
64-
"jdbc:postgresql://" + host + ":" + port + "/qdb", "admin", "quest"
65-
).load();
178+
private static String resultSetToString(ResultSet rs) throws SQLException {
179+
final StringBuilder sb = new StringBuilder();
180+
final ResultSetMetaData metaData = rs.getMetaData();
181+
final int columnCount = metaData.getColumnCount();
182+
183+
// Print column headers
184+
for (int i = 1; i <= columnCount; i++) {
185+
sb.append(metaData.getColumnName(i));
186+
if (i < columnCount) {
187+
sb.append("\t");
188+
}
189+
}
190+
sb.append("\n");
191+
192+
// Print rows
193+
while (rs.next()) {
194+
for (int i = 1; i <= columnCount; i++) {
195+
sb.append(rs.getString(i));
196+
if (i < columnCount) {
197+
sb.append("\t");
198+
}
199+
}
200+
sb.append("\n");
201+
}
66202

67-
flyway.migrate();
203+
return sb.toString();
68204
}
69205
}

flyway-database-questdb/src/test/resources/db/migration/V3__Rename_table.sql renamed to flyway-database-questdb/src/test/resources/questdb/migration/V3__Rename_table.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
-- =========================LICENSE_END==================================
1919
---
2020
RENAME TABLE trades TO trades_table;
21-
RENAME TABLE trades_table TO trades;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
-- ========================LICENSE_START=================================
3+
-- flyway-database-questdb
4+
-- ========================================================================
5+
-- Copyright (C) 2010 - 2025 Red Gate Software Ltd
6+
-- ========================================================================
7+
-- Licensed under the Apache License, Version 2.0 (the "License");
8+
-- you may not use this file except in compliance with the License.
9+
-- You may obtain a copy of the License at
10+
--
11+
-- http://www.apache.org/licenses/LICENSE-2.0
12+
--
13+
-- Unless required by applicable law or agreed to in writing, software
14+
-- distributed under the License is distributed on an "AS IS" BASIS,
15+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
-- See the License for the specific language governing permissions and
17+
-- limitations under the License.
18+
-- =========================LICENSE_END==================================
19+
---
20+
RENAME TABLE trades_table TO trades;
21+
22+
ALTER TABLE trades ADD COLUMN venue VARCHAR;

flyway-database-questdb/src/test/resources/db/migration/V4__Add_drop_column.sql renamed to flyway-database-questdb/src/test/resources/questdb/migration/V5__Alter_column_type.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
-- limitations under the License.
1818
-- =========================LICENSE_END==================================
1919
---
20-
ALTER TABLE trades ADD COLUMN venue VARCHAR;
2120
ALTER TABLE trades ALTER COLUMN venue TYPE SYMBOL;
22-
ALTER TABLE trades DROP COLUMN venue;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
-- ========================LICENSE_START=================================
3+
-- flyway-database-questdb
4+
-- ========================================================================
5+
-- Copyright (C) 2010 - 2025 Red Gate Software Ltd
6+
-- ========================================================================
7+
-- Licensed under the Apache License, Version 2.0 (the "License");
8+
-- you may not use this file except in compliance with the License.
9+
-- You may obtain a copy of the License at
10+
--
11+
-- http://www.apache.org/licenses/LICENSE-2.0
12+
--
13+
-- Unless required by applicable law or agreed to in writing, software
14+
-- distributed under the License is distributed on an "AS IS" BASIS,
15+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
-- See the License for the specific language governing permissions and
17+
-- limitations under the License.
18+
-- =========================LICENSE_END==================================
19+
---
20+
ALTER TABLE trades DROP COLUMN venue;

0 commit comments

Comments
 (0)