- 将臃肿的 parse.go 按职责拆分为多个模块:
parse_types.go、parse_io.go、parse_event_convert.go、parse_stream.go、parse_filter.go
- parse.go 保留为模块入口说明,提升可维护性与可读性
- 修复事务状态被覆盖问题(BEGIN/COMMIT/ROLLBACK 不再被重置为 PREPARE)
- 增加 include-tables 与 exclude-tables 互斥校验,同时配置时直接报配置错误
- 强化表匹配器模式校验,并补充非法模式测试
- 在明细过滤后重算事务统计(RowsCount/StartPos/EndPos/Size),避免统计失真
- 增加 TABLE_MAP 事件转换,补充列元信息透传(ColumnTypes/ColumnCollationIDs)
- 基于 unsigned 元数据规范化行值,避免无符号整型被渲染为负数
- 优化事件解析报错信息:增加有界 body 十六进制预览
- 补充单元测试:payload/tablemap 转换、unsigned 规范化、过滤逻辑、IO 预览
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package binlog
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/starainrt/go-mysql/mysql"
|
|
"github.com/starainrt/go-mysql/replication"
|
|
)
|
|
|
|
func TestParseBinlogEvent_TableMapEvent(t *testing.T) {
|
|
ev := &replication.BinlogEvent{
|
|
Header: &replication.EventHeader{EventType: replication.TABLE_MAP_EVENT},
|
|
Event: &replication.TableMapEvent{
|
|
Schema: []byte("db1"),
|
|
Table: []byte("tb1"),
|
|
},
|
|
}
|
|
|
|
events := ParseBinlogEvent(ev)
|
|
if len(events) != 1 {
|
|
t.Fatalf("expected 1 event, got %d", len(events))
|
|
}
|
|
if events[0].Type != "tablemap" {
|
|
t.Fatalf("expected tablemap event type, got %q", events[0].Type)
|
|
}
|
|
if events[0].DB != "db1" || events[0].TB != "tb1" {
|
|
t.Fatalf("unexpected db/table: %s.%s", events[0].DB, events[0].TB)
|
|
}
|
|
}
|
|
|
|
func TestParseBinlogEvent_TransactionPayloadContainsTableMap(t *testing.T) {
|
|
table := &replication.TableMapEvent{
|
|
Schema: []byte("db2"),
|
|
Table: []byte("tb2"),
|
|
ColumnType: []byte{mysql.MYSQL_TYPE_LONG},
|
|
}
|
|
|
|
payload := &replication.TransactionPayloadEvent{
|
|
CompressionType: CompressionZSTD,
|
|
Events: []*replication.BinlogEvent{
|
|
{
|
|
Header: &replication.EventHeader{EventType: replication.TABLE_MAP_EVENT},
|
|
Event: table,
|
|
},
|
|
{
|
|
Header: &replication.EventHeader{EventType: replication.WRITE_ROWS_EVENTv2},
|
|
Event: &replication.RowsEvent{
|
|
Table: table,
|
|
Rows: [][]interface{}{{int32(1)}},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
ev := &replication.BinlogEvent{
|
|
Header: &replication.EventHeader{EventType: replication.TRANSACTION_PAYLOAD_EVENT},
|
|
Event: payload,
|
|
}
|
|
|
|
events := ParseBinlogEvent(ev)
|
|
if len(events) != 2 {
|
|
t.Fatalf("expected 2 events from payload, got %d", len(events))
|
|
}
|
|
if events[0].Type != "tablemap" {
|
|
t.Fatalf("expected first payload event to be tablemap, got %q", events[0].Type)
|
|
}
|
|
if events[1].Type != "insert" {
|
|
t.Fatalf("expected second payload event to be insert, got %q", events[1].Type)
|
|
}
|
|
if events[0].CompressionType != "ZSTD" || events[1].CompressionType != "ZSTD" {
|
|
t.Fatalf("expected payload events to carry compression type, got %q/%q", events[0].CompressionType, events[1].CompressionType)
|
|
}
|
|
}
|