refactor: distinguish between UniqueIndex and Index

This commit is contained in:
black 2023-06-07 19:16:57 +08:00
parent 661781a3d7
commit d6de263c4a
4 changed files with 7 additions and 6 deletions

View File

@ -488,7 +488,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
}
// check unique
if unique, ok := columnType.Unique(); ok && unique != field.Unique {
if unique, ok := columnType.Unique(); ok && !unique && (field.Unique || field.UniqueIndex) {
// not primary key
if !field.PrimaryKey {
alterColumn = true

View File

@ -69,6 +69,7 @@ type Field struct {
DefaultValueInterface interface{}
NotNull bool
Unique bool
UniqueIndex bool
Comment string
Size int
Precision int

View File

@ -67,7 +67,7 @@ func (schema *Schema) ParseIndexes() map[string]Index {
}
for _, index := range indexes {
if index.Class == "UNIQUE" && len(index.Fields) == 1 {
index.Fields[0].Field.Unique = true
index.Fields[0].Field.UniqueIndex = true
}
}
return indexes

View File

@ -65,7 +65,7 @@ func TestParseIndex(t *testing.T) {
"idx_name": {
Name: "idx_name",
Class: "UNIQUE",
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", Unique: true}}},
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", UniqueIndex: true}}},
},
"idx_user_indices_name3": {
Name: "idx_user_indices_name3",
@ -81,7 +81,7 @@ func TestParseIndex(t *testing.T) {
"idx_user_indices_name4": {
Name: "idx_user_indices_name4",
Class: "UNIQUE",
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", Unique: true}}},
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", UniqueIndex: true}}},
},
"idx_user_indices_name5": {
Name: "idx_user_indices_name5",
@ -102,12 +102,12 @@ func TestParseIndex(t *testing.T) {
},
"idx_id": {
Name: "idx_id",
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", Unique: true}}},
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", UniqueIndex: true}}},
},
"idx_oid": {
Name: "idx_oid",
Class: "UNIQUE",
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", Unique: true}}},
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", UniqueIndex: true}}},
},
"type": {
Name: "type",