Skip to content

Commit cadcdcc

Browse files
dveedenlance6716
andauthored
dump: Support newer mysqldump versions (#932)
Old mysqldump used `CHANGE MASTER TO ...` and newer versions use `CHANGE REPLICATION SOURCE ...`. Co-authored-by: lance6716 <[email protected]>
1 parent 22158ba commit cadcdcc

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

dump/parser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var valuesExp *regexp.Regexp
2929
var gtidExp *regexp.Regexp
3030

3131
func init() {
32-
binlogExp = regexp.MustCompile(`^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\d+);`)
32+
binlogExp = regexp.MustCompile(`^CHANGE (MASTER|REPLICATION SOURCE) TO (MASTER_LOG_FILE|SOURCE_LOG_FILE)='(.+)', (MASTER_LOG_POS|SOURCE_LOG_POS)=(\d+);`)
3333
useExp = regexp.MustCompile("^USE `(.+)`;")
3434
valuesExp = regexp.MustCompile("^INSERT INTO `(.+?)` VALUES \\((.+)\\);$")
3535
// The pattern will only match MySQL GTID, as you know SET GLOBAL gtid_slave_pos='0-1-4' is used for MariaDB.
@@ -71,8 +71,8 @@ func Parse(r io.Reader, h ParseHandler, parseBinlogPos bool) error {
7171
}
7272
}
7373
if m := binlogExp.FindAllStringSubmatch(line, -1); len(m) == 1 {
74-
name := m[0][1]
75-
pos, err := strconv.ParseUint(m[0][2], 10, 64)
74+
name := m[0][3]
75+
pos, err := strconv.ParseUint(m[0][5], 10, 64)
7676
if err != nil {
7777
return errors.Errorf("parse binlog %v err, invalid number", line)
7878
}

dump/parser_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
// This tests the binlogExp regexp that matches the line that mysqldump adds when called with --master-data or --source-data
12+
func TestBinlogExp(t *testing.T) {
13+
stmts := []struct {
14+
input string
15+
file string
16+
pos string
17+
}{
18+
{
19+
// MySQL 9.1.0
20+
`CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE='binlog.000002', SOURCE_LOG_POS=170923;`,
21+
`binlog.000002`,
22+
`170923`,
23+
},
24+
{
25+
`CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.008995', MASTER_LOG_POS=102052485;`,
26+
`mysql-bin.008995`,
27+
`102052485`,
28+
},
29+
}
30+
31+
for _, stmt := range stmts {
32+
m := binlogExp.FindAllStringSubmatch(stmt.input, -1)
33+
require.NotNil(t, m)
34+
require.Equal(t, stmt.file, m[0][3])
35+
require.Equal(t, stmt.pos, m[0][5])
36+
}
37+
}
38+
1139
func TestParseGtidExp(t *testing.T) {
1240
// binlogExp := regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
1341
// gtidExp := regexp.MustCompile("(\\w{8}(-\\w{4}){3}-\\w{12}:\\d+-\\d+)")

0 commit comments

Comments
 (0)