Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: distinguish between Unique and UniqueIndex #6386

Merged
merged 20 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: distinguish between UniqueIndex and Index
  • Loading branch information
black-06 committed Jun 7, 2023
commit d6de263c4ae78353c8228a40d8ce793eb4b6cff3
2 changes: 1 addition & 1 deletion migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Field struct {
DefaultValueInterface interface{}
NotNull bool
Unique bool
UniqueIndex bool
Comment string
Size int
Precision int
Expand Down
2 changes: 1 addition & 1 deletion schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions schema/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down