Skip to content

Commit

Permalink
Fix Save with stress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 2, 2023
1 parent 877cc91 commit f387433
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
11 changes: 5 additions & 6 deletions finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
7 changes: 4 additions & 3 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

3 comments on commit f387433

@lcgash
Copy link

@lcgash lcgash commented on f387433 Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么有这个修改呢,update 如果数据没有变化,直接插入了一条,这个太不符合实际使用场景了,这在数据上面不是幂等的

@lcgash
Copy link

@lcgash lcgash commented on f387433 Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么有这个修改呢,update 如果数据没有变化,直接插入了一条,这个太不符合实际使用场景了,这在数据上面不是幂等的

同时也是 主键 有值的情况,那么一定会报错导致程序失败

@jinzhu
Copy link
Member Author

@jinzhu jinzhu commented on f387433 Feb 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么有这个修改呢,update 如果数据没有变化,直接插入了一条,这个太不符合实际使用场景了,这在数据上面不是幂等的

同时也是 主键 有值的情况,那么一定会报错导致程序失败

如果确认只有更新行为的话,建议直接使用 Updates,Save 这个方法本身就有局限性,应该只用的在有正确的 unique index 不会导致有重复数据的场景下确保数据得到更新

Please sign in to comment.