Commit ad1c1488 authored by pbair's avatar pbair

Fix text limit cop to work with new table helper

Fix the Migration/AddLimitToTextColumns cop to work with the
create_table_with_constraints helper. The cop should properly detect
usages of the migration helper that don't specify a limit for each text
column.
parent 153c2b82
......@@ -20,6 +20,10 @@ module RuboCop
(def :down ...)
PATTERN
def_node_matcher :set_text_limit?, <<~PATTERN
(send _ :text_limit ...)
PATTERN
def_node_matcher :add_text_limit?, <<~PATTERN
(send _ :add_text_limit ...)
PATTERN
......@@ -111,18 +115,29 @@ module RuboCop
limit_found = false
node.each_descendant(:send) do |send_node|
next unless add_text_limit?(send_node)
if set_text_limit?(send_node)
limit_found = matching_set_text_limit?(send_node, attribute_name)
elsif add_text_limit?(send_node)
limit_found = matching_add_text_limit?(send_node, table_name, attribute_name)
end
limit_table = send_node.children[2].value
limit_attribute = send_node.children[3].value
break if limit_found
end
if limit_table == table_name && limit_attribute == attribute_name
limit_found = true
break
!limit_found
end
def matching_set_text_limit?(send_node, attribute_name)
limit_attribute = send_node.children[2].value
limit_attribute == attribute_name
end
!limit_found
def matching_add_text_limit?(send_node, table_name, attribute_name)
limit_table = send_node.children[2].value
limit_attribute = send_node.children[3].value
limit_table == table_name && limit_attribute == attribute_name
end
def encrypted_attribute_name?(attribute_name)
......
......@@ -19,7 +19,7 @@ module RuboCop
# or through a create/alter table (TABLE_METHODS)
ADD_COLUMN_METHODS = %i(add_column add_column_with_default change_column_type_concurrently).freeze
TABLE_METHODS = %i(create_table create_table_if_not_exists change_table).freeze
TABLE_METHODS = %i(create_table create_table_if_not_exists change_table create_table_with_constraints).freeze
def high_traffic_tables
@high_traffic_tables ||= rubocop_migrations_config.dig('Migration/UpdateLargeTable', 'HighTrafficTables')
......
......@@ -28,6 +28,15 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do
^^^^ #{described_class::MSG}
end
create_table_with_constraints :test_text_limits_create do |t|
t.integer :test_id, null: false
t.text :title
t.text :description
^^^^ #{described_class::MSG}
t.text_limit :title, 100
end
add_column :test_text_limits, :email, :text
^^^^^^^^^^ #{described_class::MSG}
......@@ -57,6 +66,15 @@ RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do
t.text :name
end
create_table_with_constraints :test_text_limits_create do |t|
t.integer :test_id, null: false
t.text :title
t.text :description
t.text_limit :title, 100
t.text_limit :description, 255
end
add_column :test_text_limits, :email, :text
add_column_with_default :test_text_limits, :role, :text, default: 'default'
change_column_type_concurrently :test_text_limits, :test_id, :text
......
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