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 // 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 // not primary key
if !field.PrimaryKey { if !field.PrimaryKey {
alterColumn = true alterColumn = true

View File

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

View File

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

View File

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