From f3874339efd829d9841ad8fb6b50d7c2059153d2 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 2 Mar 2023 17:22:42 +0800 Subject: [PATCH] Fix Save with stress tests --- finisher_api.go | 11 +++++------ go.mod | 2 +- go.sum | 2 ++ tests/go.mod | 7 ++++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 39d9fca3..f16d4f43 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -101,14 +101,13 @@ func (db *DB) Save(value interface{}) (tx *DB) { tx.Statement.Selects = append(tx.Statement.Selects, "*") } - tx = tx.callbacks.Update().Execute(tx) + updateTx := tx.callbacks.Update().Execute(tx.Session(&Session{Initialized: true})) - if tx.Error == nil && tx.RowsAffected == 0 && !tx.DryRun && !selectedUpdate { - result := reflect.New(tx.Statement.Schema.ModelType).Interface() - if result := tx.Session(&Session{}).Limit(1).Find(result); result.RowsAffected == 0 { - return tx.Create(value) - } + if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate { + return tx.Create(value) } + + return updateTx } return diff --git a/go.mod b/go.mod index 03f84379..85e4242a 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/jinzhu/inflection v1.0.0 - github.com/jinzhu/now v1.1.4 + github.com/jinzhu/now v1.1.5 ) diff --git a/go.sum b/go.sum index 50fbba2f..fb4240eb 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,5 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= diff --git a/tests/go.mod b/tests/go.mod index 69d6cf87..b2d5ca97 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -4,12 +4,13 @@ go 1.16 require ( github.com/google/uuid v1.3.0 + github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jinzhu/now v1.1.5 github.com/lib/pq v1.10.7 github.com/mattn/go-sqlite3 v1.14.16 // indirect - golang.org/x/crypto v0.5.0 // indirect - gorm.io/driver/mysql v1.4.6 - gorm.io/driver/postgres v1.4.6 + github.com/microsoft/go-mssqldb v0.20.0 // indirect + gorm.io/driver/mysql v1.4.7 + gorm.io/driver/postgres v1.4.8 gorm.io/driver/sqlite v1.4.4 gorm.io/driver/sqlserver v1.4.2 gorm.io/gorm v1.24.5