Skip to content

Commit

Permalink
Bugs call Save method.
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomza committed Dec 13, 2018
1 parent da9a562 commit d9d233a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 7 additions & 2 deletions callbacks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package consistently

import (
"reflect"

"github.com/jinzhu/gorm"
)

Expand Down Expand Up @@ -32,9 +34,12 @@ func callbackConsistently(scope *gorm.Scope) {
saveVersion := field.Field.String()

if saveVersion != "" {
currentValue := scope.New(scope.Value)

scope.DB().First(currentValue.Value)
val := reflect.New(scope.IndirectValue().Type())

currentValue := scope.New(val.Interface())

scope.DB().First(currentValue.Value, scope.PrimaryKeyValue())

if scope.HasError() {
return
Expand Down
12 changes: 9 additions & 3 deletions consistently_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func TestGoConsistently(t *testing.T) {
t.Errorf("Must contain a version of the data")
}

user.Name = "gopa"

beforeUpdateVesrion := user.Version

errs := db.Model(&user).Update(&user).GetErrors()
errs := db.Save(&user).GetErrors()

errFound := false
for _, v := range errs {
Expand All @@ -50,6 +52,10 @@ func TestGoConsistently(t *testing.T) {
t.Errorf("Must not contain ErrVersionNotValid")
}

if user.Name != "gopa" {
t.Errorf("Must equal user.Name")
}

userCheck := User{}

db.First(&userCheck, user.ID)
Expand All @@ -60,7 +66,7 @@ func TestGoConsistently(t *testing.T) {

user.Version = "bad"

errs = db.Model(&user).Update(&user).GetErrors()
errs = db.Save(&user).GetErrors()

errFound = false
for _, v := range errs {
Expand Down Expand Up @@ -163,7 +169,7 @@ func TestGoConsistentlyAsync(t *testing.T) {
}

go handler(false, 2)
go handler(true, 3)
go handler(true, 4)

wg.Wait()
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module consistently

require (
cloud.google.com/go v0.33.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
Expand All @@ -11,7 +12,10 @@ require (
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/qor/qor v0.0.0-20180607095400-a9b667cbbf18
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // indirect
google.golang.org/appengine v1.3.0 // indirect
)

0 comments on commit d9d233a

Please sign in to comment.