Skip to content

Commit 99e2999

Browse files
authored
Merge pull request rails#48462 from iamradioactive/remove_additional_query
Do not revalidate encrypted attribute with current encrypted_type
2 parents 44d0859 + 2a8475c commit 99e2999

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

activerecord/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def validate_each(record, attribute, value)
1414
klass = record.class
1515
if klass.deterministic_encrypted_attributes&.include?(attribute)
1616
encrypted_type = klass.type_for_attribute(attribute)
17-
[ encrypted_type, *encrypted_type.previous_types ].each do |type|
17+
encrypted_type.previous_types.each do |type|
1818
encrypted_value = type.serialize(value)
1919
ActiveRecord::Encryption.without_encryption do
2020
super(record, attribute, encrypted_value)

activerecord/test/cases/encryption/uniqueness_validations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,11 @@ class ActiveRecord::Encryption::UniquenessValidationsTest < ActiveRecord::Encryp
5555
OldEncryptionBook.create! name: "DUNE"
5656
end
5757
end
58+
59+
test "uniqueness validation does not revalidate the attribute with current encryption type" do
60+
EncryptedBookWithUniquenessValidation.create!(name: "dune")
61+
record = EncryptedBookWithUniquenessValidation.create(name: "dune")
62+
63+
assert_equal record.errors.count, 1
64+
end
5865
end

activerecord/test/models/book_encrypted.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class EncryptedBook < ActiveRecord::Base
1010
encrypts :name, deterministic: true
1111
end
1212

13+
class EncryptedBookWithUniquenessValidation < ActiveRecord::Base
14+
self.table_name = "encrypted_books"
15+
16+
validates :name, uniqueness: true
17+
encrypts :name, deterministic: true
18+
end
19+
1320
class EncryptedBookWithDowncaseName < ActiveRecord::Base
1421
self.table_name = "encrypted_books"
1522

0 commit comments

Comments
 (0)