Search in sources :

Example 1 with PostgresConnection

use of io.debezium.connector.postgresql.connection.PostgresConnection in project debezium by debezium.

the class TestHelper method execute.

/**
 * Executes a JDBC statement using the default jdbc config without autocommitting the connection
 *
 * @param statement an array of statement
 */
public static void execute(String statement) {
    try (PostgresConnection connection = create()) {
        connection.setAutoCommit(false);
        connection.executeWithoutCommitting(statement);
        Connection jdbcConn = connection.connection();
        if (!statement.endsWith("ROLLBACK;")) {
            jdbcConn.commit();
        } else {
            jdbcConn.rollback();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReplicationConnection(io.debezium.connector.postgresql.connection.ReplicationConnection) Connection(java.sql.Connection) PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) SQLException(java.sql.SQLException)

Example 2 with PostgresConnection

use of io.debezium.connector.postgresql.connection.PostgresConnection in project debezium by debezium.

the class TestHelper method executeDDL.

protected static void executeDDL(String ddlFile) throws Exception {
    URL ddlTestFile = TestHelper.class.getClassLoader().getResource(ddlFile);
    assertNotNull("Cannot locate " + ddlFile, ddlTestFile);
    String statements = Files.readAllLines(Paths.get(ddlTestFile.toURI())).stream().collect(Collectors.joining(System.lineSeparator()));
    try (PostgresConnection connection = create()) {
        connection.executeWithoutCommitting(statements);
    }
}
Also used : PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) URL(java.net.URL)

Example 3 with PostgresConnection

use of io.debezium.connector.postgresql.connection.PostgresConnection in project debezium by debezium.

the class PostgresSchemaIT method shouldApplyFilters.

@Test
public void shouldApplyFilters() throws Exception {
    String statements = "CREATE SCHEMA s1; " + "CREATE SCHEMA s2; " + "DROP TABLE IF EXISTS s1.A;" + "DROP TABLE IF EXISTS s1.B;" + "DROP TABLE IF EXISTS s2.A;" + "DROP TABLE IF EXISTS s2.B;" + "CREATE TABLE s1.A (pk SERIAL, aa integer, PRIMARY KEY(pk));" + "CREATE TABLE s1.B (pk SERIAL, ba integer, PRIMARY KEY(pk));" + "CREATE TABLE s2.A (pk SERIAL, aa integer, PRIMARY KEY(pk));" + "CREATE TABLE s2.B (pk SERIAL, ba integer, PRIMARY KEY(pk));";
    TestHelper.execute(statements);
    PostgresConnectorConfig config = new PostgresConnectorConfig(TestHelper.defaultConfig().with(SCHEMA_BLACKLIST, "s1").build());
    final TypeRegistry typeRegistry = TestHelper.getTypeRegistry();
    schema = new PostgresSchema(config, typeRegistry, TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesIncluded("s2.a", "s2.b");
        assertTablesExcluded("s1.a", "s1.b");
    }
    config = new PostgresConnectorConfig(TestHelper.defaultConfig().with(SCHEMA_BLACKLIST, "s.*").build());
    schema = new PostgresSchema(config, typeRegistry, TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesExcluded("s1.a", "s2.a", "s1.b", "s2.b");
    }
    config = new PostgresConnectorConfig(TestHelper.defaultConfig().with(PostgresConnectorConfig.TABLE_BLACKLIST, "s1.A,s2.A").build());
    schema = new PostgresSchema(config, typeRegistry, TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesIncluded("s1.b", "s2.b");
        assertTablesExcluded("s1.a", "s2.a");
    }
    config = new PostgresConnectorConfig(TestHelper.defaultConfig().with(SCHEMA_BLACKLIST, "s2").with(PostgresConnectorConfig.TABLE_BLACKLIST, "s1.A").build());
    schema = new PostgresSchema(config, typeRegistry, TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesIncluded("s1.b");
        assertTablesExcluded("s1.a", "s2.a", "s2.b");
    }
    config = new PostgresConnectorConfig(TestHelper.defaultConfig().with(PostgresConnectorConfig.COLUMN_BLACKLIST, ".*aa").build());
    schema = new PostgresSchema(config, typeRegistry, TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertColumnsExcluded("s1.a.aa", "s2.a.aa");
    }
}
Also used : PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) Test(org.junit.Test)

