Merge pull request #48462 from iamradioactive/remove_additional_query

Do not revalidate encrypted attribute with current encrypted_type
This commit is contained in:
Jean Boussier 2023-09-25 13:50:07 +02:00 committed by GitHub
commit 99e2999f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -14,7 +14,7 @@ module ActiveRecord
klass = record.class
if klass.deterministic_encrypted_attributes&.include?(attribute)
encrypted_type = klass.type_for_attribute(attribute)
[ encrypted_type, *encrypted_type.previous_types ].each do |type|
encrypted_type.previous_types.each do |type|
encrypted_value = type.serialize(value)
ActiveRecord::Encryption.without_encryption do
super(record, attribute, encrypted_value)

View File

@ -55,4 +55,11 @@ class ActiveRecord::Encryption::UniquenessValidationsTest < ActiveRecord::Encryp
OldEncryptionBook.create! name: "DUNE"
end
end
test "uniqueness validation does not revalidate the attribute with current encryption type" do
EncryptedBookWithUniquenessValidation.create!(name: "dune")
record = EncryptedBookWithUniquenessValidation.create(name: "dune")
assert_equal record.errors.count, 1
end
end

View File

@ -10,6 +10,13 @@ class EncryptedBook < ActiveRecord::Base
encrypts :name, deterministic: true
end
class EncryptedBookWithUniquenessValidation < ActiveRecord::Base
self.table_name = "encrypted_books"
validates :name, uniqueness: true
encrypts :name, deterministic: true
end
class EncryptedBookWithDowncaseName < ActiveRecord::Base
self.table_name = "encrypted_books"