Skip to content

Commit 05c7ed5

Browse files
Add basic test for inspector (#1166)
* Add basic test for inspector * Add header * Fix return
1 parent 1fa3d4f commit 05c7ed5

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

go/logic/inspect.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ func (this *Inspector) inspectOriginalAndGhostTables() (err error) {
132132
if err != nil {
133133
return err
134134
}
135-
sharedUniqueKeys, err := this.getSharedUniqueKeys(this.migrationContext.OriginalTableUniqueKeys, this.migrationContext.GhostTableUniqueKeys)
136-
if err != nil {
137-
return err
138-
}
135+
sharedUniqueKeys := this.getSharedUniqueKeys(this.migrationContext.OriginalTableUniqueKeys, this.migrationContext.GhostTableUniqueKeys)
139136
for i, sharedUniqueKey := range sharedUniqueKeys {
140137
this.applyColumnTypes(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, &sharedUniqueKey.Columns)
141138
uniqueKeyIsValid := true
@@ -731,7 +728,7 @@ func (this *Inspector) getCandidateUniqueKeys(tableName string) (uniqueKeys [](*
731728

732729
// getSharedUniqueKeys returns the intersection of two given unique keys,
733730
// testing by list of columns
734-
func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [](*sql.UniqueKey)) (uniqueKeys [](*sql.UniqueKey), err error) {
731+
func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys []*sql.UniqueKey) (uniqueKeys []*sql.UniqueKey) {
735732
// We actually do NOT rely on key name, just on the set of columns. This is because maybe
736733
// the ALTER is on the name itself...
737734
for _, originalUniqueKey := range originalUniqueKeys {
@@ -741,7 +738,7 @@ func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [
741738
}
742739
}
743740
}
744-
return uniqueKeys, nil
741+
return uniqueKeys
745742
}
746743

747744
// getSharedColumns returns the intersection of two lists of columns in same order as the first list

go/logic/inspect_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2022 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package logic
7+
8+
import (
9+
"testing"
10+
11+
test "github.com/openark/golib/tests"
12+
13+
"github.com/github/gh-ost/go/sql"
14+
)
15+
16+
func TestInspectGetSharedUniqueKeys(t *testing.T) {
17+
origUniqKeys := []*sql.UniqueKey{
18+
{Columns: *sql.NewColumnList([]string{"id", "item_id"})},
19+
{Columns: *sql.NewColumnList([]string{"id", "org_id"})},
20+
}
21+
ghostUniqKeys := []*sql.UniqueKey{
22+
{Columns: *sql.NewColumnList([]string{"id", "item_id"})},
23+
{Columns: *sql.NewColumnList([]string{"id", "org_id"})},
24+
{Columns: *sql.NewColumnList([]string{"item_id", "user_id"})},
25+
}
26+
inspector := &Inspector{}
27+
sharedUniqKeys := inspector.getSharedUniqueKeys(origUniqKeys, ghostUniqKeys)
28+
test.S(t).ExpectEquals(len(sharedUniqKeys), 2)
29+
test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id")
30+
test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id")
31+
}

0 commit comments

Comments
 (0)