Optimisticlock
Optimisticlock is optimistic lock plugin for gorm.
Usage
import "gorm.io/plugin/optimisticlock"
type User struct {
ID int
Name string
Age uint
Version optimisticlock.Version
}
var user User
DB.First(&user)
DB.Model(&user).Update("age", 18)
DB.Model(&user).Updates(&User{Age: 18, Version: optimisticlock.Version{Int64: 1}})
DB.Model(&User{}).Where("id = 1").Update("age", 12)
DB.Transaction(func(tx *gorm.DB) error {
tx.First(&user)
user.Age = 18
rows = tx.Updates(&user).RowsAffected
user.Age = 19
rows = tx.Updates(&user).RowsAffected
return nil
})