2020-02-21 18:51:38 +03:00
|
|
|
package schema_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/schema"
|
2020-02-21 18:51:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserIndex struct {
|
2020-04-03 02:57:52 +03:00
|
|
|
Name string `gorm:"index"`
|
|
|
|
Name2 string `gorm:"index:idx_name,unique"`
|
|
|
|
Name3 string `gorm:"index:,sort:desc,collate:utf8,type:btree,length:10,where:name3 != 'jinzhu'"`
|
2020-07-03 19:36:27 +03:00
|
|
|
Name4 string `gorm:"uniqueIndex"`
|
2020-04-03 02:57:52 +03:00
|
|
|
Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"`
|
|
|
|
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
|
2020-10-21 15:15:49 +03:00
|
|
|
Age int64 `gorm:"index:profile,expression:ABS(age),option:WITH PARSER parser_name"`
|
2020-07-01 06:47:46 +03:00
|
|
|
OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
|
2020-07-08 12:59:40 +03:00
|
|
|
MemberNumber string `gorm:"index:idx_id,priority:1"`
|
2022-03-14 16:47:59 +03:00
|
|
|
Name7 string `gorm:"index:type"`
|
2022-04-25 06:39:23 +03:00
|
|
|
|
|
|
|
// 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"`
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIndex(t *testing.T) {
|
2020-02-24 03:51:35 +03:00
|
|
|
user, err := schema.Parse(&UserIndex{}, &sync.Map{}, schema.NamingStrategy{})
|
2020-02-21 18:51:38 +03:00
|
|
|
if err != nil {
|
2020-02-22 14:41:01 +03:00
|
|
|
t.Fatalf("failed to parse user index, got error %v", err)
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
results := map[string]schema.Index{
|
|
|
|
"idx_user_indices_name": {
|
|
|
|
Name: "idx_user_indices_name",
|
2020-07-08 12:59:40 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name"}}},
|
2020-02-21 18:51:38 +03:00
|
|
|
},
|
|
|
|
"idx_name": {
|
|
|
|
Name: "idx_name",
|
|
|
|
Class: "UNIQUE",
|
2023-01-11 09:05:39 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", Unique: true}}},
|
2020-02-21 18:51:38 +03:00
|
|
|
},
|
|
|
|
"idx_user_indices_name3": {
|
2020-02-21 19:02:05 +03:00
|
|
|
Name: "idx_user_indices_name3",
|
|
|
|
Type: "btree",
|
|
|
|
Where: "name3 != 'jinzhu'",
|
2020-02-21 18:51:38 +03:00
|
|
|
Fields: []schema.IndexOption{{
|
2020-07-08 12:59:40 +03:00
|
|
|
Field: &schema.Field{Name: "Name3"},
|
2020-02-21 18:51:38 +03:00
|
|
|
Sort: "desc",
|
|
|
|
Collate: "utf8",
|
|
|
|
Length: 10,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"idx_user_indices_name4": {
|
|
|
|
Name: "idx_user_indices_name4",
|
|
|
|
Class: "UNIQUE",
|
2023-01-11 09:05:39 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", Unique: true}}},
|
2020-02-21 18:51:38 +03:00
|
|
|
},
|
|
|
|
"idx_user_indices_name5": {
|
2020-02-21 19:02:05 +03:00
|
|
|
Name: "idx_user_indices_name5",
|
|
|
|
Class: "FULLTEXT",
|
|
|
|
Comment: "hello , world",
|
|
|
|
Where: "age > 10",
|
2020-07-08 12:59:40 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name5"}}},
|
2020-02-21 18:51:38 +03:00
|
|
|
},
|
|
|
|
"profile": {
|
2020-02-21 19:02:05 +03:00
|
|
|
Name: "profile",
|
|
|
|
Comment: "hello , world",
|
|
|
|
Where: "age > 10",
|
2020-10-21 15:15:49 +03:00
|
|
|
Option: "WITH PARSER parser_name",
|
2020-07-08 12:59:40 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, {
|
|
|
|
Field: &schema.Field{Name: "Age"},
|
2020-02-22 06:15:51 +03:00
|
|
|
Expression: "ABS(age)",
|
2020-02-21 18:51:38 +03:00
|
|
|
}},
|
|
|
|
},
|
2020-04-03 02:57:52 +03:00
|
|
|
"idx_id": {
|
|
|
|
Name: "idx_id",
|
2023-01-11 09:05:39 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", Unique: true}}},
|
2020-04-03 02:57:52 +03:00
|
|
|
},
|
2020-07-01 06:47:46 +03:00
|
|
|
"idx_oid": {
|
|
|
|
Name: "idx_oid",
|
|
|
|
Class: "UNIQUE",
|
2023-01-11 09:05:39 +03:00
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", Unique: true}}},
|
2020-07-01 06:47:46 +03:00
|
|
|
},
|
2022-03-14 16:47:59 +03:00
|
|
|
"type": {
|
|
|
|
Name: "type",
|
|
|
|
Type: "",
|
|
|
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name7"}}},
|
|
|
|
},
|
2022-04-25 06:39:23 +03:00
|
|
|
"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"},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
indices := user.ParseIndexes()
|
|
|
|
|
|
|
|
for k, result := range results {
|
|
|
|
v, ok := indices[k]
|
|
|
|
if !ok {
|
2020-04-03 02:57:52 +03:00
|
|
|
t.Fatalf("Failed to found index %v from parsed indices %+v", k, indices)
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:15:49 +03:00
|
|
|
for _, name := range []string{"Name", "Class", "Type", "Where", "Comment", "Option"} {
|
2020-02-21 19:02:05 +03:00
|
|
|
if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() {
|
|
|
|
t.Errorf(
|
|
|
|
"index %v %v should equal, expects %v, got %v",
|
|
|
|
k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(),
|
|
|
|
)
|
|
|
|
}
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx, ef := range result.Fields {
|
|
|
|
rf := v.Fields[idx]
|
2020-07-08 12:59:40 +03:00
|
|
|
if rf.Field.Name != ef.Field.Name {
|
|
|
|
t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name)
|
|
|
|
}
|
2023-01-11 09:05:39 +03:00
|
|
|
if rf.Field.Unique != ef.Field.Unique {
|
|
|
|
t.Fatalf("index field '%s' should equal, expects %v, got %v", rf.Field.Name, rf.Field.Unique, ef.Field.Unique)
|
|
|
|
}
|
2020-07-08 12:59:40 +03:00
|
|
|
|
2020-02-21 19:02:05 +03:00
|
|
|
for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
|
2020-02-21 18:51:38 +03:00
|
|
|
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {
|
2020-02-21 19:02:05 +03:00
|
|
|
t.Errorf(
|
|
|
|
"index %v field #%v's %v should equal, expects %v, got %v", k, idx+1, name,
|
|
|
|
reflect.ValueOf(ef).FieldByName(name).Interface(), reflect.ValueOf(rf).FieldByName(name).Interface(),
|
|
|
|
)
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|