Commit 08ae9521 authored by Adam Hegyi's avatar Adam Hegyi

Merge branch 'pl-cop-migration-add-limit-to-text-columns-encrypted' into 'master'

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

See merge request gitlab-org/gitlab!45268
parents 0e1943fb ea2213cc
......@@ -6,6 +6,10 @@ module RuboCop
module Cop
module Migration
# 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
include MigrationHelpers
......@@ -102,6 +106,8 @@ module RuboCop
# Check if there is an `add_text_limit` call for the provided
# table and attribute name
def text_limit_missing?(node, table_name, attribute_name)
return false if encrypted_attribute_name?(attribute_name)
limit_found = false
node.each_descendant(:send) do |send_node|
......@@ -118,6 +124,10 @@ module RuboCop
!limit_found
end
def encrypted_attribute_name?(attribute_name)
attribute_name.to_s.start_with?('encrypted_')
end
end
end
end
......
......@@ -129,6 +129,28 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns, type: :rubocop do
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
it 'registers no offense' do
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