Fix Save with stress tests

This commit is contained in:
Jinzhu 2023-03-02 17:22:42 +08:00
parent 877cc9148f
commit f3874339ef
4 changed files with 12 additions and 10 deletions

View File

@ -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 {
if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate {
return tx.Create(value)
}
}
return updateTx
}
return

2
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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