From 05c7ed5f8ffb2c2e13ca2c51f059266a9b051a64 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 6 Sep 2022 15:23:18 +0200 Subject: [PATCH] Add basic test for inspector (#1166) * Add basic test for inspector * Add header * Fix return --- go/logic/inspect.go | 9 +++------ go/logic/inspect_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 go/logic/inspect_test.go diff --git a/go/logic/inspect.go b/go/logic/inspect.go index 83b778a..4df80fe 100644 --- a/go/logic/inspect.go +++ b/go/logic/inspect.go @@ -132,10 +132,7 @@ func (this *Inspector) inspectOriginalAndGhostTables() (err error) { if err != nil { return err } - sharedUniqueKeys, err := this.getSharedUniqueKeys(this.migrationContext.OriginalTableUniqueKeys, this.migrationContext.GhostTableUniqueKeys) - if err != nil { - return err - } + sharedUniqueKeys := this.getSharedUniqueKeys(this.migrationContext.OriginalTableUniqueKeys, this.migrationContext.GhostTableUniqueKeys) for i, sharedUniqueKey := range sharedUniqueKeys { this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, &sharedUniqueKey.Columns) uniqueKeyIsValid := true @@ -731,7 +728,7 @@ func (this *Inspector) getCandidateUniqueKeys(tableName string) (uniqueKeys [](* // getSharedUniqueKeys returns the intersection of two given unique keys, // testing by list of columns -func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [](*sql.UniqueKey)) (uniqueKeys [](*sql.UniqueKey), err error) { +func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys []*sql.UniqueKey) (uniqueKeys []*sql.UniqueKey) { // We actually do NOT rely on key name, just on the set of columns. This is because maybe // the ALTER is on the name itself... for _, originalUniqueKey := range originalUniqueKeys { @@ -741,7 +738,7 @@ func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [ } } } - return uniqueKeys, nil + return uniqueKeys } // getSharedColumns returns the intersection of two lists of columns in same order as the first list diff --git a/go/logic/inspect_test.go b/go/logic/inspect_test.go new file mode 100644 index 0000000..54bc48f --- /dev/null +++ b/go/logic/inspect_test.go @@ -0,0 +1,31 @@ +/* + Copyright 2022 GitHub Inc. + See https://github.com/github/gh-ost/blob/master/LICENSE +*/ + +package logic + +import ( + "testing" + + test "github.com/openark/golib/tests" + + "github.com/github/gh-ost/go/sql" +) + +func TestInspectGetSharedUniqueKeys(t *testing.T) { + origUniqKeys := []*sql.UniqueKey{ + {Columns: *sql.NewColumnList([]string{"id", "item_id"})}, + {Columns: *sql.NewColumnList([]string{"id", "org_id"})}, + } + ghostUniqKeys := []*sql.UniqueKey{ + {Columns: *sql.NewColumnList([]string{"id", "item_id"})}, + {Columns: *sql.NewColumnList([]string{"id", "org_id"})}, + {Columns: *sql.NewColumnList([]string{"item_id", "user_id"})}, + } + inspector := &Inspector{} + sharedUniqKeys := inspector.getSharedUniqueKeys(origUniqKeys, ghostUniqKeys) + test.S(t).ExpectEquals(len(sharedUniqKeys), 2) + test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id") + test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id") +}