forked from jinzhu/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.
- Loading branch information
Showing
9 changed files
with
73 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -821,11 +821,11 @@ func BenchmarkGorm(b *testing.B) { | |
for x := 0; x < b.N; x++ { | ||
e := strconv.Itoa(x) + "[email protected]" | ||
now := time.Now() | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
email := EmailWithIdx{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
// Insert | ||
DB.Save(&email) | ||
// Query | ||
DB.First(&BigEmail{}, "email = ?", e) | ||
DB.First(&EmailWithIdx{}, "email = ?", e) | ||
// Update | ||
DB.Model(&email).UpdateColumn("email", "new-"+e) | ||
// Delete | ||
|
@@ -846,7 +846,7 @@ func BenchmarkRawSql(b *testing.B) { | |
var id int64 | ||
e := strconv.Itoa(x) + "[email protected]" | ||
now := time.Now() | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
email := EmailWithIdx{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
// Insert | ||
DB.QueryRow(insertSql, email.UserId, email.Email, email.UserAgent, email.RegisteredAt, time.Now(), time.Now()).Scan(&id) | ||
// Query | ||
|
@@ -860,6 +860,6 @@ func BenchmarkRawSql(b *testing.B) { | |
} | ||
|
||
func parseTime(str string) *time.Time { | ||
t := now.MustParse(str) | ||
t := now.New(time.Now().UTC()).MustParse(str) | ||
return &t | ||
} |
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 |
---|---|---|
|
@@ -31,9 +31,8 @@ type User struct { | |
Languages []Language `gorm:"many2many:user_languages;"` | ||
CompanyID *int | ||
Company Company | ||
Role | ||
Role Role | ||
PasswordHash []byte | ||
Sequence uint `gorm:"AUTO_INCREMENT"` | ||
IgnoreMe int64 `sql:"-"` | ||
IgnoreStringSlice []string `sql:"-"` | ||
Ignored struct{ Name string } `sql:"-"` | ||
|
@@ -333,7 +332,7 @@ func TestIndexes(t *testing.T) { | |
} | ||
} | ||
|
||
type BigEmail struct { | ||
type EmailWithIdx struct { | ||
Id int64 | ||
UserId int64 | ||
Email string `sql:"index:idx_email_agent"` | ||
|
@@ -343,29 +342,26 @@ type BigEmail struct { | |
UpdatedAt time.Time | ||
} | ||
|
||
func (b BigEmail) TableName() string { | ||
return "emails" | ||
} | ||
|
||
func TestAutoMigration(t *testing.T) { | ||
DB.AutoMigrate(&Address{}) | ||
if err := DB.Table("emails").AutoMigrate(&BigEmail{}).Error; err != nil { | ||
DB.DropTable(&EmailWithIdx{}) | ||
if err := DB.AutoMigrate(&EmailWithIdx{}).Error; err != nil { | ||
t.Errorf("Auto Migrate should not raise any error") | ||
} | ||
|
||
now := time.Now() | ||
DB.Save(&BigEmail{Email: "[email protected]", UserAgent: "pc", RegisteredAt: &now}) | ||
DB.Save(&EmailWithIdx{Email: "[email protected]", UserAgent: "pc", RegisteredAt: &now}) | ||
|
||
scope := DB.NewScope(&BigEmail{}) | ||
scope := DB.NewScope(&EmailWithIdx{}) | ||
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_agent") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
if !scope.Dialect().HasIndex(scope.TableName(), "uix_emails_registered_at") { | ||
if !scope.Dialect().HasIndex(scope.TableName(), "uix_email_with_idxes_registered_at") { | ||
t.Errorf("Failed to create index") | ||
} | ||
|
||
var bigemail BigEmail | ||
var bigemail EmailWithIdx | ||
DB.First(&bigemail, "user_agent = ?", "pc") | ||
if bigemail.Email != "[email protected]" || bigemail.UserAgent != "pc" || bigemail.RegisteredAt.IsZero() { | ||
t.Error("Big Emails should be saved and fetched correctly") | ||
|
@@ -386,7 +382,7 @@ func TestMultipleIndexes(t *testing.T) { | |
} | ||
|
||
DB.AutoMigrate(&MultipleIndexes{}) | ||
if err := DB.AutoMigrate(&BigEmail{}).Error; err != nil { | ||
if err := DB.AutoMigrate(&EmailWithIdx{}).Error; err != nil { | ||
t.Errorf("Auto Migrate should not raise any error") | ||
} | ||
|
||
|
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