Skip to content

FIX #7427 Optimistic Lock in BeforeUpdate: PK Condition Placement Affecting DB Plan Efficiency #7428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion callbacks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
switch updatingValue.Kind() {
case reflect.Struct:
set = make([]clause.Assignment, 0, len(stmt.Schema.FieldsByDBName))
priExpr := make([]clause.Expression, 0)
for _, dbName := range stmt.Schema.DBNames {
if field := updatingSchema.LookUpField(dbName); field != nil {
if !field.PrimaryKey || !updatingValue.CanAddr() || stmt.Dest != stmt.Model {
Expand Down Expand Up @@ -290,11 +291,26 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
}
} else {
if value, isZero := field.ValueOf(stmt.Context, updatingValue); !isZero {
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Eq{Column: field.DBName, Value: value}}})
// stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.Eq{Column: field.DBName, Value: value}}})
priExpr = append(priExpr, clause.Eq{Column: field.DBName, Value: value})

}
}
}
}
if len(priExpr) > 0 {
where := clause.Where{Exprs: priExpr}
wname := where.Name()
existWc := stmt.Clauses[wname]
existWc.Name = wname
if existingWhere, ok := existWc.Expression.(clause.Where); ok {
where.Exprs = append(priExpr, existingWhere.Exprs...)
existWc.Expression = where
stmt.Clauses[wname] = existWc
}
existWc.Expression = where
stmt.Clauses[wname] = existWc
}
default:
stmt.AddError(gorm.ErrInvalidData)
}
Expand Down
65 changes: 65 additions & 0 deletions tests/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,68 @@ func TestUpdateFrom(t *testing.T) {
}
}
}

type Hzw struct {
Id int32 `gorm:"column:ID;primarykey"`
Type int32 `gorm:"column:TYPE;primarykey"`
Name string `gorm:"column:NAME;size:100;not null"`
Version int32 `gorm:"column:VERSION;default:0"`
}

func (h *Hzw) BeforeUpdate(tx *gorm.DB) (err error) {
cv := h.Version
h.Version++
nExprs := tx.Statement.BuildCondition("VERSION", cv)
newwhere := clause.Where{Exprs: nExprs}
tx.Statement.AddClause(newwhere)
return nil
}

func TestUpdateHookOptimisticLock(t *testing.T) {
hzw := &Hzw{
Id: 1,
Type: 2,
Name: "hzw",
Version: 0,
}

statement := DB.Session(&gorm.Session{DryRun: true}).Save(hzw).Statement
sqlstr := statement.SQL.String()

// Find the WHERE clause
whereIndex := strings.Index(strings.ToUpper(sqlstr), "WHERE")
if whereIndex == -1 {
t.Errorf("No WHERE clause found in the SQL statement")
return
}
whereClause := sqlstr[whereIndex+len("WHERE"):]

// Define the expected order of conditions
expectedOrder := []string{"ID", "TYPE", "VERSION"}

// Use regular expression to match column names
re := regexp.MustCompile(`\b(?:ID|TYPE|VERSION)\b`)
matches := re.FindAllString(whereClause, -1)

if len(matches) < len(expectedOrder) {
t.Errorf("The actual number of WHERE conditions is less than the expected number")
return
}

for i, expected := range expectedOrder {
if strings.ToUpper(matches[i]) != expected {
t.Errorf("The order of WHERE conditions is incorrect. Expected %s at position %d, but got %s", expected, i+1, matches[i])
return
}
}

vars := statement.Vars
cversion := vars[4]
vversion := vars[1]
if cversion != int32(0) {
t.Fatalf("current VERSION should be 0")
}
if vversion != int32(1) {
t.Fatalf("value VERSION should be 1")
}
}
Loading