Skip to content

Commit

Permalink
Improve tests and documentation for UpdateColumns function (#451)
Browse files Browse the repository at this point in the history
In the documentation it states that UpdateColumns updates the `updated_at` field automatically.
It is slightly confusing because you still need to include `updated_at` as a parameter for the function.
This commit tries to clarify the ambiguity and also adds a test for `updated_at` field
  • Loading branch information
mfkaptan authored and stanislas-m committed Oct 31, 2019
1 parent 32735d3 commit b5e47d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {

// UpdateColumns writes changes from an entry to the database, including only the given columns
// or all columns if no column names are provided.
// It updates the `updated_at` column automatically.
// It updates the `updated_at` column automatically if you include `updated_at` in columnNames.
//
// If model is a slice, each item of the slice is updated in the database.
func (c *Connection) UpdateColumns(model interface{}, columnNames ...string) error {
Expand Down
23 changes: 23 additions & 0 deletions executors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,29 @@ func Test_UpdateColumns(t *testing.T) {
})
}

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

user := User{Name: nulls.NewString("Foo")}
tx.Create(&user)

r.NotZero(user.CreatedAt)
r.NotZero(user.UpdatedAt)
updatedAtBefore := user.UpdatedAt

user.Name.String = "Bar"
err := tx.UpdateColumns(&user, "name", "updated_at") // Update name and updated_at
r.NoError(err)

r.NoError(tx.Reload(&user))
r.NotEqual(user.UpdatedAt, updatedAtBefore) // UpdatedAt should be updated automatically
})
}

func Test_UpdateColumns_MultipleColumns(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
Expand Down

0 comments on commit b5e47d5

Please sign in to comment.