forked from mirror/gorm
Fix Change UpdatedAt to current time when doing OnConflict UpdateAll
This commit is contained in:
parent
b864a5457a
commit
de850edb4f
|
@ -281,7 +281,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
||||||
} else if field.AutoUpdateTime > 0 {
|
} else if field.AutoUpdateTime > 0 {
|
||||||
if _, ok := stmt.DB.InstanceGet("gorm:update_track_time"); ok {
|
if _, ok := stmt.DB.InstanceGet("gorm:update_track_time"); ok {
|
||||||
field.Set(rv, curTime)
|
field.Set(rv, curTime)
|
||||||
values.Values[0][idx], _ = field.ValueOf(rv)
|
values.Values[i][idx], _ = field.ValueOf(rv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,8 +156,8 @@ func TestUpdates(t *testing.T) {
|
||||||
t.Errorf("User2's name should be updated")
|
t.Errorf("User2's name should be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if user2.UpdatedAt.Format(time.RFC1123) == user3.UpdatedAt.Format(time.RFC1123) {
|
if user2.UpdatedAt.Format(time.RFC1123Z) == user3.UpdatedAt.Format(time.RFC1123Z) {
|
||||||
t.Errorf("User's updated at should be changed, old %v, new %v", user2.UpdatedAt.Format(time.RFC1123), user3.UpdatedAt.Format(time.RFC1123))
|
t.Errorf("User's updated at should be changed, old %v, new %v", user2.UpdatedAt.Format(time.RFC1123Z), user3.UpdatedAt.Format(time.RFC1123Z))
|
||||||
}
|
}
|
||||||
|
|
||||||
// update with gorm exprs
|
// update with gorm exprs
|
||||||
|
@ -615,13 +615,28 @@ func TestSave(t *testing.T) {
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
user1UpdatedAt := result.UpdatedAt
|
user1UpdatedAt := result.UpdatedAt
|
||||||
|
user2UpdatedAt := user2.UpdatedAt
|
||||||
var users = []*User{&result, &user2}
|
var users = []*User{&result, &user2}
|
||||||
DB.Save(&users)
|
DB.Save(&users)
|
||||||
|
|
||||||
if user1UpdatedAt == result.UpdatedAt {
|
if user1UpdatedAt.Format(time.RFC1123Z) == result.UpdatedAt.Format(time.RFC1123Z) {
|
||||||
t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt)
|
t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if user2UpdatedAt.Format(time.RFC1123Z) == user2.UpdatedAt.Format(time.RFC1123Z) {
|
||||||
|
t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user2UpdatedAt, user2.UpdatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.First(&result)
|
||||||
|
if user1UpdatedAt.Format(time.RFC1123Z) == result.UpdatedAt.Format(time.RFC1123Z) {
|
||||||
|
t.Fatalf("user's updated at should be changed after reload, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.First(&user2)
|
||||||
|
if user2UpdatedAt.Format(time.RFC1123Z) == user2.UpdatedAt.Format(time.RFC1123Z) {
|
||||||
|
t.Fatalf("user2's updated at should be changed after reload, expects: %+v, got: %+v", user2UpdatedAt, user2.UpdatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
||||||
stmt := dryDB.Save(&user).Statement
|
stmt := dryDB.Save(&user).Statement
|
||||||
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {
|
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {
|
||||||
|
|
Loading…
Reference in New Issue