Search in sources :

Example 16 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testInsertMutation.

@Test
public void testInsertMutation() throws Exception {
    Serializable[] change = new Serializable[4];
    change[0] = 12131L;
    change[1] = "test_user";
    change[2] = 25;
    change[3] = 0;
    Serializable[] change2 = new Serializable[4];
    change2[0] = 12334L;
    change2[1] = "test_user2";
    change2[2] = 12;
    change2[3] = 1;
    BinlogEvent event = new WriteEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change, change2));
    List<? extends Mutation> mutations = eventMapper.map(event);
    assertEquals(2, mutations.size());
    assertTrue(mutations.get(0) instanceof MysqlInsertMutation);
    MysqlInsertMutation mutation = (MysqlInsertMutation) mutations.get(0);
    validateMetadata(mutation, 0);
    Row row = mutation.getEntity();
    assertEquals(12131L, row.getColumns().get("id").getValue());
    assertEquals("test_user", row.getColumns().get("name").getValue());
    assertEquals(25, row.getColumns().get("age").getValue());
    assertEquals(0, row.getColumns().get("sex").getValue());
    assertTrue(mutations.get(1) instanceof MysqlInsertMutation);
    mutation = (MysqlInsertMutation) mutations.get(1);
    validateMetadata(mutation, 1);
    row = mutation.getEntity();
    assertEquals(12334L, row.getColumns().get("id").getValue());
    assertEquals("test_user2", row.getColumns().get("name").getValue());
    assertEquals(12, row.getColumns().get("age").getValue());
    assertEquals(1, row.getColumns().get("sex").getValue());
}
Also used : WriteEvent(com.airbnb.spinaltap.mysql.event.WriteEvent) Serializable(java.io.Serializable) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Example 17 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testUpdateMutationWithNullPK.

@Test
public void testUpdateMutationWithNullPK() throws Exception {
    Serializable[] old = new Serializable[4];
    old[0] = null;
    old[1] = "test_user";
    old[2] = 25;
    old[3] = 0;
    Serializable[] current = new Serializable[4];
    current[0] = null;
    current[1] = old[1];
    current[2] = 26;
    current[3] = old[3];
    Map.Entry<Serializable[], Serializable[]> change = new AbstractMap.SimpleEntry<>(old, current);
    BinlogEvent event = new UpdateEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change));
    List<? extends Mutation> mutations = eventMapper.map(event);
    assertEquals(1, mutations.size());
    assertTrue(mutations.get(0) instanceof MysqlUpdateMutation);
    MysqlUpdateMutation mutation = (MysqlUpdateMutation) mutations.get(0);
    validateMetadata(mutation, 0);
    Row oldRow = mutation.getPreviousRow();
    Row newRow = mutation.getRow();
    assertEquals(null, oldRow.getColumns().get("id").getValue());
    assertEquals("test_user", oldRow.getColumns().get("name").getValue());
    assertEquals(25, oldRow.getColumns().get("age").getValue());
    assertEquals(0, oldRow.getColumns().get("sex").getValue());
    assertEquals(null, newRow.getColumns().get("id").getValue());
    assertEquals("test_user", newRow.getColumns().get("name").getValue());
    assertEquals(26, newRow.getColumns().get("age").getValue());
    assertEquals(0, newRow.getColumns().get("sex").getValue());
}
Also used : Serializable(java.io.Serializable) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Map(java.util.Map) AbstractMap(java.util.AbstractMap) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Example 18 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class MysqlKeyProviderTest method testGetNullKey.

@Test
public void testGetNullKey() throws Exception {
    Row row = new Row(TABLE, ImmutableMap.of(ID_COLUMN, new Column(TABLE.getColumns().get(ID_COLUMN), null)));
    MysqlMutation mutation = new MysqlInsertMutation(MUTATION_METADATA, row);
    assertEquals("test:users:null", MysqlKeyProvider.INSTANCE.get(mutation));
}
Also used : Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Example 19 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class AbstractMysqlSourceTest method testCommitCheckpoint.

@Test
public void testCommitCheckpoint() throws Exception {
    StateHistory stateHistory = new TestStateHistory();
    TestSource source = new TestSource(stateHistory);
    Row row = new Row(null, ImmutableMap.of());
    BinlogFilePos filePos = new BinlogFilePos("test.txt", 18, 156);
    Transaction lastTransaction = new Transaction(0L, 0L, filePos);
    MysqlMutationMetadata metadata = new MysqlMutationMetadata(null, filePos, null, 0L, 0L, 0L, null, lastTransaction, 0, 0);
    MysqlMutation mutation = new MysqlInsertMutation(metadata, row);
    SourceState savedState = new SourceState(SAVED_TIMESTAMP, SAVED_OFFSET, 0L, BINLOG_FILE_POS);
    when(stateRepository.read()).thenReturn(savedState);
    source.initialize();
    source.checkpoint(mutation);
    assertEquals(savedState, source.getLastSavedState().get());
    source.checkpoint(null);
    assertEquals(savedState, source.getLastSavedState().get());
    long newOffset = SAVED_OFFSET + 1;
    metadata = new MysqlMutationMetadata(null, filePos, null, 0L, newOffset, 23L, null, lastTransaction, 0, 0);
    mutation = new MysqlInsertMutation(metadata, row);
    source.checkpoint(mutation);
    assertEquals(new SourceState(23L, newOffset, 0L, filePos), source.getLastSavedState().get());
    assertEquals(stateHistory.removeLast(), source.getLastSavedState().get());
}
Also used : MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) SourceState(com.airbnb.spinaltap.common.source.SourceState) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Example 20 with Row

use of com.airbnb.spinaltap.mysql.mutation.schema.Row in project SpinalTap by airbnb.

the class MutationSchemaValidatorTest method testIncorrectColumn.

@Test(expected = IllegalStateException.class)
public void testIncorrectColumn() throws Exception {
    Row row = new Row(TABLE, ImmutableMap.of(ID_COLUMN, createColumn(ID_COLUMN, ColumnDataType.LONGLONG, true, 1L, 0), NAME_COLUMN, createColumn(NAME_COLUMN, ColumnDataType.VARCHAR, false, "bob", 1), AGE_COLUMN, createColumn(AGE_COLUMN, ColumnDataType.INT24, false, 25, 2), "bad_column", createColumn("bad_column", ColumnDataType.VARCHAR, false, "bad", 3)));
    validator.validate(createMutation(row));
}
Also used : Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Aggregations

Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)22 Test (org.junit.Test)17 ColumnMetadata (com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata)9 Column (com.airbnb.spinaltap.mysql.mutation.schema.Column)8 Table (com.airbnb.spinaltap.mysql.mutation.schema.Table)7 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)6 Serializable (java.io.Serializable)6 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)5 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)5 MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)4 UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)3 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)3 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)3 AbstractMap (java.util.AbstractMap)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 Mutation (com.airbnb.jitney.event.spinaltap.v1.Mutation)1 SourceState (com.airbnb.spinaltap.common.source.SourceState)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 DataSource (com.airbnb.spinaltap.mysql.DataSource)1