Skip to content

Commit

Permalink
Move RequiresBinlogFormatChange to inspector + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
timvaillancourt committed Dec 13, 2022
1 parent 4ab42b4 commit 39022cf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
8 changes: 0 additions & 8 deletions go/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,6 @@ func (this *MigrationContext) GetVoluntaryLockName() string {
return fmt.Sprintf("%s.%s.lock", this.DatabaseName, this.OriginalTableName)
}

// RequiresBinlogFormatChange is `true` when the original binlog format isn't `ROW`
func (this *MigrationContext) RequiresBinlogFormatChange() bool {
if this.InspectorServerInfo == nil {
return true
}
return this.InspectorServerInfo.BinlogFormat != "ROW"
}

// GetApplierHostname is a safe access method to the applier hostname
func (this *MigrationContext) GetApplierHostname() string {
if this.ApplierConnectionConfig == nil {
Expand Down
12 changes: 11 additions & 1 deletion go/base/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestValidateConnection(t *testing.T) {
Port: mysql.DefaultInstancePort,
},
}
// check valid port matches connectionConfig

// check valid port matching connectionConfig validates
{
migrationContext := &MigrationContext{Log: NewDefaultLogger()}
serverInfo := &mysql.ServerInfo{
Expand Down Expand Up @@ -81,6 +82,15 @@ func TestValidateConnection(t *testing.T) {
}
test.S(t).ExpectNil(ValidateConnection(serverInfo, connectionConfig, migrationContext, "test"))
}
// check extra_port validates when port does not match but extra_port does
{
migrationContext := &MigrationContext{Log: NewDefaultLogger()}
serverInfo := &mysql.ServerInfo{
Port: gosql.NullInt64{Int64: 12345, Valid: true},
ExtraPort: gosql.NullInt64{Int64: mysql.DefaultInstancePort, Valid: true},
}
test.S(t).ExpectNil(ValidateConnection(serverInfo, connectionConfig, migrationContext, "test"))
}
// check validation fails when valid port does not match connectionConfig
{
migrationContext := &MigrationContext{Log: NewDefaultLogger()}
Expand Down
12 changes: 10 additions & 2 deletions go/logic/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (this *Inspector) InitDBConnections() (err error) {
return nil
}

// RequiresBinlogFormatChange is `true` when the original binlog format isn't `ROW`
func (this *Inspector) RequiresBinlogFormatChange() bool {
if this.ServerInfo() == nil {
return true
}
return this.ServerInfo().BinlogFormat != "ROW"
}

func (this *Inspector) ValidateOriginalTable() (err error) {
if err := this.validateTable(); err != nil {
return err
Expand Down Expand Up @@ -318,7 +326,7 @@ func (this *Inspector) restartReplication() error {
// applyBinlogFormat sets ROW binlog format and restarts replication to make
// the replication thread apply it.
func (this *Inspector) applyBinlogFormat() error {
if this.migrationContext.RequiresBinlogFormatChange() {
if this.RequiresBinlogFormatChange() {
if !this.migrationContext.SwitchToRowBinlogFormat {
return fmt.Errorf("Existing binlog_format is %s. Am not switching it to ROW unless you specify --switch-to-rbr",
this.ServerInfo().BinlogFormat)
Expand Down Expand Up @@ -350,7 +358,7 @@ func (this *Inspector) validateBinlogs() error {
return fmt.Errorf("%s must have binary logs enabled", this.connectionConfig.Key.String())
}

if this.migrationContext.RequiresBinlogFormatChange() {
if this.RequiresBinlogFormatChange() {
if !this.migrationContext.SwitchToRowBinlogFormat {
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())
}
Expand Down
24 changes: 24 additions & 0 deletions go/logic/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

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

"github.com/github/gh-ost/go/base"
"github.com/github/gh-ost/go/mysql"
"github.com/github/gh-ost/go/sql"
)

Expand All @@ -29,3 +31,25 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) {
test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id")
test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id")
}

func TestRequiresBinlogFormatChange(t *testing.T) {
migrationContext := &base.MigrationContext{
InspectorServerInfo: &mysql.ServerInfo{},
}

{
migrationContext.InspectorServerInfo.BinlogFormat = "MINIMAL"
inspector := &Inspector{migrationContext: migrationContext}
test.S(t).ExpectTrue(inspector.RequiresBinlogFormatChange())
}
{
migrationContext.InspectorServerInfo.BinlogFormat = "MIXED"
inspector := &Inspector{migrationContext: migrationContext}
test.S(t).ExpectTrue(inspector.RequiresBinlogFormatChange())
}
{
migrationContext.InspectorServerInfo.BinlogFormat = "ROW"
inspector := &Inspector{migrationContext: migrationContext}
test.S(t).ExpectFalse(inspector.RequiresBinlogFormatChange())
}
}

0 comments on commit 39022cf

Please sign in to comment.