Search in sources :

Example 41 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MySqlDdlParserTest method parseDdlForDecAndFixed.

@Test
public void parseDdlForDecAndFixed() {
    String ddl = "CREATE TABLE t ( c1 DEC(2) NOT NULL, c2 FIXED(1,0) NOT NULL);";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, null, "t"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("c1", "c2");
    assertThat(t.primaryKeyColumnNames()).isEmpty();
    assertColumn(t, "c1", "DEC", Types.DECIMAL, 2, 0, false, false, false);
    assertColumn(t, "c2", "FIXED", Types.DECIMAL, 1, 0, false, false, false);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 42 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MySqlDdlParserTest method shouldParseCreateTableStatementWithCharacterSetForTable.

@Test
public void shouldParseCreateTableStatementWithCharacterSetForTable() {
    String ddl = "CREATE TABLE t ( col1 VARCHAR(25) ) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; ";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, null, "t"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("col1");
    assertThat(t.primaryKeyColumnNames()).isEmpty();
    assertColumn(t, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false);
    ddl = "CREATE TABLE t2 ( col1 VARCHAR(25) ) DEFAULT CHARSET utf8 DEFAULT COLLATE utf8_general_ci; ";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(2);
    Table t2 = tables.forTable(new TableId(null, null, "t2"));
    assertThat(t2).isNotNull();
    assertThat(t2.columnNames()).containsExactly("col1");
    assertThat(t2.primaryKeyColumnNames()).isEmpty();
    assertColumn(t2, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false);
    ddl = "CREATE TABLE t3 ( col1 VARCHAR(25) ) CHARACTER SET utf8 COLLATE utf8_general_ci; ";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(3);
    Table t3 = tables.forTable(new TableId(null, null, "t3"));
    assertThat(t3).isNotNull();
    assertThat(t3.columnNames()).containsExactly("col1");
    assertThat(t3.primaryKeyColumnNames()).isEmpty();
    assertColumn(t3, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false);
    ddl = "CREATE TABLE t4 ( col1 VARCHAR(25) ) CHARSET utf8 COLLATE utf8_general_ci; ";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(4);
    Table t4 = tables.forTable(new TableId(null, null, "t4"));
    assertThat(t4).isNotNull();
    assertThat(t4.columnNames()).containsExactly("col1");
    assertThat(t4.primaryKeyColumnNames()).isEmpty();
    assertColumn(t4, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 43 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MySqlDdlParserTest method parseTableWithPageChecksum.

@Test
public void parseTableWithPageChecksum() {
    String ddl = "CREATE TABLE t (id INT NOT NULL, PRIMARY KEY (`id`)) PAGE_CHECKSUM=1;" + "ALTER TABLE t PAGE_CHECKSUM=0;";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, null, "t"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("id");
    assertThat(t.primaryKeyColumnNames()).hasSize(1);
    assertColumn(t, "id", "INT", Types.INTEGER, -1, -1, false, false, false);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 44 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MySqlDdlParserTest method shouldParseCreateTableStatementWithSignedTypes.

@Test
public void shouldParseCreateTableStatementWithSignedTypes() {
    String ddl = "CREATE TABLE foo ( " + System.lineSeparator() + " c1 BIGINT SIGNED NOT NULL, " + System.lineSeparator() + " c2 INT UNSIGNED NOT NULL " + System.lineSeparator() + "); " + System.lineSeparator();
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table foo = tables.forTable(new TableId(null, null, "foo"));
    assertThat(foo).isNotNull();
    assertThat(foo.columnNames()).containsExactly("c1", "c2");
    assertThat(foo.primaryKeyColumnNames()).isEmpty();
    assertColumn(foo, "c1", "BIGINT SIGNED", Types.BIGINT, -1, -1, false, false, false);
    assertColumn(foo, "c2", "INT UNSIGNED", Types.INTEGER, -1, -1, false, false, false);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 45 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MetadataIT method shouldLoadMetadataViaJdbc.

/**
 * Loads the {@link Tables} definition by reading JDBC metadata. Note that some characteristics, such as whether columns
 * are generated, are not exposed through JDBC (unlike when reading DDL).
 * @throws SQLException if there's an error
 */
@Test
public void shouldLoadMetadataViaJdbc() throws SQLException {
    final UniqueDatabase DATABASE = new UniqueDatabase("readbinlog_it", "readbinlog_test");
    DATABASE.createAndInitialize();
    try (MySQLConnection conn = MySQLConnection.forTestDatabase(DATABASE.getDatabaseName())) {
        conn.connect();
        // Set up the table as one transaction and wait to see the events ...
        conn.execute("DROP TABLE IF EXISTS person", "DROP TABLE IF EXISTS product", "DROP TABLE IF EXISTS purchased");
        conn.execute("CREATE TABLE person (" + "  name VARCHAR(255) primary key," + "  birthdate DATE NULL," + "  age INTEGER NULL DEFAULT 10," + "  salary DECIMAL(5,2)," + "  bitStr BIT(18)" + ")");
        conn.execute("SELECT * FROM person");
        Tables tables = new Tables();
        conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true);
        // System.out.println(tables);
        assertThat(tables.size()).isEqualTo(1);
        Table person = tables.forTable(DATABASE.getDatabaseName(), null, "person");
        assertThat(person).isNotNull();
        assertThat(person.filterColumns(col -> col.isAutoIncremented())).isEmpty();
        assertThat(person.primaryKeyColumnNames()).containsOnly("name");
        assertThat(person.columnNames()).containsExactly("name", "birthdate", "age", "salary", "bitStr");
        assertThat(person.columnWithName("name").name()).isEqualTo("name");
        assertThat(person.columnWithName("name").typeName()).isEqualTo("VARCHAR");
        assertThat(person.columnWithName("name").jdbcType()).isEqualTo(Types.VARCHAR);
        assertThat(person.columnWithName("name").length()).isEqualTo(255);
        assertThat(person.columnWithName("name").scale()).isEqualTo(0);
        assertThat(person.columnWithName("name").position()).isEqualTo(1);
        assertThat(person.columnWithName("name").isAutoIncremented()).isFalse();
        assertThat(person.columnWithName("name").isGenerated()).isFalse();
        assertThat(person.columnWithName("name").isOptional()).isFalse();
        assertThat(person.columnWithName("birthdate").name()).isEqualTo("birthdate");
        assertThat(person.columnWithName("birthdate").typeName()).isEqualTo("DATE");
        assertThat(person.columnWithName("birthdate").jdbcType()).isEqualTo(Types.DATE);
        assertThat(person.columnWithName("birthdate").length()).isEqualTo(10);
        assertThat(person.columnWithName("birthdate").scale()).isEqualTo(0);
        assertThat(person.columnWithName("birthdate").position()).isEqualTo(2);
        assertThat(person.columnWithName("birthdate").isAutoIncremented()).isFalse();
        assertThat(person.columnWithName("birthdate").isGenerated()).isFalse();
        assertThat(person.columnWithName("birthdate").isOptional()).isTrue();
        assertThat(person.columnWithName("age").name()).isEqualTo("age");
        assertThat(person.columnWithName("age").typeName()).isEqualTo("INT");
        assertThat(person.columnWithName("age").jdbcType()).isEqualTo(Types.INTEGER);
        assertThat(person.columnWithName("age").length()).isEqualTo(10);
        assertThat(person.columnWithName("age").scale()).isEqualTo(0);
        assertThat(person.columnWithName("age").position()).isEqualTo(3);
        assertThat(person.columnWithName("age").isAutoIncremented()).isFalse();
        assertThat(person.columnWithName("age").isGenerated()).isFalse();
        assertThat(person.columnWithName("age").isOptional()).isTrue();
        assertThat(person.columnWithName("salary").name()).isEqualTo("salary");
        assertThat(person.columnWithName("salary").typeName()).isEqualTo("DECIMAL");
        assertThat(person.columnWithName("salary").jdbcType()).isEqualTo(Types.DECIMAL);
        assertThat(person.columnWithName("salary").length()).isEqualTo(5);
        assertThat(person.columnWithName("salary").scale()).isEqualTo(2);
        assertThat(person.columnWithName("salary").position()).isEqualTo(4);
        assertThat(person.columnWithName("salary").isAutoIncremented()).isFalse();
        assertThat(person.columnWithName("salary").isGenerated()).isFalse();
        assertThat(person.columnWithName("salary").isOptional()).isTrue();
        assertThat(person.columnWithName("bitStr").name()).isEqualTo("bitStr");
        assertThat(person.columnWithName("bitStr").typeName()).isEqualTo("BIT");
        assertThat(person.columnWithName("bitStr").jdbcType()).isEqualTo(Types.BIT);
        assertThat(person.columnWithName("bitStr").length()).isEqualTo(18);
        assertThat(person.columnWithName("bitStr").scale()).isEqualTo(0);
        assertThat(person.columnWithName("bitStr").position()).isEqualTo(5);
        assertThat(person.columnWithName("bitStr").isAutoIncremented()).isFalse();
        assertThat(person.columnWithName("bitStr").isGenerated()).isFalse();
        assertThat(person.columnWithName("bitStr").isOptional()).isTrue();
        conn.execute("CREATE TABLE product (" + "  id INT NOT NULL AUTO_INCREMENT," + "  createdByDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP," + "  modifiedDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," + "  PRIMARY KEY(id)" + ")");
        conn.execute("SELECT * FROM product");
        tables = new Tables();
        conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true);
        // System.out.println(tables);
        assertThat(tables.size()).isEqualTo(2);
        Table product = tables.forTable(DATABASE.getDatabaseName(), null, "product");
        assertThat(product).isNotNull();
        assertThat(product.filterColumnNames(Column::isAutoIncremented)).containsOnly("id");
        assertThat(product.primaryKeyColumnNames()).containsOnly("id");
        assertThat(product.columnNames()).containsExactly("id", "createdByDate", "modifiedDate");
        assertThat(product.columnWithName("id").name()).isEqualTo("id");
        assertThat(product.columnWithName("id").typeName()).isEqualTo("INT");
        assertThat(product.columnWithName("id").jdbcType()).isEqualTo(Types.INTEGER);
        assertThat(product.columnWithName("id").length()).isEqualTo(10);
        assertThat(product.columnWithName("id").scale()).isEqualTo(0);
        assertThat(product.columnWithName("id").position()).isEqualTo(1);
        assertThat(product.columnWithName("id").isAutoIncremented()).isTrue();
        assertThat(product.columnWithName("id").isGenerated()).isFalse();
        assertThat(product.columnWithName("id").isOptional()).isFalse();
        assertThat(product.columnWithName("createdByDate").name()).isEqualTo("createdByDate");
        assertThat(product.columnWithName("createdByDate").typeName()).isEqualTo("DATETIME");
        assertThat(product.columnWithName("createdByDate").jdbcType()).isEqualTo(Types.TIMESTAMP);
        assertThat(product.columnWithName("createdByDate").length()).isEqualTo(19);
        assertThat(product.columnWithName("createdByDate").scale()).isEqualTo(0);
        assertThat(product.columnWithName("createdByDate").position()).isEqualTo(2);
        assertThat(product.columnWithName("createdByDate").isAutoIncremented()).isFalse();
        assertThat(product.columnWithName("createdByDate").isGenerated()).isFalse();
        assertThat(product.columnWithName("createdByDate").isOptional()).isFalse();
        assertThat(product.columnWithName("modifiedDate").name()).isEqualTo("modifiedDate");
        assertThat(product.columnWithName("modifiedDate").typeName()).isEqualTo("DATETIME");
        assertThat(product.columnWithName("modifiedDate").jdbcType()).isEqualTo(Types.TIMESTAMP);
        assertThat(product.columnWithName("modifiedDate").length()).isEqualTo(19);
        assertThat(product.columnWithName("modifiedDate").scale()).isEqualTo(0);
        assertThat(product.columnWithName("modifiedDate").position()).isEqualTo(3);
        assertThat(product.columnWithName("modifiedDate").isAutoIncremented()).isFalse();
        assertThat(product.columnWithName("modifiedDate").isGenerated()).isFalse();
        assertThat(product.columnWithName("modifiedDate").isOptional()).isFalse();
        conn.execute("CREATE TABLE purchased (" + "  purchaser VARCHAR(255) NOT NULL," + "  productId INT NOT NULL," + "  purchaseDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP," + "  PRIMARY KEY(productId,purchaser)" + ")");
        conn.execute("SELECT * FROM purchased");
        tables = new Tables();
        conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true);
        // System.out.println(tables);
        assertThat(tables.size()).isEqualTo(3);
        Table purchased = tables.forTable(DATABASE.getDatabaseName(), null, "purchased");
        assertThat(purchased).isNotNull();
        assertThat(person.filterColumns(col -> col.isAutoIncremented())).isEmpty();
        assertThat(purchased.primaryKeyColumnNames()).containsOnly("productId", "purchaser");
        assertThat(purchased.columnNames()).containsExactly("purchaser", "productId", "purchaseDate");
        assertThat(purchased.columnWithName("purchaser").name()).isEqualTo("purchaser");
        assertThat(purchased.columnWithName("purchaser").typeName()).isEqualTo("VARCHAR");
        assertThat(purchased.columnWithName("purchaser").jdbcType()).isEqualTo(Types.VARCHAR);
        assertThat(purchased.columnWithName("purchaser").length()).isEqualTo(255);
        assertThat(purchased.columnWithName("purchaser").scale()).isEqualTo(0);
        assertThat(purchased.columnWithName("purchaser").position()).isEqualTo(1);
        assertThat(purchased.columnWithName("purchaser").isAutoIncremented()).isFalse();
        assertThat(purchased.columnWithName("purchaser").isGenerated()).isFalse();
        assertThat(purchased.columnWithName("purchaser").isOptional()).isFalse();
        assertThat(purchased.columnWithName("productId").name()).isEqualTo("productId");
        assertThat(purchased.columnWithName("productId").typeName()).isEqualTo("INT");
        assertThat(purchased.columnWithName("productId").jdbcType()).isEqualTo(Types.INTEGER);
        assertThat(purchased.columnWithName("productId").length()).isEqualTo(10);
        assertThat(purchased.columnWithName("productId").scale()).isEqualTo(0);
        assertThat(purchased.columnWithName("productId").position()).isEqualTo(2);
        assertThat(purchased.columnWithName("productId").isAutoIncremented()).isFalse();
        assertThat(purchased.columnWithName("productId").isGenerated()).isFalse();
        assertThat(purchased.columnWithName("productId").isOptional()).isFalse();
        assertThat(purchased.columnWithName("purchaseDate").name()).isEqualTo("purchaseDate");
        assertThat(purchased.columnWithName("purchaseDate").typeName()).isEqualTo("DATETIME");
        assertThat(purchased.columnWithName("purchaseDate").jdbcType()).isEqualTo(Types.TIMESTAMP);
        assertThat(purchased.columnWithName("purchaseDate").length()).isEqualTo(19);
        assertThat(purchased.columnWithName("purchaseDate").scale()).isEqualTo(0);
        assertThat(purchased.columnWithName("purchaseDate").position()).isEqualTo(3);
        assertThat(purchased.columnWithName("purchaseDate").isAutoIncremented()).isFalse();
        assertThat(purchased.columnWithName("purchaseDate").isGenerated()).isFalse();
        assertThat(purchased.columnWithName("purchaseDate").isOptional()).isFalse();
    }
}
Also used : Table(io.debezium.relational.Table) Column(io.debezium.relational.Column) Tables(io.debezium.relational.Tables) Test(org.junit.Test)

Aggregations

Table (io.debezium.relational.Table)47 TableId (io.debezium.relational.TableId)39 Test (org.junit.Test)34 FixFor (io.debezium.doc.FixFor)17 Column (io.debezium.relational.Column)6 TableSchema (io.debezium.relational.TableSchema)4 TableEditor (io.debezium.relational.TableEditor)3 ArrayList (java.util.ArrayList)3 Predicates (io.debezium.function.Predicates)2 ParsingException (io.debezium.text.ParsingException)2 Marker (io.debezium.text.TokenStream.Marker)2 Strings (io.debezium.util.Strings)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConnectException (org.apache.kafka.connect.errors.ConnectException)2 Immutable (io.debezium.annotation.Immutable)1 Configuration (io.debezium.config.Configuration)1 RecordsForTable (io.debezium.connector.mysql.RecordMakers.RecordsForTable)1 PostgresConnection (io.debezium.connector.postgresql.connection.PostgresConnection)1 BufferedBlockingConsumer (io.debezium.function.BufferedBlockingConsumer)1