From d6de263c4ae78353c8228a40d8ce793eb4b6cff3 Mon Sep 17 00:00:00 2001 From: black Date: Wed, 7 Jun 2023 19:16:57 +0800 Subject: [PATCH] refactor: distinguish between UniqueIndex and Index --- migrator/migrator.go | 2 +- schema/field.go | 1 + schema/index.go | 2 +- schema/index_test.go | 8 ++++---- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index de60f91c..10e8d12f 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -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 diff --git a/schema/field.go b/schema/field.go index dd08e056..c25a7ec0 100644 --- a/schema/field.go +++ b/schema/field.go @@ -69,6 +69,7 @@ type Field struct { DefaultValueInterface interface{} NotNull bool Unique bool + UniqueIndex bool Comment string Size int Precision int diff --git a/schema/index.go b/schema/index.go index f5ac5dd2..e7b3eca4 100644 --- a/schema/index.go +++ b/schema/index.go @@ -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 diff --git a/schema/index_test.go b/schema/index_test.go index 890327de..2aca1f1a 100644 --- a/schema/index_test.go +++ b/schema/index_test.go @@ -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",