diff --git a/go/base/context.go b/go/base/context.go index 91ac8e4..f06fc75 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -8,6 +8,7 @@ package base import ( "encoding/json" "fmt" + "io" "os" "regexp" "strings" @@ -256,8 +257,12 @@ func (this *MigrationContext) LoadJSON(jsonString string) error { defer this.throttleMutex.Unlock() jsonBytes := []byte(jsonString) - err := json.Unmarshal(jsonBytes, this) + if err := json.Unmarshal(jsonBytes, this); err != nil && err != io.EOF { + return err + } + + var err error if this.MigrationRangeMinValues, err = sql.NewColumnValuesFromBase64(this.EncodedRangeValues["MigrationRangeMinValues"]); err != nil { return err } @@ -271,7 +276,7 @@ func (this *MigrationContext) LoadJSON(jsonString string) error { return err } - return err + return nil } // GetGhostTableName generates the name of ghost table, based on original table name diff --git a/go/base/context_test.go b/go/base/context_test.go new file mode 100644 index 0000000..a733e02 --- /dev/null +++ b/go/base/context_test.go @@ -0,0 +1,54 @@ +/* + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-ost/blob/master/LICENSE +*/ + +package base + +import ( + "io" + "testing" + + "github.com/outbrain/golib/log" + test "github.com/outbrain/golib/tests" + + "github.com/github/gh-ost/go/mysql" + "github.com/github/gh-ost/go/sql" +) + +func init() { + log.SetLevel(log.ERROR) +} + +func TestContextToJSON(t *testing.T) { + context := NewMigrationContext() + jsonString, err := context.ToJSON() + test.S(t).ExpectNil(err) + test.S(t).ExpectNotEquals(jsonString, "") +} + +func TestContextLoadJSON(t *testing.T) { + var jsonString string + var err error + { + context := NewMigrationContext() + context.AppliedBinlogCoordinates = mysql.BinlogCoordinates{LogFile: "mysql-bin.012345", LogPos: 6789} + + abstractValues := []interface{}{31, "2016-12-24 17:04:32"} + context.MigrationRangeMinValues = sql.ToColumnValues(abstractValues) + + jsonString, err = context.ToJSON() + test.S(t).ExpectNil(err) + test.S(t).ExpectNotEquals(jsonString, "") + } + { + context := NewMigrationContext() + err = context.LoadJSON(jsonString) + test.S(t).ExpectEqualsAny(err, nil, io.EOF) + test.S(t).ExpectEquals(context.AppliedBinlogCoordinates, mysql.BinlogCoordinates{LogFile: "mysql-bin.012345", LogPos: 6789}) + abstractValues := context.MigrationRangeMinValues.AbstractValues() + test.S(t).ExpectEquals(len(abstractValues), 2) + test.S(t).ExpectEquals(abstractValues[0], 31) + test.S(t).ExpectEquals(abstractValues[1], "2016-12-24 17:04:32") + } +}