Skip to content

Commit 39022cf

Browse files
Move RequiresBinlogFormatChange to inspector + add test
1 parent 4ab42b4 commit 39022cf

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

go/base/context.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,6 @@ func (this *MigrationContext) GetVoluntaryLockName() string {
354354
return fmt.Sprintf("%s.%s.lock", this.DatabaseName, this.OriginalTableName)
355355
}
356356

357-
// RequiresBinlogFormatChange is `true` when the original binlog format isn't `ROW`
358-
func (this *MigrationContext) RequiresBinlogFormatChange() bool {
359-
if this.InspectorServerInfo == nil {
360-
return true
361-
}
362-
return this.InspectorServerInfo.BinlogFormat != "ROW"
363-
}
364-
365357
// GetApplierHostname is a safe access method to the applier hostname
366358
func (this *MigrationContext) GetApplierHostname() string {
367359
if this.ApplierConnectionConfig == nil {

go/base/utils_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func TestValidateConnection(t *testing.T) {
3737
Port: mysql.DefaultInstancePort,
3838
},
3939
}
40-
// check valid port matches connectionConfig
40+
41+
// check valid port matching connectionConfig validates
4142
{
4243
migrationContext := &MigrationContext{Log: NewDefaultLogger()}
4344
serverInfo := &mysql.ServerInfo{
@@ -81,6 +82,15 @@ func TestValidateConnection(t *testing.T) {
8182
}
8283
test.S(t).ExpectNil(ValidateConnection(serverInfo, connectionConfig, migrationContext, "test"))
8384
}
85+
// check extra_port validates when port does not match but extra_port does
86+
{
87+
migrationContext := &MigrationContext{Log: NewDefaultLogger()}
88+
serverInfo := &mysql.ServerInfo{
89+
Port: gosql.NullInt64{Int64: 12345, Valid: true},
90+
ExtraPort: gosql.NullInt64{Int64: mysql.DefaultInstancePort, Valid: true},
91+
}
92+
test.S(t).ExpectNil(ValidateConnection(serverInfo, connectionConfig, migrationContext, "test"))
93+
}
8494
// check validation fails when valid port does not match connectionConfig
8595
{
8696
migrationContext := &MigrationContext{Log: NewDefaultLogger()}

go/logic/inspect.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func (this *Inspector) InitDBConnections() (err error) {
8181
return nil
8282
}
8383

84+
// RequiresBinlogFormatChange is `true` when the original binlog format isn't `ROW`
85+
func (this *Inspector) RequiresBinlogFormatChange() bool {
86+
if this.ServerInfo() == nil {
87+
return true
88+
}
89+
return this.ServerInfo().BinlogFormat != "ROW"
90+
}
91+
8492
func (this *Inspector) ValidateOriginalTable() (err error) {
8593
if err := this.validateTable(); err != nil {
8694
return err
@@ -318,7 +326,7 @@ func (this *Inspector) restartReplication() error {
318326
// applyBinlogFormat sets ROW binlog format and restarts replication to make
319327
// the replication thread apply it.
320328
func (this *Inspector) applyBinlogFormat() error {
321-
if this.migrationContext.RequiresBinlogFormatChange() {
329+
if this.RequiresBinlogFormatChange() {
322330
if !this.migrationContext.SwitchToRowBinlogFormat {
323331
return fmt.Errorf("Existing binlog_format is %s. Am not switching it to ROW unless you specify --switch-to-rbr",
324332
this.ServerInfo().BinlogFormat)
@@ -350,7 +358,7 @@ func (this *Inspector) validateBinlogs() error {
350358
return fmt.Errorf("%s must have binary logs enabled", this.connectionConfig.Key.String())
351359
}
352360

353-
if this.migrationContext.RequiresBinlogFormatChange() {
361+
if this.RequiresBinlogFormatChange() {
354362
if !this.migrationContext.SwitchToRowBinlogFormat {
355363
return fmt.Errorf("You must be using ROW binlog format. I can switch it for you, provided --switch-to-rbr and that %s doesn't have replicas", this.connectionConfig.Key.String())
356364
}

go/logic/inspect_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
test "github.com/openark/golib/tests"
1212

13+
"github.com/github/gh-ost/go/base"
14+
"github.com/github/gh-ost/go/mysql"
1315
"github.com/github/gh-ost/go/sql"
1416
)
1517

@@ -29,3 +31,25 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) {
2931
test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id")
3032
test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id")
3133
}
34+
35+
func TestRequiresBinlogFormatChange(t *testing.T) {
36+
migrationContext := &base.MigrationContext{
37+
InspectorServerInfo: &mysql.ServerInfo{},
38+
}
39+
40+
{
41+
migrationContext.InspectorServerInfo.BinlogFormat = "MINIMAL"
42+
inspector := &Inspector{migrationContext: migrationContext}
43+
test.S(t).ExpectTrue(inspector.RequiresBinlogFormatChange())
44+
}
45+
{
46+
migrationContext.InspectorServerInfo.BinlogFormat = "MIXED"
47+
inspector := &Inspector{migrationContext: migrationContext}
48+
test.S(t).ExpectTrue(inspector.RequiresBinlogFormatChange())
49+
}
50+
{
51+
migrationContext.InspectorServerInfo.BinlogFormat = "ROW"
52+
inspector := &Inspector{migrationContext: migrationContext}
53+
test.S(t).ExpectFalse(inspector.RequiresBinlogFormatChange())
54+
}
55+
}

0 commit comments

Comments
 (0)