Commit ea2213cc authored by Peter Leitzen's avatar Peter Leitzen Committed by Adam Hegyi

Don't force encrypted database columns to have a text limit

parent eff188a5
...@@ -6,6 +6,10 @@ module RuboCop ...@@ -6,6 +6,10 @@ module RuboCop
module Cop module Cop
module Migration module Migration
# Cop that enforces always adding a limit on text columns # Cop that enforces always adding a limit on text columns
#
# Text columns starting with `encrypted_` are very likely used
# by `attr_encrypted` which controls the text length. Those columns
# should not add a text limit.
class AddLimitToTextColumns < RuboCop::Cop::Cop class AddLimitToTextColumns < RuboCop::Cop::Cop
include MigrationHelpers include MigrationHelpers
...@@ -102,6 +106,8 @@ module RuboCop ...@@ -102,6 +106,8 @@ module RuboCop
# Check if there is an `add_text_limit` call for the provided # Check if there is an `add_text_limit` call for the provided
# table and attribute name # table and attribute name
def text_limit_missing?(node, table_name, attribute_name) def text_limit_missing?(node, table_name, attribute_name)
return false if encrypted_attribute_name?(attribute_name)
limit_found = false limit_found = false
node.each_descendant(:send) do |send_node| node.each_descendant(:send) do |send_node|
...@@ -118,6 +124,10 @@ module RuboCop ...@@ -118,6 +124,10 @@ module RuboCop
!limit_found !limit_found
end end
def encrypted_attribute_name?(attribute_name)
attribute_name.to_s.start_with?('encrypted_')
end
end end
end end
end end
......
...@@ -129,6 +129,28 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns, type: :rubocop do ...@@ -129,6 +129,28 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns, type: :rubocop do
end end
end end
context 'when text columns are used for encryption' do
it 'registers no offenses' do
expect_no_offenses(<<~RUBY)
class TestTextLimits < ActiveRecord::Migration[6.0]
DOWNTIME = false
disable_ddl_transaction!
def up
create_table :test_text_limits, id: false do |t|
t.integer :test_id, null: false
t.text :encrypted_name
end
add_column :encrypted_test_text_limits, :encrypted_email, :text
add_column_with_default :encrypted_test_text_limits, :encrypted_role, :text, default: 'default'
change_column_type_concurrently :encrypted_test_text_limits, :encrypted_test_id, :text
end
end
RUBY
end
end
context 'on down' do context 'on down' do
it 'registers no offense' do it 'registers no offense' do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment