diff --git a/canal/sync.go b/canal/sync.go index ff515a06a..e93826663 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -324,13 +324,34 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error { } } -func (c *Canal) GetMasterPos() (mysql.Position, error) { - showBinlogStatus := "SHOW BINARY LOG STATUS" - if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq < 0) { - showBinlogStatus = "SHOW MASTER STATUS" +// getShowBinaryLogQuery returns the correct SQL statement to query binlog status +// for the given database flavor and server version. +// +// Sources: +// +// MySQL: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html +// MariaDB: https://mariadb.com/kb/en/show-binlog-status +func getShowBinaryLogQuery(flavor, serverVersion string) string { + switch flavor { + case mysql.MariaDBFlavor: + eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2") + if (err == nil) && (eq >= 0) { + return "SHOW BINLOG STATUS" + } + case mysql.MySQLFlavor: + eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0") + if (err == nil) && (eq >= 0) { + return "SHOW BINARY LOG STATUS" + } } - rr, err := c.Execute(showBinlogStatus) + return "SHOW MASTER STATUS" +} + +func (c *Canal) GetMasterPos() (mysql.Position, error) { + query := getShowBinaryLogQuery(c.cfg.Flavor, c.conn.GetServerVersion()) + + rr, err := c.Execute(query) if err != nil { return mysql.Position{}, errors.Trace(err) } diff --git a/canal/sync_test.go b/canal/sync_test.go new file mode 100644 index 000000000..cd3a6711d --- /dev/null +++ b/canal/sync_test.go @@ -0,0 +1,31 @@ +package canal + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGetShowBinaryLogQuery(t *testing.T) { + tests := []struct { + flavor string + serverVersion string + expected string + }{ + {flavor: "mariadb", serverVersion: "10.5.2", expected: "SHOW BINLOG STATUS"}, + {flavor: "mariadb", serverVersion: "10.6.0", expected: "SHOW BINLOG STATUS"}, + {flavor: "mariadb", serverVersion: "10.4.0", expected: "SHOW MASTER STATUS"}, + {flavor: "mysql", serverVersion: "8.4.0", expected: "SHOW BINARY LOG STATUS"}, + {flavor: "mysql", serverVersion: "8.4.1", expected: "SHOW BINARY LOG STATUS"}, + {flavor: "mysql", serverVersion: "8.0.33", expected: "SHOW MASTER STATUS"}, + {flavor: "mysql", serverVersion: "5.7.41", expected: "SHOW MASTER STATUS"}, + {flavor: "other", serverVersion: "1.0.0", expected: "SHOW MASTER STATUS"}, + } + + for _, tt := range tests { + t.Run(tt.flavor+"_"+tt.serverVersion, func(t *testing.T) { + got := getShowBinaryLogQuery(tt.flavor, tt.serverVersion) + require.Equal(t, tt.expected, got) + }) + } +}