forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple indexes seperated by commas for issue go-gor…
- Loading branch information
Michael Goff
authored and
Michael Goff
committed
Jun 15, 2016
1 parent
bf0e236
commit e7f00bd
Showing
2 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,3 +367,68 @@ func TestAutoMigration(t *testing.T) { | |
t.Error("Big Emails should be saved and fetched correctly") | ||
} | ||
} | ||
|
||
type MultipleIndexes struct { | ||
ID int64 | ||
UserID int64 `sql:"unique_index:uix_multipleindexes_user_name,uix_multipleindexes_user_email;index:idx_multipleindexes_user_other"` | ||
Name string `sql:"unique_index:uix_multipleindexes_user_name"` | ||
Email string `sql:"unique_index:,uix_multipleindexes_user_email"` | ||
Other string `sql:"index:,idx_multipleindexes_user_other"` | ||
} | ||
|
||
func TestMultipleIndexes(t *testing.T) { | ||
if err := DB.DropTableIfExists(&MultipleIndexes{}).Error; err != nil { | ||
fmt.Printf("Got error when try to delete table multiple_indexes, %+v\n", err) | ||
} | ||
|
||
DB.Debug().AutoMigrate(&MultipleIndexes{}) | ||
if err := DB.AutoMigrate(&BigEmail{}).Error; err != nil { | ||
t.Errorf("Auto Migrate should not raise any error") | ||
} | ||
|
||
DB.Save(&MultipleIndexes{UserID: 1, Name: "jinzhu", Email: "[email protected]", Other: "foo"}) | ||
|
||
scope := DB.NewScope(&MultipleIndexes{}) | ||
if !scope.Dialect().HasIndex(scope.TableName(), "uix_multipleindexes_user_name") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope.TableName(), "uix_multipleindexes_user_email") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope.TableName(), "uix_multiple_indexes_email") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope.TableName(), "idx_multipleindexes_user_other") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope.TableName(), "idx_multiple_indexes_other") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
var mutipleIndexes MultipleIndexes | ||
DB.First(&mutipleIndexes, "name = ?", "jinzhu") | ||
if mutipleIndexes.Email != "[email protected]" || mutipleIndexes.Name != "jinzhu" { | ||
t.Error("MutipleIndexes should be saved and fetched correctly") | ||
} | ||
|
||
// Check unique constraints | ||
if err := DB.Save(&MultipleIndexes{UserID: 1, Name: "name1", Email: "[email protected]", Other: "foo"}).Error; err == nil { | ||
t.Error("MultipleIndexes unique index failed") | ||
} | ||
|
||
if err := DB.Save(&MultipleIndexes{UserID: 1, Name: "name1", Email: "[email protected]", Other: "foo"}).Error; err != nil { | ||
t.Error("MultipleIndexes unique index failed") | ||
} | ||
|
||
if err := DB.Save(&MultipleIndexes{UserID: 2, Name: "name1", Email: "[email protected]", Other: "foo"}).Error; err == nil { | ||
t.Error("MultipleIndexes unique index failed") | ||
} | ||
|
||
if err := DB.Save(&MultipleIndexes{UserID: 2, Name: "name1", Email: "[email protected]", Other: "foo"}).Error; err != nil { | ||
t.Error("MultipleIndexes unique index failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters