Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#973] IT(jdbc-postgresql): Support jdbc-postgresql integration testing. (#974)

Clearvive11fde3940e6b
diff --git a/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java b/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
index 354f0f755b..b0ee4ae218 100644
--- a/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
+++ b/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
@@ -42,6 +42,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -232,6 +233,29 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
}
}
+ @Override
+ public boolean purgeTable(NameIdentifier ident) throws UnsupportedOperationException {
+ NameIdentifier.checkTable(ident);
+
+ Map<String, String> params = new HashMap<>();
+ params.put("purge", "true");
+ try {
+ DropResponse resp =
+ restClient.delete(
+ formatTableRequestPath(ident.namespace()) + "/" + ident.name(),
+ params,
+ DropResponse.class,
+ Collections.emptyMap(),
+ ErrorHandlers.tableErrorHandler());
+ resp.validate();
+ return resp.dropped();
+
+ } catch (Exception e) {
+ LOG.warn("Failed to purge table {}", ident, e);
+ return false;
+ }
+ }
+
/**
* List all the schemas under the given catalog namespace.
*
diff --git a/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java b/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java
index e35f7cc333..6305c629df 100644
--- a/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java
+++ b/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java
@@ -914,6 +914,29 @@ public class TestRelationalCatalog extends TestBase {
Assertions.assertFalse(catalog.asTableCatalog().dropTable(tableId));
}
+ @Test
+ public void testPurgeTable() throws JsonProcessingException {
+ NameIdentifier tableId = NameIdentifier.of(metalakeName, catalogName, "schema1", "table1");
+ String tablePath =
+ withSlash(
+ RelationalCatalog.formatTableRequestPath(tableId.namespace()) + "/" + tableId.name());
+ DropResponse resp = new DropResponse(true);
+ buildMockResource(Method.DELETE, tablePath, null, resp, SC_OK);
+
+ Assertions.assertTrue(catalog.asTableCatalog().purgeTable(tableId));
+
+ // return false
+ resp = new DropResponse(false);
+ buildMockResource(Method.DELETE, tablePath, null, resp, SC_OK);
+ Assertions.assertFalse(catalog.asTableCatalog().purgeTable(tableId));
+
+ // Test with exception
+ ErrorResponse errorResp = ErrorResponse.internalError("internal error");
+ buildMockResource(Method.DELETE, tablePath, null, errorResp, SC_INTERNAL_SERVER_ERROR);
+
+ Assertions.assertFalse(catalog.asTableCatalog().purgeTable(tableId));
+ }
+
private void testAlterTable(NameIdentifier ident, TableUpdateRequest req, TableDTO updatedTable)
throws JsonProcessingException {
String tablePath =
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java
new file mode 100644
index 0000000000..14919138aa
--- /dev/null
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java
@@ -0,0 +1,438 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.integration.test.catalog.jdbc.postgresql;
+
+import com.datastrato.gravitino.Catalog;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
+import com.datastrato.gravitino.client.GravitinoMetaLake;
+import com.datastrato.gravitino.dto.rel.ColumnDTO;
+import com.datastrato.gravitino.dto.rel.SortOrderDTO;
+import com.datastrato.gravitino.dto.rel.partitions.Partitioning;
+import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
+import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
+import com.datastrato.gravitino.integration.test.catalog.jdbc.postgresql.service.PostgreSqlService;
+import com.datastrato.gravitino.integration.test.catalog.jdbc.utils.JdbcDriverDownloader;
+import com.datastrato.gravitino.integration.test.util.AbstractIT;
+import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
+import com.datastrato.gravitino.integration.test.util.ITUtils;
+import com.datastrato.gravitino.rel.Schema;
+import com.datastrato.gravitino.rel.SupportsSchemas;
+import com.datastrato.gravitino.rel.Table;
+import com.datastrato.gravitino.rel.TableCatalog;
+import com.datastrato.gravitino.rel.TableChange;
+import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
+import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
+import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
+import com.datastrato.gravitino.rel.types.Types;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.testcontainers.containers.PostgreSQLContainer;
+
+@Tag("gravitino-docker-it")
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+public class CatalogPostgreSqlIT extends AbstractIT {
+ public static String metalakeName = GravitinoITUtils.genRandomName("postgresql_it_metalake");
+ public static String catalogName = GravitinoITUtils.genRandomName("postgresql_it_catalog");
+ public static String schemaName = GravitinoITUtils.genRandomName("postgresql_it_schema");
+ public static String tableName = GravitinoITUtils.genRandomName("postgresql_it_table");
+ public static String alertTableName = "alert_table_name";
+ public static String table_comment = "table_comment";
+
+ public static String schema_comment = "schema_comment";
+
+ public static String POSTGRESQL_COL_NAME1 = "postgresql_col_name1";
+ public static String POSTGRESQL_COL_NAME2 = "postgresql_col_name2";
+ public static String POSTGRESQL_COL_NAME3 = "postgresql_col_name3";
+ private static final String provider = "jdbc-postgresql";
+ private static final String DOWNLOAD_JDK_URL =
+ "https://jdbc.postgresql.org/download/postgresql-42.7.0.jar";
+
+ private static GravitinoMetaLake metalake;
+
+ private static Catalog catalog;
+
+ private static PostgreSqlService postgreSqlService;
+
+ private static PostgreSQLContainer<?> POSTGRESQL_CONTAINER;
+
+ protected static final String TEST_DB_NAME = GravitinoITUtils.genRandomName("test_db");
+
+ @BeforeAll
+ public static void startup() throws IOException {
+
+ if (!ITUtils.EMBEDDED_TEST_MODE.equals(testMode)) {
+ String gravitinoHome = System.getenv("GRAVITINO_HOME");
+ Path tmpPath = Paths.get(gravitinoHome, "/catalogs/jdbc-postgresql/libs");
+ JdbcDriverDownloader.downloadJdbcDriver(DOWNLOAD_JDK_URL, tmpPath.toString());
+ }
+
+ POSTGRESQL_CONTAINER =
+ new PostgreSQLContainer<>("postgres:9.6.12")
+ .withDatabaseName(TEST_DB_NAME)
+ .withUsername("root")
+ .withPassword("root");
+ POSTGRESQL_CONTAINER.start();
+ postgreSqlService = new PostgreSqlService(POSTGRESQL_CONTAINER);
+ createMetalake();
+ createCatalog();
+ createSchema();
+ }
+
+ @AfterAll
+ public static void stop() {
+ clearTableAndSchema();
+ client.dropMetalake(NameIdentifier.of(metalakeName));
+ postgreSqlService.close();
+ POSTGRESQL_CONTAINER.stop();
+ }
+
+ @AfterEach
+ private void resetSchema() {
+ clearTableAndSchema();
+ createSchema();
+ }
+
+ private static void clearTableAndSchema() {
+ NameIdentifier[] nameIdentifiers =
+ catalog.asTableCatalog().listTables(Namespace.of(metalakeName, catalogName, schemaName));
+ for (NameIdentifier nameIdentifier : nameIdentifiers) {
+ catalog.asTableCatalog().purgeTable(nameIdentifier);
+ }
+ catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);
+ }
+
+ private static void createMetalake() {
+ GravitinoMetaLake[] gravitinoMetaLakes = client.listMetalakes();
+ Assertions.assertEquals(0, gravitinoMetaLakes.length);
+
+ GravitinoMetaLake createdMetalake =
+ client.createMetalake(NameIdentifier.of(metalakeName), "comment", Collections.emptyMap());
+ GravitinoMetaLake loadMetalake = client.loadMetalake(NameIdentifier.of(metalakeName));
+ Assertions.assertEquals(createdMetalake, loadMetalake);
+
+ metalake = loadMetalake;
+ }
+
+ private static void createCatalog() {
+ Map<String, String> catalogProperties = Maps.newHashMap();
+
+ try {
+ String jdbcUrl = POSTGRESQL_CONTAINER.getJdbcUrl();
+ String database = new URI(jdbcUrl.substring(jdbcUrl.lastIndexOf("/") + 1)).getPath();
+ catalogProperties.put(JdbcConfig.JDBC_URL.getKey(), jdbcUrl);
+ catalogProperties.put(JdbcConfig.JDBC_DATABASE.getKey(), database);
+ catalogProperties.put(JdbcConfig.USERNAME.getKey(), POSTGRESQL_CONTAINER.getUsername());
+ catalogProperties.put(JdbcConfig.PASSWORD.getKey(), POSTGRESQL_CONTAINER.getPassword());
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+
+ Catalog createdCatalog =
+ metalake.createCatalog(
+ NameIdentifier.of(metalakeName, catalogName),
+ Catalog.Type.RELATIONAL,
+ provider,
+ "comment",
+ catalogProperties);
+ Catalog loadCatalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));
+ Assertions.assertEquals(createdCatalog, loadCatalog);
+
+ catalog = loadCatalog;
+ }
+
+ private static void createSchema() {
+ NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName, schemaName);
+
+ Schema createdSchema =
+ catalog.asSchemas().createSchema(ident, schema_comment, Collections.EMPTY_MAP);
+ Schema loadSchema = catalog.asSchemas().loadSchema(ident);
+ Assertions.assertEquals(createdSchema.name(), loadSchema.name());
+ Assertions.assertEquals(createdSchema.comment(), loadSchema.comment());
+ }
+
+ private ColumnDTO[] createColumns() {
+ ColumnDTO col1 =
+ new ColumnDTO.Builder()
+ .withName(POSTGRESQL_COL_NAME1)
+ .withDataType(Types.IntegerType.get())
+ .withComment("col_1_comment")
+ .build();
+ ColumnDTO col2 =
+ new ColumnDTO.Builder()
+ .withName(POSTGRESQL_COL_NAME2)
+ .withDataType(Types.DateType.get())
+ .withComment("col_2_comment")
+ .build();
+ ColumnDTO col3 =
+ new ColumnDTO.Builder()
+ .withName(POSTGRESQL_COL_NAME3)
+ .withDataType(Types.StringType.get())
+ .withComment("col_3_comment")
+ .build();
+ return new ColumnDTO[] {col1, col2, col3};
+ }
+
+ private Map<String, String> createProperties() {
+ Map<String, String> properties = Maps.newHashMap();
+ return properties;
+ }
+
+ @Test
+ void testOperationPostgreSqlSchema() {
+ SupportsSchemas schemas = catalog.asSchemas();
+ Namespace namespace = Namespace.of(metalakeName, catalogName);
+ // list schema check.
+ NameIdentifier[] nameIdentifiers = schemas.listSchemas(namespace);
+ Set<String> schemaNames =
+ Arrays.stream(nameIdentifiers).map(NameIdentifier::name).collect(Collectors.toSet());
+ Assertions.assertTrue(schemaNames.contains(schemaName));
+
+ NameIdentifier[] postgreSqlNamespaces = postgreSqlService.listSchemas(namespace);
+ schemaNames =
+ Arrays.stream(postgreSqlNamespaces).map(NameIdentifier::name).collect(Collectors.toSet());
+ Assertions.assertTrue(schemaNames.contains(schemaName));
+
+ // create schema check.
+ String testSchemaName = GravitinoITUtils.genRandomName("test_schema_1");
+ NameIdentifier schemaIdent = NameIdentifier.of(metalakeName, catalogName, testSchemaName);
+ schemas.createSchema(schemaIdent, schema_comment, Collections.emptyMap());
+ nameIdentifiers = schemas.listSchemas(Namespace.of(metalakeName, catalogName));
+ Map<String, NameIdentifier> schemaMap =
+ Arrays.stream(nameIdentifiers).collect(Collectors.toMap(NameIdentifier::name, v -> v));
+ Assertions.assertTrue(schemaMap.containsKey(testSchemaName));
+
+ postgreSqlNamespaces = postgreSqlService.listSchemas(namespace);
+ schemaNames =
+ Arrays.stream(postgreSqlNamespaces).map(NameIdentifier::name).collect(Collectors.toSet());
+ Assertions.assertTrue(schemaNames.contains(testSchemaName));
+
+ Assertions.assertThrows(
+ SchemaAlreadyExistsException.class,
+ () -> schemas.createSchema(schemaIdent, schema_comment, Collections.emptyMap()));
+
+ // drop schema check.
+ schemas.dropSchema(schemaIdent, false);
+ Assertions.assertThrows(NoSuchSchemaException.class, () -> schemas.loadSchema(schemaIdent));
+ Assertions.assertThrows(
+ NoSuchSchemaException.class, () -> postgreSqlService.loadSchema(schemaIdent));
+
+ nameIdentifiers = schemas.listSchemas(Namespace.of(metalakeName, catalogName));
+ schemaMap =
+ Arrays.stream(nameIdentifiers).collect(Collectors.toMap(NameIdentifier::name, v -> v));
+ Assertions.assertFalse(schemaMap.containsKey(testSchemaName));
+ Assertions.assertFalse(
+ schemas.dropSchema(NameIdentifier.of(metalakeName, catalogName, "no-exits"), false));
+ TableCatalog tableCatalog = catalog.asTableCatalog();
+
+ // create failed check.
+ NameIdentifier table =
+ NameIdentifier.of(metalakeName, catalogName, testSchemaName, "test_table");
+ Assertions.assertThrows(
+ NoSuchSchemaException.class,
+ () ->
+ tableCatalog.createTable(
+ table,
+ createColumns(),
+ table_comment,
+ createProperties(),
+ null,
+ Distributions.NONE,
+ null));
+ // drop schema failed check.
+ Assertions.assertFalse(schemas.dropSchema(schemaIdent, true));
+ Assertions.assertFalse(schemas.dropSchema(schemaIdent, false));
+ Assertions.assertFalse(tableCatalog.dropTable(table));
+ postgreSqlNamespaces = postgreSqlService.listSchemas(Namespace.empty());
+ schemaNames =
+ Arrays.stream(postgreSqlNamespaces).map(NameIdentifier::name).collect(Collectors.toSet());
+ Assertions.assertTrue(schemaNames.contains(schemaName));
+ }
+
+ @Test
+ void testCreateAndLoadPostgreSqlTable() {
+ // Create table from Gravitino API
+ ColumnDTO[] columns = createColumns();
+
+ NameIdentifier tableIdentifier =
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);
+ Distribution distribution = Distributions.NONE;
+
+ final SortOrderDTO[] sortOrders = SortOrderDTO.EMPTY_SORT;
+
+ Partitioning[] partitioning = Partitioning.EMPTY_PARTITIONING;
+
+ Map<String, String> properties = createProperties();
+ TableCatalog tableCatalog = catalog.asTableCatalog();
+ Table createdTable =
+ tableCatalog.createTable(
+ tableIdentifier,
+ columns,
+ table_comment,
+ properties,
+ partitioning,
+ distribution,
+ sortOrders);
+ Assertions.assertEquals(createdTable.name(), tableName);
+ Map<String, String> resultProp = createdTable.properties();
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ Assertions.assertTrue(resultProp.containsKey(entry.getKey()));
+ Assertions.assertEquals(entry.getValue(), resultProp.get(entry.getKey()));
+ }
+ Assertions.assertEquals(createdTable.columns().length, columns.length);
+
+ for (int i = 0; i < columns.length; i++) {
+ Assertions.assertEquals(createdTable.columns()[i], columns[i]);
+ }
+
+ Table loadTable = tableCatalog.loadTable(tableIdentifier);
+ Assertions.assertEquals(tableName, loadTable.name());
+ Assertions.assertEquals(table_comment, loadTable.comment());
+ resultProp = loadTable.properties();
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ Assertions.assertTrue(resultProp.containsKey(entry.getKey()));
+ Assertions.assertEquals(entry.getValue(), resultProp.get(entry.getKey()));
+ }
+ Assertions.assertEquals(loadTable.columns().length, columns.length);
+ for (int i = 0; i < columns.length; i++) {
+ Assertions.assertEquals(columns[i], loadTable.columns()[i]);
+ }
+ }
+
+ @Test
+ void testAlterAndDropPostgreSqlTable() {
+ ColumnDTO[] columns = createColumns();
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ columns,
+ table_comment,
+ createProperties());
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> {
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.rename(alertTableName),
+ TableChange.updateComment(table_comment + "_new"));
+ });
+
+ // rename table
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.rename(alertTableName));
+
+ // update table
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName),
+ TableChange.updateComment(table_comment + "_new"),
+ TableChange.addColumn(new String[] {"col_4"}, Types.StringType.get()),
+ TableChange.renameColumn(new String[] {POSTGRESQL_COL_NAME2}, "col_2_new"),
+ TableChange.updateColumnType(
+ new String[] {POSTGRESQL_COL_NAME1}, Types.IntegerType.get()));
+
+ Table table =
+ catalog
+ .asTableCatalog()
+ .loadTable(NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName));
+ Assertions.assertEquals(alertTableName, table.name());
+
+ Assertions.assertEquals(POSTGRESQL_COL_NAME1, table.columns()[0].name());
+ Assertions.assertEquals(Types.IntegerType.get(), table.columns()[0].dataType());
+ Assertions.assertTrue(table.columns()[0].nullable());
+
+ Assertions.assertEquals("col_2_new", table.columns()[1].name());
+ Assertions.assertEquals(Types.DateType.get(), table.columns()[1].dataType());
+ Assertions.assertEquals("col_2_comment", table.columns()[1].comment());
+ Assertions.assertTrue(table.columns()[1].nullable());
+
+ Assertions.assertEquals(POSTGRESQL_COL_NAME3, table.columns()[2].name());
+ Assertions.assertEquals(Types.StringType.get(), table.columns()[2].dataType());
+ Assertions.assertEquals("col_3_comment", table.columns()[2].comment());
+ Assertions.assertTrue(table.columns()[2].nullable());
+
+ Assertions.assertEquals("col_4", table.columns()[3].name());
+ Assertions.assertEquals(Types.StringType.get(), table.columns()[3].dataType());
+ Assertions.assertNull(table.columns()[3].comment());
+ Assertions.assertTrue(table.columns()[3].nullable());
+
+ ColumnDTO col1 =
+ new ColumnDTO.Builder()
+ .withName("name")
+ .withDataType(Types.StringType.get())
+ .withComment("comment")
+ .build();
+ ColumnDTO col2 =
+ new ColumnDTO.Builder()
+ .withName("address")
+ .withDataType(Types.StringType.get())
+ .withComment("comment")
+ .build();
+ ColumnDTO col3 =
+ new ColumnDTO.Builder()
+ .withName("date_of_birth")
+ .withDataType(Types.DateType.get())
+ .withComment("comment")
+ .build();
+ ColumnDTO[] newColumns = new ColumnDTO[] {col1, col2, col3};
+ NameIdentifier tableIdentifier =
+ NameIdentifier.of(
+ metalakeName, catalogName, schemaName, GravitinoITUtils.genRandomName("jdbc_it_table"));
+ catalog
+ .asTableCatalog()
+ .createTable(
+ tableIdentifier,
+ newColumns,
+ table_comment,
+ ImmutableMap.of(),
+ Partitioning.EMPTY_PARTITIONING,
+ Distributions.NONE,
+ new SortOrder[0]);
+
+ // delete column
+ Assertions.assertDoesNotThrow(
+ () ->
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ tableIdentifier,
+ TableChange.deleteColumn(new String[] {col3.name()}, true),
+ TableChange.deleteColumn(new String[] {col2.name()}, true)));
+ Table delColTable = catalog.asTableCatalog().loadTable(tableIdentifier);
+ Assertions.assertEquals(1, delColTable.columns().length);
+ Assertions.assertEquals(col1.name(), delColTable.columns()[0].name());
+
+ Assertions.assertDoesNotThrow(
+ () -> {
+ catalog.asTableCatalog().dropTable(tableIdentifier);
+ });
+ }
+}
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlAbstractIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlAbstractIT.java
index 043b19ca46..5b4f8eed8f 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlAbstractIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/TestPostgreSqlAbstractIT.java
@@ -22,7 +22,7 @@ public class TestPostgreSqlAbstractIT extends TestJdbcAbstractIT {
@BeforeAll
public static void startup() {
CONTAINER =
- new PostgreSQLContainer<>()
+ new PostgreSQLContainer<>("postgres:9.6.12")
.withDatabaseName(TEST_DB_NAME)
.withUsername("root")
.withPassword("root");
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/service/PostgreSqlService.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/service/PostgreSqlService.java
new file mode 100644
index 0000000000..8d7ac95f78
--- /dev/null
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/service/PostgreSqlService.java
@@ -0,0 +1,85 @@
+package com.datastrato.gravitino.integration.test.catalog.jdbc.postgresql.service;
+
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.jdbc.JdbcSchema;
+import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
+import java.net.URI;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.lang3.ArrayUtils;
+import org.testcontainers.containers.PostgreSQLContainer;
+
+public class PostgreSqlService {
+
+ private final Connection connection;
+
+ private final String database;
+
+ public PostgreSqlService(PostgreSQLContainer<?> postgreSQLContainer) {
+ String username = postgreSQLContainer.getUsername();
+ String password = postgreSQLContainer.getPassword();
+
+ try {
+ String jdbcUrl = postgreSQLContainer.getJdbcUrl();
+ String database = new URI(jdbcUrl.substring(jdbcUrl.lastIndexOf("/") + 1)).getPath();
+ this.connection = DriverManager.getConnection(jdbcUrl, username, password);
+ connection.setCatalog(database);
+ this.database = database;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public NameIdentifier[] listSchemas(Namespace namespace) {
+ List<String> databases = new ArrayList<>();
+ try (PreparedStatement statement =
+ connection.prepareStatement(
+ "SELECT schema_name FROM information_schema.schemata WHERE catalog_name = ?")) {
+ statement.setString(1, database);
+ ResultSet resultSet = statement.executeQuery();
+ while (resultSet.next()) {
+ String databaseName = resultSet.getString(1);
+ databases.add(databaseName);
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ return databases.stream()
+ .map(s -> NameIdentifier.of(ArrayUtils.add(namespace.levels(), s)))
+ .toArray(NameIdentifier[]::new);
+ }
+
+ public JdbcSchema loadSchema(NameIdentifier schemaIdent) {
+ String schema = schemaIdent.name();
+ String sql =
+ "SELECT schema_name FROM information_schema.schemata WHERE schema_name = ? AND catalog_name = ?";
+ try (PreparedStatement statement = connection.prepareStatement(sql)) {
+ statement.setString(1, schema);
+ statement.setString(2, database);
+ try (ResultSet resultSet = statement.executeQuery()) {
+ if (!resultSet.next()) {
+ throw new NoSuchSchemaException("No such schema: " + schema);
+ }
+ String schemaName = resultSet.getString(1);
+ return new JdbcSchema.Builder().withName(schemaName).build();
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void close() {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+}