Skip to content

Commit 428d9ba

Browse files
Give RowsEvent methods to inspect the underlying event type.
It's currently impossible to figure out what kind of operation led to a `RowsEvent`; multiple results indicate an `UPDATE`, but there's no way to differentiate an `INSERT` from a `DELETE`.
1 parent 567aa59 commit 428d9ba

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

replication/row_event.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,18 @@ func (e *RowsEvent) Decode(data []byte) error {
11201120
return e.DecodeData(pos, data)
11211121
}
11221122

1123+
func (e *RowsEvent) IsInsert() bool {
1124+
return e.eventType == WRITE_ROWS_EVENTv0 || e.eventType == WRITE_ROWS_EVENTv1 || e.eventType == WRITE_ROWS_EVENTv2 || e.eventType == MARIADB_WRITE_ROWS_COMPRESSED_EVENT_V1
1125+
}
1126+
1127+
func (e *RowsEvent) IsUpdate() bool {
1128+
return e.eventType == UPDATE_ROWS_EVENTv0 || e.eventType == UPDATE_ROWS_EVENTv1 || e.eventType == UPDATE_ROWS_EVENTv2 || e.eventType == MARIADB_UPDATE_ROWS_COMPRESSED_EVENT_V1
1129+
}
1130+
1131+
func (e *RowsEvent) IsDelete() bool {
1132+
return e.eventType == DELETE_ROWS_EVENTv0 || e.eventType == DELETE_ROWS_EVENTv1 || e.eventType == DELETE_ROWS_EVENTv2 || e.eventType == MARIADB_DELETE_ROWS_COMPRESSED_EVENT_V1
1133+
}
1134+
11231135
func isBitSet(bitmap []byte, i int) bool {
11241136
return bitmap[i>>3]&(1<<(uint(i)&7)) > 0
11251137
}

replication/row_event_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,41 @@ func TestRowsDataExtraData(t *testing.T) {
11761176
}
11771177
}
11781178

1179+
func TestRowsEventTypoe(t *testing.T) {
1180+
testcases := []struct {
1181+
eventType EventType
1182+
isInsert bool
1183+
isUpdate bool
1184+
isDelete bool
1185+
}{
1186+
{WRITE_ROWS_EVENTv0, true, false, false},
1187+
{WRITE_ROWS_EVENTv1, true, false, false},
1188+
{WRITE_ROWS_EVENTv2, true, false, false},
1189+
{MARIADB_WRITE_ROWS_COMPRESSED_EVENT_V1, true, false, false},
1190+
{UPDATE_ROWS_EVENTv0, false, true, false},
1191+
{UPDATE_ROWS_EVENTv1, false, true, false},
1192+
{UPDATE_ROWS_EVENTv2, false, true, false},
1193+
{MARIADB_UPDATE_ROWS_COMPRESSED_EVENT_V1, false, true, false},
1194+
{DELETE_ROWS_EVENTv0, false, false, true},
1195+
{DELETE_ROWS_EVENTv1, false, false, true},
1196+
{DELETE_ROWS_EVENTv2, false, false, true},
1197+
{MARIADB_DELETE_ROWS_COMPRESSED_EVENT_V1, false, false, true},
1198+
1199+
// Whoops, these are not rows events at all
1200+
{EXEC_LOAD_EVENT, false, false, false},
1201+
{HEARTBEAT_EVENT, false, false, false},
1202+
}
1203+
1204+
for _, tc := range testcases {
1205+
rev := new(RowsEvent)
1206+
rev.eventType = tc.eventType
1207+
1208+
require.Equal(t, tc.isInsert, rev.IsInsert())
1209+
require.Equal(t, tc.isUpdate, rev.IsUpdate())
1210+
require.Equal(t, tc.isDelete, rev.IsDelete())
1211+
}
1212+
}
1213+
11791214
func TestTableMapHelperMaps(t *testing.T) {
11801215
/*
11811216
CREATE TABLE `_types` (

0 commit comments

Comments
 (0)