Skip to content

Commit d8bbdb2

Browse files
Replace H2 use for testing with PostgreSQL
1 parent ea7e29b commit d8bbdb2

File tree

9 files changed

+1973
-2194
lines changed

9 files changed

+1973
-2194
lines changed

gateway-ha/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ routingRules:
77
# rulesConfigPath: "src/main/resources/rules/routing_rules.yml"
88

99
dataStore:
10-
jdbcUrl: jdbc:postgresql://localhost:5432/trino_gateway_db
11-
user: trino_gateway_db_admin
12-
password: P0stG&es
10+
jdbcUrl: jdbc:postgresql://${ENV:DB_HOSTNAME:localhost}:${ENV:DB_PORT:5432}/${ENV:DB_NAME:trino_gateway_db}
11+
user: ${ENV:DB_USER:trino_gateway_db_admin}
12+
password: ${ENV:DB_PASSWORD:P0stG&es}
1313
driver: org.postgresql.Driver
1414
queryHistoryHoursRetention: 24
1515

gateway-ha/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,6 @@
322322
</dependency>
323323

324324
<!-- Test deps -->
325-
<dependency>
326-
<groupId>com.h2database</groupId>
327-
<artifactId>h2</artifactId>
328-
<version>1.4.192</version>
329-
<scope>test</scope>
330-
</dependency>
331325

332326
<dependency>
333327
<groupId>com.squareup.okhttp3</groupId>

gateway-ha/src/main/java/io/trino/gateway/ha/persistence/dao/ExactMatchSourceSelectorsDao.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,45 @@
1414
package io.trino.gateway.ha.persistence.dao;
1515

1616
import io.trino.gateway.ha.router.ResourceGroupsManager;
17+
import org.jdbi.v3.core.mapper.ColumnMapper;
18+
import org.jdbi.v3.core.statement.StatementContext;
19+
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
1720
import org.jdbi.v3.sqlobject.customizer.BindBean;
1821
import org.jdbi.v3.sqlobject.statement.SqlQuery;
1922
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
2023

24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.sql.Timestamp;
27+
import java.time.format.DateTimeFormatter;
2128
import java.util.List;
2229

30+
@RegisterColumnMapper(ExactMatchSourceSelectorsDao.TimestampColumnMapper.class)
2331
public interface ExactMatchSourceSelectorsDao
2432
{
33+
class TimestampColumnMapper
34+
implements ColumnMapper<String>
35+
{
36+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
37+
38+
@Override
39+
public String map(ResultSet r, int columnNumber, StatementContext ctx)
40+
throws SQLException
41+
{
42+
String columnName = r.getMetaData().getColumnName(columnNumber);
43+
if ("update_time".equals(columnName)) {
44+
try {
45+
Timestamp timestamp = r.getTimestamp(columnNumber);
46+
return timestamp != null ? timestamp.toLocalDateTime().format(FORMATTER) : null;
47+
}
48+
catch (SQLException e) {
49+
// Handle case when timestamp is invalid
50+
return "2020-07-06 00:00:00";
51+
}
52+
}
53+
return r.getString(columnNumber);
54+
}
55+
}
2556
@SqlQuery("""
2657
SELECT * FROM exact_match_source_selectors
2758
""")
@@ -30,7 +61,7 @@ public interface ExactMatchSourceSelectorsDao
3061
@SqlUpdate("""
3162
INSERT INTO exact_match_source_selectors
3263
(resource_group_id, update_time, source, environment, query_type)
33-
VALUES (:resourceGroupId, :updateTime, :source, :environment, :queryType)
64+
VALUES (:resourceGroupId, CAST(:updateTime AS TIMESTAMP), :source, :environment, :queryType)
3465
""")
3566
void insert(@BindBean ResourceGroupsManager.ExactSelectorsDetail exactSelectors);
3667
}

gateway-ha/src/test/java/io/trino/gateway/ha/HaGatewayTestUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ public static void prepareMockBackend(
7070
.setResponseCode(200));
7171
}
7272

73-
public static void seedRequiredData(String h2DbFilePath)
73+
public static void seedRequiredData(PostgreSQLContainer<?> container)
7474
{
75-
String jdbcUrl = "jdbc:h2:" + h2DbFilePath;
76-
Jdbi jdbi = Jdbi.create(jdbcUrl, "sa", "sa");
75+
Jdbi jdbi = Jdbi.create(container.getJdbcUrl(), container.getUsername(), container.getPassword());
7776
try (Handle handle = jdbi.open()) {
78-
handle.createUpdate(HaGatewayTestUtils.getResourceFileContent("gateway-ha-persistence-mysql.sql"))
77+
handle.createUpdate(HaGatewayTestUtils.getResourceFileContent("gateway-ha-persistence-postgres.sql"))
7978
.execute();
8079
}
8180
}

