Skip to content

Commit b290e28

Browse files
author
Guillem
committed
add test
1 parent db9a729 commit b290e28

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

canal/sync.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,19 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error {
324324
}
325325
}
326326

327-
func (c *Canal) getShowBinaryLogQuery() string {
328-
switch c.cfg.Flavor {
327+
func getShowBinaryLogQuery(flavor, serverVersion string) string {
328+
switch flavor {
329329
case mysql.MariaDBFlavor:
330330
// Source: https://mariadb.com/kb/en/show-binlog-status/#:~:text=SHOW%20BINLOG%20STATUS%20%2D%2D-,From%20MariaDB%2010.5.2,-Description
331-
if eq, err := c.conn.CompareServerVersion("10.5.2"); (err == nil) && (eq >= 0) {
331+
332+
eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2")
333+
if (err == nil) && (eq >= 0) {
332334
return "SHOW BINLOG STATUS"
333335
}
334336
case mysql.MySQLFlavor:
335337
// Source: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html#:~:text=AND%20GTIDS)%3B-,SHOW%20MASTER%20STATUS,-(SHOW%20BINARY
336-
if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq >= 0) {
338+
eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0")
339+
if (err == nil) && (eq >= 0) {
337340
return "SHOW BINARY LOG STATUS"
338341
}
339342
}
@@ -342,7 +345,7 @@ func (c *Canal) getShowBinaryLogQuery() string {
342345
}
343346

344347
func (c *Canal) GetMasterPos() (mysql.Position, error) {
345-
query := c.getShowBinaryLogQuery()
348+
query := getShowBinaryLogQuery(c.cfg.Flavor, c.conn.GetServerVersion())
346349

347350
rr, err := c.Execute(query)
348351
if err != nil {

canal/sync_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package canal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestGetShowBinaryLogQuery(t *testing.T) {
10+
tests := []struct {
11+
flavor string
12+
serverVersion string
13+
expected string
14+
}{
15+
{flavor: "mariadb", serverVersion: "10.5.2", expected: "SHOW BINLOG STATUS"},
16+
{flavor: "mariadb", serverVersion: "10.6.0", expected: "SHOW BINLOG STATUS"},
17+
{flavor: "mariadb", serverVersion: "10.4.0", expected: "SHOW MASTER STATUS"},
18+
{flavor: "mysql", serverVersion: "8.4.0", expected: "SHOW BINARY LOG STATUS"},
19+
{flavor: "mysql", serverVersion: "8.4.1", expected: "SHOW BINARY LOG STATUS"},
20+
{flavor: "mysql", serverVersion: "8.0.33", expected: "SHOW MASTER STATUS"},
21+
{flavor: "mysql", serverVersion: "5.7.41", expected: "SHOW MASTER STATUS"},
22+
{flavor: "other", serverVersion: "1.0.0", expected: "SHOW MASTER STATUS"},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.flavor+"_"+tt.serverVersion, func(t *testing.T) {
27+
got := getShowBinaryLogQuery(tt.flavor, tt.serverVersion)
28+
require.Equal(t, tt.expected, got)
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)