Skip to content

Commit

Permalink
Change ClearEmail to use a direct UPDATE query (#7724)
Browse files Browse the repository at this point in the history
Change ClearEmail to use a direct `UPDATE` query instead of
`tx.Update()`, in order to avoid `LockCol` issues.

Part of #7716
  • Loading branch information
jprenken committed Sep 27, 2024
1 parent 8dac30f commit 267c82d
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions sa/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,33 @@ func ClearEmail(ctx context.Context, dbMap db.DatabaseMap, regID int64, email st
return nil, nil
}

currPb.Contact = newContacts
newModel, err := registrationPbToModel(currPb)
// We don't want to write literal JSON "null" strings into the database if the
// list of contact addresses is empty. Replace any possibly-`nil` slice with
// an empty JSON array. We don't need to check reg.ContactPresent, because
// we're going to write the whole object to the database anyway.
jsonContact := []byte("[]")
if len(newContacts) != 0 {
jsonContact, err = json.Marshal(newContacts)
if err != nil {
return nil, err
}
}

// UPDATE the row with a direct database query, in order to avoid LockCol issues.
result, err := tx.ExecContext(ctx,
"UPDATE registrations SET contact = ? WHERE id = ? LIMIT 1",
jsonContact,
regID,
)
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
return nil, berrors.InternalServerError("no registration updated with new contact field")
}

return tx.Update(ctx, newModel)
return nil, nil
})
if overallError != nil {
return overallError
Expand Down

0 comments on commit 267c82d

Please sign in to comment.