Skip to content

Commit

Permalink
Pass Time structure into timestamp update functions. (#625)
Browse files Browse the repository at this point in the history
Closes #624
  • Loading branch information
mpontillo committed Mar 8, 2021
1 parent e314840 commit 46cfd45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
12 changes: 8 additions & 4 deletions executors.go
Expand Up @@ -2,6 +2,7 @@ package pop

import (
"reflect"
"time"

"github.com/gobuffalo/pop/v5/associations"
"github.com/gobuffalo/pop/v5/columns"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions model.go
Expand Up @@ -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
Expand All @@ -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:
Expand Down
24 changes: 8 additions & 16 deletions model_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 46cfd45

Please sign in to comment.