Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TimescaleDB available with JDBC syntax #3891

Merged
merged 17 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/modules/databases/jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database

`jdbc:tc:postgis:9.6-2.5:///databasename`

#### Using TimescaleDB

`jdbc:tc:timescaldb:2.1.0-pg13:///databasename`

#### Using Trino

`jdbc:tc:trino:352://localhost/memory/default`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ private void performTestForJDBCParamUsage(HikariDataSource dataSource) throws SQ
String databaseQuery = "SELECT DATABASE()";
// Postgres does not have Database() as a function
String databaseType = ConnectionUrl.newInstance(jdbcUrl).getDatabaseType();
if (databaseType.equalsIgnoreCase("postgresql") || databaseType.equalsIgnoreCase("postgis")) {
if (databaseType.equalsIgnoreCase("postgresql") ||
databaseType.equalsIgnoreCase("postgis") ||
databaseType.equalsIgnoreCase("timescaledb")) {
databaseQuery = "SELECT CURRENT_DATABASE()";
}
Comment on lines +120 to 124
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty ugly and should be made more generic, but that would be a different PR.


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.testcontainers.containers;

import org.testcontainers.jdbc.ConnectionUrl;
import org.testcontainers.utility.DockerImageName;

/**
* Factory for TimescaleDB containers, which are a special flavour of PostgreSQL.
*
* @see <a href="https://docs.timescale.com/latest/introduction">https://docs.timescale.com/latest/introduction</a>
*/
public class TimescaleDBContainerProvider extends JdbcDatabaseContainerProvider {

private static final String NAME = "timescaledb";
private static final String DEFAULT_TAG = "2.1.0-pg11";
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse("timescale/timescaledb").asCompatibleSubstituteFor("postgres");
public static final String USER_PARAM = "user";
public static final String PASSWORD_PARAM = "password";


@Override
public boolean supports(String databaseType) {
return databaseType.equals(NAME);
}

@Override
public JdbcDatabaseContainer newInstance() {
return newInstance(DEFAULT_TAG);
}

@Override
public JdbcDatabaseContainer newInstance(String tag) {
return new PostgreSQLContainer(DEFAULT_IMAGE.withTag(tag));
}

@Override
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) {
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.testcontainers.containers.PostgreSQLContainerProvider
org.testcontainers.containers.PostgisContainerProvider
org.testcontainers.containers.TimescaleDBContainerProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.testcontainers.containers;

import org.junit.Test;
import org.testcontainers.db.AbstractContainerDatabaseTest;

import java.sql.ResultSet;
import java.sql.SQLException;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotEquals;

public class TimescaleDBContainerTest extends AbstractContainerDatabaseTest {

@Test
public void testSimple() throws SQLException {
try (JdbcDatabaseContainer<?> postgres = new TimescaleDBContainerProvider().newInstance()) {
postgres.start();

ResultSet resultSet = performQuery(postgres, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
}
}

@Test
public void testCommandOverride() throws SQLException {
try (GenericContainer<?> postgres = new TimescaleDBContainerProvider().newInstance().withCommand("postgres -c max_connections=42")) {
postgres.start();

ResultSet resultSet = performQuery((JdbcDatabaseContainer<?>) postgres, "SELECT current_setting('max_connections')");
String result = resultSet.getString(1);
assertEquals("max_connections should be overriden", "42", result);
}
}

@Test
public void testUnsetCommand() throws SQLException {
try (GenericContainer<?> postgres = new TimescaleDBContainerProvider().newInstance().withCommand("postgres -c max_connections=42").withCommand()) {
postgres.start();

ResultSet resultSet = performQuery((JdbcDatabaseContainer<?>) postgres, "SELECT current_setting('max_connections')");
String result = resultSet.getString(1);
assertNotEquals("max_connections should not be overriden", "42", result);
}
}

@Test
public void testExplicitInitScript() throws SQLException {
try (JdbcDatabaseContainer<?> postgres = new TimescaleDBContainerProvider().newInstance().withInitScript("somepath/init_timescaledb.sql")) {
postgres.start();

ResultSet resultSet = performQuery(postgres, "SELECT foo FROM bar");

String firstColumnValue = resultSet.getString(1);
assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.testcontainers.jdbc.timescaledb;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.jdbc.AbstractJDBCDriverTest;

import java.util.EnumSet;

import static java.util.Arrays.asList;

@RunWith(Parameterized.class)
public class TimescaleDBJDBCDriverTest extends AbstractJDBCDriverTest {

@Parameterized.Parameters(name = "{index} - {0}")
public static Iterable<Object[]> data() {
return asList(
new Object[][]{
{"jdbc:tc:timescaledb://hostname/databasename?user=someuser&password=somepwd", EnumSet.of(Options.JDBCParams)},
{"jdbc:tc:timescaledb:2.1.0-pg13://hostname/databasename?user=someuser&password=somepwd", EnumSet.of(Options.JDBCParams)},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Extend the database with TimescaleDB
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

CREATE TABLE bar
(
foo VARCHAR(255),
time TIMESTAMPTZ NOT NULL
);

SELECT create_hypertable('bar', 'time');

INSERT INTO bar (time, foo)
VALUES (CURRENT_TIMESTAMP, 'hello world');