Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle t.remove with multiple columns in Rails/BulkChangeTable #635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_bulk_change_table_remove_multiple_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#635](https://github.com/rubocop/rubocop-rails/pull/635): Handle `t.remove` with multiple columns in `Rails/BulkChangeTable`. ([@eugeneius][])
26 changes: 20 additions & 6 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,31 @@ def on_send(node)
return if include_bulk_options?(node)
return unless node.block_node

send_nodes = node.block_node.body.each_child_node(:send).to_a
send_nodes = send_nodes_from_change_table_block(node.block_node.body)

transformations = send_nodes.select do |send_node|
combinable_transformations.include?(send_node.method_name)
end

add_offense_for_change_table(node) if transformations.size > 1
add_offense_for_change_table(node) if count_transformations(send_nodes) > 1
end

private

def send_nodes_from_change_table_block(body)
if body.send_type?
[body]
else
body.each_child_node(:send).to_a
end
end

def count_transformations(send_nodes)
send_nodes.sum do |node|
if node.method?(:remove)
node.arguments.count { |arg| !arg.hash_type? }
else
combinable_transformations.include?(node.method_name) ? 1 : 0
end
end
end

# @param node [RuboCop::AST::SendNode] (send nil? :change_table ...)
def include_bulk_options?(node)
# arguments: [{(sym :table)(str "table")} (hash (pair (sym :bulk) _))]
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,48 @@ def change
end
RUBY
end

it 'registers an offense for a single `t.remove` with multiple columns' do
expect_offense(<<~RUBY)
def change
change_table :users do |t|
^^^^^^^^^^^^^^^^^^^ You can combine alter queries using `bulk: true` options.
t.remove :name, :metadata
end
end
RUBY
end

it 'does not register an offense for a single `t.remove` with one column' do
expect_no_offenses(<<~RUBY)
def change
change_table :users do |t|
t.remove :name
end
end
RUBY
end

it 'registers an offense for a single `t.remove` with multiple columns and options' do
expect_offense(<<~RUBY)
def change
change_table :users do |t|
^^^^^^^^^^^^^^^^^^^ You can combine alter queries using `bulk: true` options.
t.remove :name, :metadata, type: :string
end
end
RUBY
end

it 'does not register an offense for a single `t.remove` with one column and options' do
expect_no_offenses(<<~RUBY)
def change
change_table :users do |t|
t.remove :name, type: :string
end
end
RUBY
end
end

context 'when database is PostgreSQL' do
Expand Down