Skip to content

Commit

Permalink
Address feedback & fix JSON data type
Browse files Browse the repository at this point in the history
  • Loading branch information
jprenken committed Sep 26, 2024
1 parent 2768ac0 commit 32d5847
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions sa/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,33 @@ func ClearEmail(ctx context.Context, dbMap db.DatabaseMap, regID int64, email st
return nil, nil
}

// 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.
//
// TODO(#7716): Revert to use tx.Update() once LockCol has been removed.
_, err = dbMap.ExecContext(ctx,
result, err := tx.ExecContext(ctx,
"UPDATE registrations SET contact = ? WHERE id = ? LIMIT 1",
newContacts,
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 nil, err
return nil, nil
})

if overallError != nil {
Expand Down

0 comments on commit 32d5847

Please sign in to comment.