Skip to content
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

Change ClearEmail to use a direct UPDATE query #7724

Merged
merged 3 commits into from
Sep 27, 2024
Merged
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
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