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 HasIndex method for dialect interface
- Loading branch information
Showing
10 changed files
with
71 additions
and
15 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
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
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
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
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
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 |
---|---|---|
|
@@ -30,22 +30,43 @@ func TestIndexes(t *testing.T) { | |
t.Errorf("Got error when tried to create index: %+v", err) | ||
} | ||
|
||
scope := DB.NewScope(&Email{}) | ||
if !scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email") { | ||
t.Errorf("Email should have index idx_email_email") | ||
} | ||
|
||
if err := DB.Model(&Email{}).RemoveIndex("idx_email_email").Error; err != nil { | ||
t.Errorf("Got error when tried to remove index: %+v", err) | ||
} | ||
|
||
if scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email") { | ||
t.Errorf("Email's index idx_email_email should be deleted") | ||
} | ||
|
||
if err := DB.Model(&Email{}).AddIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil { | ||
t.Errorf("Got error when tried to create index: %+v", err) | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email_and_user_id") { | ||
t.Errorf("Email should have index idx_email_email_and_user_id") | ||
} | ||
|
||
if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil { | ||
t.Errorf("Got error when tried to remove index: %+v", err) | ||
} | ||
|
||
if scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email_and_user_id") { | ||
t.Errorf("Email's index idx_email_email_and_user_id should be deleted") | ||
} | ||
|
||
if err := DB.Model(&Email{}).AddUniqueIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil { | ||
t.Errorf("Got error when tried to create index: %+v", err) | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email_and_user_id") { | ||
t.Errorf("Email should have index idx_email_email_and_user_id") | ||
} | ||
|
||
if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}, {Email: "[email protected]"}}}).Error == nil { | ||
t.Errorf("Should get to create duplicate record when having unique index") | ||
} | ||
|
@@ -54,6 +75,10 @@ func TestIndexes(t *testing.T) { | |
t.Errorf("Got error when tried to remove index: %+v", err) | ||
} | ||
|
||
if scope.Dialect().HasIndex(scope, scope.TableName(), "idx_email_email_and_user_id") { | ||
t.Errorf("Email's index idx_email_email_and_user_id should be deleted") | ||
} | ||
|
||
if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}}}).Error != nil { | ||
t.Errorf("Should be able to create duplicated emails after remove unique index") | ||
} | ||
|
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
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
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
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