about summary refs log tree commit diff
path: root/app/models/canonical_email_block.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2021-11-24 17:41:03 +0100
committerGitHub <noreply@github.com>2021-11-24 17:41:03 +0100
commit02a87431cf260e466ff58cc159b52cbce9aaef6b (patch)
treea67a63815f79aea7745ed759f8ba1a2915c622b8 /app/models/canonical_email_block.rb
parent9c44cf205f098ef499854bb52b698b594807b5c7 (diff)
Fix error when suspending user with an already-existing canonical email block (#17036)
* Fix error when suspending user with an already-existing canonical email block

Fixes #17033

While attempting to create a `CanonicalEmailBlock` with an existing hash would
raise an `ActiveRecord::RecordNotUnique` error, this being done within a
transaction would cancel the whole transaction. For this reason, checking for
uniqueness in Rails would query the database within the transaction and avoid
invalidating the whole transaction for this reason.

A race condition is still possible, where multiple accounts sharing a canonical
email would be blocked in concurrent transactions, in which only one would
succeed, but that is way less likely to happen that the current issue, and can
always be retried after the first failure, unlike the current situation.

* Add tests
Diffstat (limited to 'app/models/canonical_email_block.rb')
-rw-r--r--app/models/canonical_email_block.rb2
1 files changed, 1 insertions, 1 deletions
diff --git a/app/models/canonical_email_block.rb b/app/models/canonical_email_block.rb
index a8546d65a..be8c45bfe 100644
--- a/app/models/canonical_email_block.rb
+++ b/app/models/canonical_email_block.rb
@@ -15,7 +15,7 @@ class CanonicalEmailBlock < ApplicationRecord
 
   belongs_to :reference_account, class_name: 'Account'
 
-  validates :canonical_email_hash, presence: true
+  validates :canonical_email_hash, presence: true, uniqueness: true
 
   def email=(email)
     self.canonical_email_hash = email_to_canonical_email_hash(email)