diff --git a/executors.go b/executors.go index 657ecea7..c382f260 100644 --- a/executors.go +++ b/executors.go @@ -2,6 +2,7 @@ package pop import ( "reflect" + "time" "github.com/gobuffalo/pop/v5/associations" "github.com/gobuffalo/pop/v5/columns" @@ -234,8 +235,9 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error { cols.Remove(excludeColumns...) } - m.touchCreatedAt() - m.touchUpdatedAt() + now := nowFunc().Truncate(time.Microsecond) + m.setUpdatedAt(now) + m.setCreatedAt(now) if err = c.Dialect.Create(c.Store, m, cols); err != nil { return err @@ -357,7 +359,8 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error { cols.Remove(excludeColumns...) } - m.touchUpdatedAt() + now := nowFunc().Truncate(time.Microsecond) + m.setUpdatedAt(now) if err = c.Dialect.Update(c.Store, m, cols); err != nil { return err @@ -401,7 +404,8 @@ func (c *Connection) UpdateColumns(model interface{}, columnNames ...string) err } cols.Remove("id", "created_at") - m.touchUpdatedAt() + now := nowFunc().Truncate(time.Microsecond) + m.setUpdatedAt(now) if err = c.Dialect.Update(c.Store, m, cols); err != nil { return err diff --git a/model.go b/model.go index 2b20dace..8b47e1be 100644 --- a/model.go +++ b/model.go @@ -190,10 +190,9 @@ func (m *Model) setID(i interface{}) { } } -func (m *Model) touchCreatedAt() { +func (m *Model) setCreatedAt(now time.Time) { fbn, err := m.fieldByName("CreatedAt") if err == nil { - now := nowFunc().Truncate(time.Microsecond) v := fbn.Interface() if !IsZeroOfUnderlyingType(v) { // Do not override already set CreatedAt @@ -208,10 +207,9 @@ func (m *Model) touchCreatedAt() { } } -func (m *Model) touchUpdatedAt() { +func (m *Model) setUpdatedAt(now time.Time) { fbn, err := m.fieldByName("UpdatedAt") if err == nil { - now := nowFunc().Truncate(time.Microsecond) v := fbn.Interface() switch v.(type) { case int, int64: diff --git a/model_test.go b/model_test.go index d0d0c23c..13a376fa 100644 --- a/model_test.go +++ b/model_test.go @@ -122,11 +122,9 @@ func Test_Touch_Time_Timestamp(t *testing.T) { // Override time.Now() t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") - nowFunc = func() time.Time { return t0 } - defer func() { nowFunc = time.Now }() - m.touchCreatedAt() - m.touchUpdatedAt() + m.setCreatedAt(t0) + m.setUpdatedAt(t0) v := m.Value.(*TimeTimestamp) r.Equal(t0, v.CreatedAt) r.Equal(t0, v.UpdatedAt) @@ -137,14 +135,12 @@ func Test_Touch_Time_Timestamp_With_Existing_Value(t *testing.T) { // Override time.Now() t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") - nowFunc = func() time.Time { return t0 } - defer func() { nowFunc = time.Now }() createdAt := nowFunc().Add(-36 * time.Hour) m := NewModel(&TimeTimestamp{CreatedAt: createdAt}, context.Background()) - m.touchCreatedAt() - m.touchUpdatedAt() + m.setCreatedAt(t0) + m.setUpdatedAt(t0) v := m.Value.(*TimeTimestamp) r.Equal(createdAt, v.CreatedAt) r.Equal(t0, v.UpdatedAt) @@ -157,11 +153,9 @@ func Test_Touch_Unix_Timestamp(t *testing.T) { // Override time.Now() t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") - nowFunc = func() time.Time { return t0 } - defer func() { nowFunc = time.Now }() - m.touchCreatedAt() - m.touchUpdatedAt() + m.setCreatedAt(t0) + m.setUpdatedAt(t0) v := m.Value.(*UnixTimestamp) r.Equal(int(t0.Unix()), v.CreatedAt) r.Equal(int(t0.Unix()), v.UpdatedAt) @@ -172,14 +166,12 @@ func Test_Touch_Unix_Timestamp_With_Existing_Value(t *testing.T) { // Override time.Now() t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z") - nowFunc = func() time.Time { return t0 } - defer func() { nowFunc = time.Now }() createdAt := int(time.Now().Add(-36 * time.Hour).Unix()) m := NewModel(&UnixTimestamp{CreatedAt: createdAt}, context.Background()) - m.touchCreatedAt() - m.touchUpdatedAt() + m.setCreatedAt(t0) + m.setUpdatedAt(t0) v := m.Value.(*UnixTimestamp) r.Equal(createdAt, v.CreatedAt) r.Equal(int(t0.Unix()), v.UpdatedAt)