Skip to content

Commit

Permalink
index: add composite id (#5269)
Browse files Browse the repository at this point in the history
* index: add composite id

* index: add test cases of composite id

* index: improve the comments for the test cases of composite id
  • Loading branch information
photon3108 committed Apr 25, 2022
1 parent a0cc631 commit 0211ac9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
26 changes: 23 additions & 3 deletions schema/index.go
@@ -1,6 +1,7 @@
package schema

import (
"fmt"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -31,7 +32,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {

for _, field := range schema.Fields {
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" {
for _, index := range parseFieldIndexes(field) {
fieldIndexes, err := parseFieldIndexes(field)
if err != nil {
schema.err = err
break
}
for _, index := range fieldIndexes {
idx := indexes[index.Name]
idx.Name = index.Name
if idx.Class == "" {
Expand Down Expand Up @@ -82,7 +88,7 @@ func (schema *Schema) LookIndex(name string) *Index {
return nil
}

func parseFieldIndexes(field *Field) (indexes []Index) {
func parseFieldIndexes(field *Field) (indexes []Index, err error) {
for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
if value != "" {
v := strings.Split(value, ":")
Expand All @@ -106,7 +112,20 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
}

if name == "" {
name = field.Schema.namer.IndexName(field.Schema.Table, field.Name)
subName := field.Name
const key = "COMPOSITE"
if composite, found := settings[key]; found {
if len(composite) == 0 || composite == key {
err = fmt.Errorf(
"The composite tag of %s.%s cannot be empty",
field.Schema.Name,
field.Name)
return
}
subName = composite
}
name = field.Schema.namer.IndexName(
field.Schema.Table, subName)
}

if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
Expand Down Expand Up @@ -138,5 +157,6 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
}
}

err = nil
return
}
60 changes: 60 additions & 0 deletions schema/index_test.go
Expand Up @@ -19,6 +19,36 @@ type UserIndex struct {
OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
MemberNumber string `gorm:"index:idx_id,priority:1"`
Name7 string `gorm:"index:type"`

// Composite Index: Flattened structure.
Data0A string `gorm:"index:,composite:comp_id0"`
Data0B string `gorm:"index:,composite:comp_id0"`

// Composite Index: Nested structure.
Data1A string `gorm:"index:,composite:comp_id1"`
CompIdxLevel1C

// Composite Index: Unique and priority.
Data2A string `gorm:"index:,unique,composite:comp_id2,priority:2"`
CompIdxLevel2C
}

type CompIdxLevel1C struct {
CompIdxLevel1B
Data1C string `gorm:"index:,composite:comp_id1"`
}

type CompIdxLevel1B struct {
Data1B string `gorm:"index:,composite:comp_id1"`
}

type CompIdxLevel2C struct {
CompIdxLevel2B
Data2C string `gorm:"index:,unique,composite:comp_id2,priority:1"`
}

type CompIdxLevel2B struct {
Data2B string `gorm:"index:,unique,composite:comp_id2,priority:3"`
}

func TestParseIndex(t *testing.T) {
Expand Down Expand Up @@ -84,6 +114,36 @@ func TestParseIndex(t *testing.T) {
Type: "",
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name7"}}},
},
"idx_user_indices_comp_id0": {
Name: "idx_user_indices_comp_id0",
Type: "",
Fields: []schema.IndexOption{{
Field: &schema.Field{Name: "Data0A"},
}, {
Field: &schema.Field{Name: "Data0B"},
}},
},
"idx_user_indices_comp_id1": {
Name: "idx_user_indices_comp_id1",
Fields: []schema.IndexOption{{
Field: &schema.Field{Name: "Data1A"},
}, {
Field: &schema.Field{Name: "Data1B"},
}, {
Field: &schema.Field{Name: "Data1C"},
}},
},
"idx_user_indices_comp_id2": {
Name: "idx_user_indices_comp_id2",
Class: "UNIQUE",
Fields: []schema.IndexOption{{
Field: &schema.Field{Name: "Data2C"},
}, {
Field: &schema.Field{Name: "Data2A"},
}, {
Field: &schema.Field{Name: "Data2B"},
}},
},
}

indices := user.ParseIndexes()
Expand Down

0 comments on commit 0211ac9

Please sign in to comment.