gateway-ha/src/test/java/io/trino/gateway/ha/TestingJdbcConnectionManager.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@
1818
import org.jdbi.v3.core.Jdbi;
1919
import org.testcontainers.containers.JdbcDatabaseContainer;
2020

21-
import java.io.File;
22-
import java.nio.file.Path;
23-
2421
public final class TestingJdbcConnectionManager
2522
{
2623
private TestingJdbcConnectionManager() {}
2724

2825
public static JdbcConnectionManager createTestingJdbcConnectionManager()
2926
{
30-
File tempH2DbDir = Path.of(System.getProperty("java.io.tmpdir"), "h2db-" + System.currentTimeMillis()).toFile();
31-
tempH2DbDir.deleteOnExit();
32-
String jdbcUrl = "jdbc:h2:" + tempH2DbDir.getAbsolutePath();
33-
HaGatewayTestUtils.seedRequiredData(tempH2DbDir.getAbsolutePath());
34-
DataStoreConfiguration db = new DataStoreConfiguration(jdbcUrl, "sa", "sa", "org.h2.Driver", 4, false);
35-
Jdbi jdbi = Jdbi.create(jdbcUrl, "sa", "sa");
27+
org.testcontainers.containers.PostgreSQLContainer<?> postgres = new org.testcontainers.containers.PostgreSQLContainer<>("postgres:14-alpine");
28+
postgres.start();
29+
30+
String jdbcUrl = postgres.getJdbcUrl();
31+
String username = postgres.getUsername();
32+
String password = postgres.getPassword();
33+
34+
DataStoreConfiguration db = new DataStoreConfiguration(jdbcUrl, username, password, "org.postgresql.Driver", 4, false);
35+
Jdbi jdbi = Jdbi.create(jdbcUrl, username, password);
36+
37+
// Initialize the database with required schema
38+
try (var handle = jdbi.open()) {
39+
handle.createUpdate(HaGatewayTestUtils.getResourceFileContent("gateway-ha-persistence-postgres.sql"))
40+
.execute();
41+
}
42+
3643
return new JdbcConnectionManager(jdbi, db);
3744
}
3845

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@
2424

2525
final class TestJdbcConnectionManager
2626
{
27-
@Test
28-
void testBuildJdbcUrlWithH2AndNoRoutingGroupDatabase()
29-
{
30-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
31-
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:h2:/mydb");
32-
}
33-
34-
@Test
35-
void testBuildJdbcUrlWithH2AndRoutingGroupDatabase()
36-
{
37-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
38-
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:h2:/newdb");
39-
}
40-
4127
@Test
4228
void testBuildJdbcUrlWithMySQLAndNoRoutingGroupDatabase()
4329
{
@@ -108,7 +94,7 @@ void testBuildJdbcUrlWithNullJdbcUrlThrowsException()
10894
DataStoreConfiguration dataStoreConfiguration = Mockito.mock(DataStoreConfiguration.class);
10995
when(dataStoreConfiguration.getJdbcUrl()).thenReturn(null);
11096

111-
JdbcConnectionManager connectionManager = new JdbcConnectionManager(Jdbi.create("jdbc:h2:/mydb", "sa", "sa"), dataStoreConfiguration);
97+
JdbcConnectionManager connectionManager = new JdbcConnectionManager(Jdbi.create("jdbc:postgresql://localhost:5432/mydb", "postgres", "postgres"), dataStoreConfiguration);
11298
assertThatThrownBy(() -> connectionManager.buildJdbcUrl(null))
11399
.isInstanceOf(IllegalArgumentException.class)
114100
.hasMessage("JDBC URL cannot be null");
@@ -117,10 +103,10 @@ void testBuildJdbcUrlWithNullJdbcUrlThrowsException()
117103
@Test
118104
void testBuildJdbcUrlWithNoSlashThrowsException()
119105
{
120-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:mem:test");
106+
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:postgresql:mydb");
121107
assertThatThrownBy(() -> connectionManager.buildJdbcUrl("newdb"))
122108
.isInstanceOf(IllegalArgumentException.class)
123-
.hasMessage("Invalid JDBC URL: no '/' found in jdbc:h2:mem:test");
109+
.hasMessage("Invalid JDBC URL: no '/' found in jdbc:postgresql:mydb");
124110
}
125111

126112
private static JdbcConnectionManager createConnectionManager(String jdbcUrl)

0 commit comments

Comments
 (0)