Example 4 with PostgresConnection

use of io.debezium.connector.postgresql.connection.PostgresConnection in project debezium by debezium.

the class PostgresSchemaIT method shouldLoadSchemaForPostgisTypes.

@Test
public void shouldLoadSchemaForPostgisTypes() throws Exception {
    TestHelper.executeDDL("init_postgis.ddl");
    TestHelper.executeDDL("postgis_create_tables.ddl");
    PostgresConnectorConfig config = new PostgresConnectorConfig(TestHelper.defaultConfig().build());
    schema = new PostgresSchema(config, TestHelper.getTypeRegistry(), TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        final String[] testTables = new String[] { "public.postgis_table" };
        assertTablesIncluded(testTables);
        Arrays.stream(testTables).forEach(tableId -> assertKeySchema(tableId, "pk", Schema.INT32_SCHEMA));
        assertTableSchema("public.postgis_table", "p, ml", Geometry.builder().optional().build(), Geography.builder().optional().build());
    }
}
Also used : PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) Test(org.junit.Test)

Example 5 with PostgresConnection

use of io.debezium.connector.postgresql.connection.PostgresConnection in project debezium by debezium.

the class PostgresSchemaIT method shouldDetectNewChangesAfterRefreshing.

@Test
public void shouldDetectNewChangesAfterRefreshing() throws Exception {
    String statements = "CREATE SCHEMA IF NOT EXISTS public;" + "DROP TABLE IF EXISTS table1;" + "CREATE TABLE table1 (pk SERIAL,  PRIMARY KEY(pk));";
    TestHelper.execute(statements);
    PostgresConnectorConfig config = new PostgresConnectorConfig(TestHelper.defaultConfig().build());
    schema = new PostgresSchema(config, TestHelper.getTypeRegistry(), TopicSelector.create(config));
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesIncluded("public.table1");
    }
    statements = "DROP TABLE IF EXISTS table1;" + "DROP TABLE IF EXISTS table2;" + "CREATE TABLE table2 (pk SERIAL, strcol VARCHAR, PRIMARY KEY(pk));";
    TestHelper.execute(statements);
    String tableId = "public.table2";
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, false);
        assertTablesIncluded(tableId);
        assertTablesExcluded("public.table1");
        assertTableSchema(tableId, "strcol", Schema.OPTIONAL_STRING_SCHEMA);
    }
    statements = "ALTER TABLE table2 ADD COLUMN vc VARCHAR(2);" + "ALTER TABLE table2 ADD COLUMN si SMALLINT;" + "ALTER TABLE table2 DROP COLUMN strcol;";
    TestHelper.execute(statements);
    try (PostgresConnection connection = TestHelper.create()) {
        schema.refresh(connection, TableId.parse(tableId, false));
        assertTablesIncluded(tableId);
        assertTablesExcluded("public.table1");
        assertTableSchema(tableId, "vc, si", Schema.OPTIONAL_STRING_SCHEMA, Schema.OPTIONAL_INT16_SCHEMA);
        assertColumnsExcluded(tableId + ".strcol");
    }
}
Also used : PostgresConnection(io.debezium.connector.postgresql.connection.PostgresConnection) Test(org.junit.Test)

Aggregations

PostgresConnection (io.debezium.connector.postgresql.connection.PostgresConnection)12 Test (org.junit.Test)5 SQLException (java.sql.SQLException)4 ReplicationConnection (io.debezium.connector.postgresql.connection.ReplicationConnection)2 TableSchema (io.debezium.relational.TableSchema)2 Connection (java.sql.Connection)2 ConnectException (org.apache.kafka.connect.errors.ConnectException)2 Table (io.debezium.relational.Table)1 TableId (io.debezium.relational.TableId)1 LoggingContext (io.debezium.util.LoggingContext)1 URL (java.net.URL)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Config (org.apache.kafka.common.config.Config)1 ConfigValue (org.apache.kafka.common.config.ConfigValue)1 SourceRecord (org.apache.kafka.connect.source.SourceRecord)1