Skip to content

Commit

Permalink
dump: Support newer mysqldump versions
Browse files Browse the repository at this point in the history
Old mysqldump used `CHANGE MASTER TO ...` and newer versions use `CHANGE
REPLICATION SOURCE ...`.
  • Loading branch information
dveeden committed Oct 26, 2024
1 parent ff1dab4 commit ced81e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dump/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var valuesExp *regexp.Regexp
var gtidExp *regexp.Regexp

func init() {
binlogExp = regexp.MustCompile(`^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\d+);`)
binlogExp = regexp.MustCompile(`^CHANGE (MASTER|REPLICATION SOURCE) TO (MASTER_LOG_FILE|SOURCE_LOG_FILE)='(.+)', (MASTER_LOG_POS|SOURCE_LOG_POS)=(\d+);`)
useExp = regexp.MustCompile("^USE `(.+)`;")
valuesExp = regexp.MustCompile("^INSERT INTO `(.+?)` VALUES \\((.+)\\);$")
// The pattern will only match MySQL GTID, as you know SET GLOBAL gtid_slave_pos='0-1-4' is used for MariaDB.
Expand Down Expand Up @@ -71,8 +71,8 @@ func Parse(r io.Reader, h ParseHandler, parseBinlogPos bool) error {
}
}
if m := binlogExp.FindAllStringSubmatch(line, -1); len(m) == 1 {
name := m[0][1]
pos, err := strconv.ParseUint(m[0][2], 10, 64)
name := m[0][3]
pos, err := strconv.ParseUint(m[0][5], 10, 64)
if err != nil {
return errors.Errorf("parse binlog %v err, invalid number", line)
}
Expand Down
28 changes: 28 additions & 0 deletions dump/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ import (
"github.com/stretchr/testify/require"
)

// This tests the binlogExp regexp that matches the line that mysqldump adds when called with --master-data or --source-data
func TestBinlogExp(t *testing.T) {
stmts := []struct {
input string
file string
pos string
}{
{
// MySQL 9.1.0
`CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE='binlog.000002', SOURCE_LOG_POS=170923;`,
`binlog.000002`,
`170923`,
},
{
`CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.008995', MASTER_LOG_POS=102052485;`,
`mysql-bin.008995`,
`102052485`,
},
}

for _, stmt := range stmts {
m := binlogExp.FindAllStringSubmatch(stmt.input, -1)
require.NotNil(t, m)
require.Equal(t, stmt.file, m[0][3])
require.Equal(t, stmt.pos, m[0][5])
}
}

func TestParseGtidExp(t *testing.T) {
// binlogExp := regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
// gtidExp := regexp.MustCompile("(\\w{8}(-\\w{4}){3}-\\w{12}:\\d+-\\d+)")
Expand Down

0 comments on commit ced81e5

Please sign in to comment.