Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use map look-up for indexes #7242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {
func (schema *Schema) LookIndex(name string) *Index {
if schema != nil {
indexes := schema.ParseIndexes()
for _, index := range indexes {
if index.Name == name {
return &index
}

if index, found := indexes[name]; found {
return &index
}

for _, index := range indexes {
for _, field := range index.Fields {
if field.Name == name {
return &index
Expand Down Expand Up @@ -111,10 +112,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
idx = len(tag)
}

if idx != -1 {
name = tag[0:idx]
}

name = tag[0:idx]
if name == "" {
subName := field.Name
const key = "COMPOSITE"
Expand Down
14 changes: 13 additions & 1 deletion schema/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type UserIndex struct {
Name7 string `gorm:"index:type"`
Name8 string `gorm:"index:,length:10;index:,collate:utf8"`

CompName1 string `gorm:"index:,unique,composite:idx_compname_1,option:NULLS NOT DISTINCT;not null"`
CompName2 string `gorm:"index:,composite:idx_compname_1"`

// Composite Index: Flattened structure.
Data0A string `gorm:"index:,composite:comp_id0"`
Data0B string `gorm:"index:,composite:comp_id0"`
Expand Down Expand Up @@ -154,6 +157,15 @@ func TestParseIndex(t *testing.T) {
Field: &schema.Field{Name: "Data2B"},
}},
},
"idx_user_indices_idx_compname_1": {
Class: "UNIQUE",
Name: "idx_user_indices_idx_compname_1",
Option: "NULLS NOT DISTINCT",
Fields: []schema.IndexOption{
{Field: &schema.Field{Name: "CompName1", NotNull: true}},
{Field: &schema.Field{Name: "CompName2"}},
},
},
}

CheckIndices(t, results, user.ParseIndexes())
Expand Down Expand Up @@ -253,7 +265,7 @@ func CheckIndices(t *testing.T, expected, actual map[string]schema.Index) {
}
for i, ef := range ei.Fields {
af := ai.Fields[i]
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length")
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length", "NotNull")
}
})
delete(actual, k)
Expand Down
Loading