Skip to content

Commit 1c8ce25

Browse files
ebyhroneonestar
authored andcommitted
Simplify database migration tests
1 parent 99677f7 commit 1c8ce25

File tree

3 files changed

+31
-62
lines changed

3 files changed

+31
-62
lines changed

gateway-ha/src/test/java/io/trino/gateway/ha/persistence/BaseTestDatabaseMigrations.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.util.List;
2727

28+
import static java.lang.String.format;
29+
import static java.util.Objects.requireNonNull;
2830
import static org.assertj.core.api.Assertions.assertThat;
2931
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
3032
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
@@ -34,14 +36,19 @@
3436
@Isolated
3537
public abstract class BaseTestDatabaseMigrations
3638
{
37-
protected final JdbcDatabaseContainer<?> container = startContainer();
38-
protected final Jdbi jdbi = Jdbi.create(container.getJdbcUrl(), container.getUsername(), container.getPassword());
39-
40-
protected abstract JdbcDatabaseContainer<?> startContainer();
39+
protected abstract void createGatewaySchema();
4140

42-
protected abstract String getDriver();
41+
private final JdbcDatabaseContainer<?> container;
42+
private final String schema;
43+
protected final Jdbi jdbi;
4344

44-
protected abstract void createGatewaySchema();
45+
public BaseTestDatabaseMigrations(JdbcDatabaseContainer<?> container, String schema)
46+
{
47+
this.container = requireNonNull(container, "container is null");
48+
this.container.start();
49+
this.schema = requireNonNull(schema, "schema is null");
50+
jdbi = Jdbi.create(container.getJdbcUrl(), container.getUsername(), container.getPassword());
51+
}
4552

4653
@AfterAll
4754
public final void close()
@@ -52,13 +59,7 @@ public final void close()
5259
@Test
5360
public void testMigrationWithEmptyDatabase()
5461
{
55-
DataStoreConfiguration config = new DataStoreConfiguration(
56-
container.getJdbcUrl(),
57-
container.getUsername(),
58-
container.getPassword(),
59-
getDriver(),
60-
4,
61-
true);
62+
DataStoreConfiguration config = dataStoreConfiguration();
6263
FlywayMigration.migrate(config);
6364
verifyGatewaySchema(0);
6465

@@ -68,13 +69,7 @@ public void testMigrationWithEmptyDatabase()
6869
@Test
6970
public void testMigrationWithNonemptyDatabase()
7071
{
71-
DataStoreConfiguration config = new DataStoreConfiguration(
72-
container.getJdbcUrl(),
73-
container.getUsername(),
74-
container.getPassword(),
75-
getDriver(),
76-
4,
77-
true);
72+
DataStoreConfiguration config = dataStoreConfiguration();
7873
String t1Create = "CREATE TABLE t1 (id INT)";
7974
String t2Create = "CREATE TABLE t2 (id INT)";
8075
Handle jdbiHandle = jdbi.open();
@@ -98,13 +93,7 @@ public void testMigrationWithExistingGatewaySchema()
9893
// add a row to one of the existing tables before migration
9994
jdbi.withHandle(handle ->
10095
handle.execute("INSERT INTO resource_groups_global_properties VALUES ('a_name', 'a_value')"));
101-
DataStoreConfiguration config = new DataStoreConfiguration(
102-
container.getJdbcUrl(),
103-
container.getUsername(),
104-
container.getPassword(),
105-
getDriver(),
106-
4,
107-
true);
96+
DataStoreConfiguration config = dataStoreConfiguration();
10897
FlywayMigration.migrate(config);
10998
verifyGatewaySchema(1);
11099
dropAllTables();
@@ -137,7 +126,7 @@ protected void dropAllTables()
137126
String exactMatchTable = "DROP TABLE IF EXISTS exact_match_source_selectors";
138127
String flywayHistoryTable = "DROP TABLE IF EXISTS flyway_schema_history";
139128
Handle jdbiHandle = jdbi.open();
140-
String sql = String.format("SELECT 1 FROM information_schema.tables WHERE table_schema = '%s'", getTestSchema());
129+
String sql = format("SELECT 1 FROM information_schema.tables WHERE table_schema = '%s'", schema);
141130
verifyResultSetCount(sql, 7);
142131
jdbiHandle.execute(gatewayBackendTable);
143132
jdbiHandle.execute(queryHistoryTable);
@@ -150,8 +139,14 @@ protected void dropAllTables()
150139
jdbiHandle.close();
151140
}
152141

153-
protected String getTestSchema()
142+
private DataStoreConfiguration dataStoreConfiguration()
154143
{
155-
return "public";
144+
return new DataStoreConfiguration(
145+
container.getJdbcUrl(),
146+
container.getUsername(),
147+
container.getPassword(),
148+
container.getDriverClassName(),
149+
4,
150+
true);
156151
}
157152
}

gateway-ha/src/test/java/io/trino/gateway/ha/persistence/TestDatabaseMigrationsMySql.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,14 @@
1414
package io.trino.gateway.ha.persistence;
1515

1616
import org.jdbi.v3.core.Handle;
17-
import org.testcontainers.containers.JdbcDatabaseContainer;
1817
import org.testcontainers.containers.MySQLContainer;
1918

20-
public class TestDatabaseMigrationsMySql
19+
final class TestDatabaseMigrationsMySql
2120
extends BaseTestDatabaseMigrations
2221
{
23-
@Override
24-
protected final JdbcDatabaseContainer<?> startContainer()
25-
{
26-
JdbcDatabaseContainer<?> container = new MySQLContainer<>("mysql:8.0.36");
27-
container.start();
28-
return container;
29-
}
30-
31-
@Override
32-
protected final String getDriver()
33-
{
34-
return "com.mysql.cj.jdbc.Driver";
35-
}
36-
37-
@Override
38-
protected final String getTestSchema()
22+
public TestDatabaseMigrationsMySql()
3923
{
40-
return "test";
24+
super(new MySQLContainer<>("mysql:8.0.36"), "test");
4125
}
4226

4327
@Override
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,14 @@
1414
package io.trino.gateway.ha.persistence;
1515

1616
import org.jdbi.v3.core.Handle;
17-
import org.testcontainers.containers.JdbcDatabaseContainer;
1817
import org.testcontainers.containers.PostgreSQLContainer;
1918

20-
public class TestDatabaseMigrationsPostgresql
19+
final class TestDatabaseMigrationsPostgreSql
2120
extends BaseTestDatabaseMigrations
2221
{
23-
@Override
24-
protected final JdbcDatabaseContainer<?> startContainer()
25-
{
26-
JdbcDatabaseContainer<?> container = new PostgreSQLContainer<>("postgres:11");
27-
container.start();
28-
return container;
29-
}
30-
31-
@Override
32-
protected final String getDriver()
22+
public TestDatabaseMigrationsPostgreSql()
3323
{
34-
return "org.postgresql.Driver";
24+
super(new PostgreSQLContainer<>("postgres:11"), "public");
3525
}
3626

3727
@Override

0 commit comments

Comments
 (0)