Only run limit-validation tests when the limit exists

SQLite and PostgreSQL won't add a default limit of 255 for string types
This commit is contained in:
Jorge Manrubia 2021-03-12 15:56:33 +01:00
parent dcbe9cd5e4
commit 9bfca80641
2 changed files with 3 additions and 2 deletions

@ -128,7 +128,7 @@ def preserve_original_encrypted(name)
end
def validate_column_size(attribute_name)
if table_exists? && limit = connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]&.limit
if table_exists? && limit = columns_hash[attribute_name.to_s]&.limit
validates_length_of attribute_name, maximum: limit
end
end

@ -215,7 +215,8 @@ class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::TestCase
assert_equal "Stephen King", author.name
end
unless current_adapter?(:SQLite3Adapter)
# Only run for adapters that add a default string limit when not provided (MySQL, 255)
if EncryptedAuthor.columns_hash["name"].limit
# No column limits in SQLLite
test "validate column sizes" do
assert EncryptedAuthor.new(name: "jorge").valid?