Skip to content

Commit 02d989e

Browse files
authored
Merge pull request rails#43009 from basecamp/encryption-lenght-validation
Move length-validation for encrypted columns after schema is loaded
2 parents 0965b67 + f0fe547 commit 02d989e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

activerecord/lib/active_record/encryption/encryptable_record.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def encrypt_attribute(name, attribute_scheme)
8989
end
9090

9191
preserve_original_encrypted(name) if attribute_scheme.ignore_case?
92-
validate_column_size(name) if ActiveRecord::Encryption.config.validate_column_size
9392
ActiveRecord::Encryption.encrypted_attribute_was_declared(self, name)
9493
end
9594

@@ -121,12 +120,22 @@ def override_accessors_to_preserve_original(name, original_attribute_name)
121120
end)
122121
end
123122

123+
def load_schema!
124+
super
125+
126+
add_length_validation_for_encrypted_columns if ActiveRecord::Encryption.config.validate_column_size
127+
end
128+
129+
def add_length_validation_for_encrypted_columns
130+
encrypted_attributes&.each do |attribute_name|
131+
validate_column_size attribute_name
132+
end
133+
end
134+
124135
def validate_column_size(attribute_name)
125136
if table_exists? && limit = columns_hash[attribute_name.to_s]&.limit
126137
validates_length_of attribute_name, maximum: limit
127138
end
128-
rescue ActiveRecord::ConnectionNotEstablished => e
129-
Rails.logger.warn "Skipping adding length validation for #{self.name}\##{attribute_name}. Can't check column limit due to: #{e.inspect}"
130139
end
131140
end
132141

0 commit comments

Comments
 (0)