Skip to content

Commit 919af6c

Browse files
authored
change OnDDL to OnTableChanged (go-mysql-org#244)
1 parent 251f569 commit 919af6c

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

canal/handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77

88
type EventHandler interface {
99
OnRotate(roateEvent *replication.RotateEvent) error
10+
// OnTableChanged is called when the table is created, altered, renamed or dropped.
11+
// You need to clear the associated data like cache with the table.
12+
// It will be called before OnDDL.
13+
OnTableChanged(schema string, table string) error
1014
OnDDL(nextPos mysql.Position, queryEvent *replication.QueryEvent) error
1115
OnRow(e *RowsEvent) error
1216
OnXID(nextPos mysql.Position) error
@@ -19,8 +23,9 @@ type EventHandler interface {
1923
type DummyEventHandler struct {
2024
}
2125

22-
func (h *DummyEventHandler) OnRotate(*replication.RotateEvent) error { return nil }
23-
func (h *DummyEventHandler) OnDDL(mysql.Position, *replication.QueryEvent) error {
26+
func (h *DummyEventHandler) OnRotate(*replication.RotateEvent) error { return nil }
27+
func (h *DummyEventHandler) OnTableChanged(schema string, table string) error { return nil }
28+
func (h *DummyEventHandler) OnDDL(nextPos mysql.Position, queryEvent *replication.QueryEvent) error {
2429
return nil
2530
}
2631
func (h *DummyEventHandler) OnRow(*RowsEvent) error { return nil }

canal/sync.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
var (
17-
expCreateTable = regexp.MustCompile("(?i)^CREATE\\sTABLE(\\sIF\\sNOT\\sEXISTS)?\\s`{0,1}(.*?)`{0,1}\\.{0,1}`{0,1}([^`\\.]+?)`{0,1}\\s.*")
17+
expCreateTable = regexp.MustCompile("(?i)^CREATE\\sTABLE(\\sIF\\sNOT\\sEXISTS)?\\s`{0,1}(.*?)`{0,1}\\.{0,1}`{0,1}([^`\\.]+?)`{0,1}\\s.*")
1818
expAlterTable = regexp.MustCompile("(?i)^ALTER\\sTABLE\\s.*?`{0,1}(.*?)`{0,1}\\.{0,1}`{0,1}([^`\\.]+?)`{0,1}\\s.*")
1919
expRenameTable = regexp.MustCompile("(?i)^RENAME\\sTABLE\\s.*?`{0,1}(.*?)`{0,1}\\.{0,1}`{0,1}([^`\\.]+?)`{0,1}\\s{1,}TO\\s.*?")
2020
expDropTable = regexp.MustCompile("(?i)^DROP\\sTABLE(\\sIF\\sEXISTS){0,1}\\s`{0,1}(.*?)`{0,1}\\.{0,1}`{0,1}([^`\\.]+?)`{0,1}(?:$|\\s)")
@@ -115,9 +115,9 @@ func (c *Canal) runSyncBinlog() error {
115115
}
116116
case *replication.QueryEvent:
117117
var (
118-
mb [][]byte
118+
mb [][]byte
119119
schema []byte
120-
table []byte
120+
table []byte
121121
)
122122
regexps := []regexp.Regexp{*expCreateTable, *expAlterTable, *expRenameTable, *expDropTable}
123123
for _, reg := range regexps {
@@ -143,10 +143,14 @@ func (c *Canal) runSyncBinlog() error {
143143
force = true
144144
c.ClearTableCache(schema, table)
145145
log.Infof("table structure changed, clear table cache: %s.%s\n", schema, table)
146-
if err = c.eventHandler.OnDDL(pos, e); err != nil {
146+
if err = c.eventHandler.OnTableChanged(string(schema), string(table)); err != nil {
147147
return errors.Trace(err)
148148
}
149149

150+
// Now we only handle Table Changed DDL, maybe we will support more later.
151+
if err = c.eventHandler.OnDDL(pos, e); err != nil {
152+
return errors.Trace(err)
153+
}
150154
default:
151155
continue
152156
}

replication/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func (e *MariadbGTIDEvent) Decode(data []byte) error {
453453
}
454454

455455
func (e *MariadbGTIDEvent) Dump(w io.Writer) {
456-
fmt.Fprintf(w, "GTID: %s\n", e.GTID)
456+
fmt.Fprintf(w, "GTID: %v\n", e.GTID)
457457
fmt.Fprintln(w)
458458
}
459459

0 commit comments

Comments
 (0)