Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow customizing the time used for CreatedAt/UpdatedAt #708

Merged
merged 1 commit into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions executors_test.go
Expand Up @@ -1964,3 +1964,28 @@ func Test_Delete(t *testing.T) {
r.Equal(count, ctx)
})
}

func Test_Create_Timestamps_With_NowFunc(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)

originalNowFunc := nowFunc
// ensure the original function is restored
defer func() {
nowFunc = originalNowFunc
}()

fakeNow, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
SetNowFunc(func() time.Time { return fakeNow })

friend:= Friend{FirstName: "Yester", LastName: "Day"}
err := tx.Create(&friend)
r.NoError(err)

r.Equal(fakeNow, friend.CreatedAt)
r.Equal(fakeNow, friend.UpdatedAt)
})
}
5 changes: 5 additions & 0 deletions model.go
Expand Up @@ -15,6 +15,11 @@ import (

var nowFunc = time.Now

// SetNowFunc allows an override of time.Now for customizing CreatedAt/UpdatedAt
func SetNowFunc(f func() time.Time) {
nowFunc = f
}

// Value is the contents of a `Model`.
type Value interface{}

Expand Down