Add option tag support for index
This commit is contained in:
		
							parent
							
								
									635dcc9ad4
								
							
						
					
					
						commit
						5fee5b1b24
					
				| @ -188,7 +188,13 @@ func (m Migrator) CreateTable(values ...interface{}) error { | ||||
| 					if idx.Class != "" { | ||||
| 						createTableSQL += idx.Class + " " | ||||
| 					} | ||||
| 					createTableSQL += "INDEX ? ?," | ||||
| 					createTableSQL += "INDEX ? ?" | ||||
| 
 | ||||
| 					if idx.Option != "" { | ||||
| 						createTableSQL += " " + idx.Option | ||||
| 					} | ||||
| 
 | ||||
| 					createTableSQL += "," | ||||
| 					values = append(values, clause.Expr{SQL: idx.Name}, tx.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)) | ||||
| 				} | ||||
| 			} | ||||
| @ -543,6 +549,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error { | ||||
| 				createIndexSQL += " USING " + idx.Type | ||||
| 			} | ||||
| 
 | ||||
| 			if idx.Option != "" { | ||||
| 				createIndexSQL += " " + idx.Option | ||||
| 			} | ||||
| 
 | ||||
| 			return m.DB.Exec(createIndexSQL, values...).Error | ||||
| 		} | ||||
| 
 | ||||
|  | ||||
| @ -12,6 +12,7 @@ type Index struct { | ||||
| 	Type    string // btree, hash, gist, spgist, gin, and brin
 | ||||
| 	Where   string | ||||
| 	Comment string | ||||
| 	Option  string // WITH PARSER parser_name
 | ||||
| 	Fields  []IndexOption | ||||
| } | ||||
| 
 | ||||
| @ -45,6 +46,9 @@ func (schema *Schema) ParseIndexes() map[string]Index { | ||||
| 				if idx.Comment == "" { | ||||
| 					idx.Comment = index.Comment | ||||
| 				} | ||||
| 				if idx.Option == "" { | ||||
| 					idx.Option = index.Option | ||||
| 				} | ||||
| 
 | ||||
| 				idx.Fields = append(idx.Fields, index.Fields...) | ||||
| 				sort.Slice(idx.Fields, func(i, j int) bool { | ||||
| @ -119,6 +123,7 @@ func parseFieldIndexes(field *Field) (indexes []Index) { | ||||
| 					Type:    settings["TYPE"], | ||||
| 					Where:   settings["WHERE"], | ||||
| 					Comment: settings["COMMENT"], | ||||
| 					Option:  settings["OPTION"], | ||||
| 					Fields: []IndexOption{{ | ||||
| 						Field:      field, | ||||
| 						Expression: settings["EXPRESSION"], | ||||
|  | ||||
| @ -15,7 +15,7 @@ type UserIndex struct { | ||||
| 	Name4        string `gorm:"uniqueIndex"` | ||||
| 	Name5        int64  `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"` | ||||
| 	Name6        int64  `gorm:"index:profile,comment:hello \\, world,where:age > 10"` | ||||
| 	Age          int64  `gorm:"index:profile,expression:ABS(age)"` | ||||
| 	Age          int64  `gorm:"index:profile,expression:ABS(age),option:WITH PARSER parser_name"` | ||||
| 	OID          int64  `gorm:"index:idx_id;index:idx_oid,unique"` | ||||
| 	MemberNumber string `gorm:"index:idx_id,priority:1"` | ||||
| } | ||||
| @ -63,6 +63,7 @@ func TestParseIndex(t *testing.T) { | ||||
| 			Name:    "profile", | ||||
| 			Comment: "hello , world", | ||||
| 			Where:   "age > 10", | ||||
| 			Option:  "WITH PARSER parser_name", | ||||
| 			Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, { | ||||
| 				Field:      &schema.Field{Name: "Age"}, | ||||
| 				Expression: "ABS(age)", | ||||
| @ -87,7 +88,7 @@ func TestParseIndex(t *testing.T) { | ||||
| 			t.Fatalf("Failed to found index %v from parsed indices %+v", k, indices) | ||||
| 		} | ||||
| 
 | ||||
| 		for _, name := range []string{"Name", "Class", "Type", "Where", "Comment"} { | ||||
| 		for _, name := range []string{"Name", "Class", "Type", "Where", "Comment", "Option"} { | ||||
| 			if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() { | ||||
| 				t.Errorf( | ||||
| 					"index %v %v should equal, expects %v, got %v", | ||||
|  | ||||
| @ -7,7 +7,7 @@ require ( | ||||
| 	github.com/jinzhu/now v1.1.1 | ||||
| 	github.com/lib/pq v1.6.0 | ||||
| 	gorm.io/driver/mysql v1.0.2 | ||||
| 	gorm.io/driver/postgres v1.0.3 | ||||
| 	gorm.io/driver/postgres v1.0.4 | ||||
| 	gorm.io/driver/sqlite v1.1.3 | ||||
| 	gorm.io/driver/sqlserver v1.0.5 | ||||
| 	gorm.io/gorm v1.20.2 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu