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

Fix a false positive for Rails/ReversibleMigration #136

Merged
merged 1 commit into from
Nov 3, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#120](https://github.com/rubocop-hq/rubocop-rails/issues/120): Fix message for `Rails/SaveBang` when the save is in the body of a conditional. ([@jas14][])
* [#131](https://github.com/rubocop-hq/rubocop-rails/pull/131): Fix an incorrect autocorrect for `Rails/Presence` when using `[]` method. ([@forresty][])
* [#142](https://github.com/rubocop-hq/rubocop-rails/pull/142): Fix an incorrect autocorrect for `Rails/EnumHash` when using nested constants. ([@koic][])
* [#136](https://github.com/rubocop-hq/rubocop-rails/pull/136): Fix a false positive for `Rails/ReversibleMigration` when using `change_default` with `:from` and `:to` options. ([@sinsoku][])

## 2.3.2 (2019-09-01)

Expand Down Expand Up @@ -93,3 +94,4 @@
[@eugeneius]: https://github.com/eugeneius
[@jas14]: https://github.com/jas14
[@forresty]: https://github.com/forresty
[@sinsoku]: https://github.com/sinsoku
16 changes: 12 additions & 4 deletions lib/rubocop/cop/rails/reversible_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ module Rails
# @see https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html
class ReversibleMigration < Cop
MSG = '%<action>s is not reversible.'
IRREVERSIBLE_CHANGE_TABLE_CALLS = %i[
change change_default remove
].freeze

def_node_matcher :irreversible_schema_statement_call, <<-PATTERN
(send nil? ${:change_table_comment :execute :remove_belongs_to} ...)
Expand Down Expand Up @@ -244,14 +241,25 @@ def check_change_table_node(node, block)
def check_change_table_offense(receiver, node)
method_name = node.method_name
return if receiver != node.receiver &&
!IRREVERSIBLE_CHANGE_TABLE_CALLS.include?(method_name)
reversible_change_table_call?(node)

add_offense(
node,
message: format(MSG, action: "change_table(with #{method_name})")
)
end

def reversible_change_table_call?(node)
case node.method_name
when :change, :remove
false
when :change_default
all_hash_key?(node.arguments.last, :from, :to)
else
true
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to replace IRREVERSIBLE_CHANGE_TABLE_CALLS constant with the following method.

diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb
index 782b151fd..bf0179c57 100644
--- a/lib/rubocop/cop/rails/reversible_migration.rb
+++ b/lib/rubocop/cop/rails/reversible_migration.rb
@@ -127,9 +127,6 @@ module RuboCop
       # @see https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html
       class ReversibleMigration < Cop
         MSG = '%<action>s is not reversible.'
-        IRREVERSIBLE_CHANGE_TABLE_CALLS = %i[
-          change remove
-        ].freeze

         def_node_matcher :irreversible_schema_statement_call, <<-PATTERN
           (send nil? ${:change_table_comment :execute :remove_belongs_to} ...)
@@ -244,8 +241,7 @@ module RuboCop
         def check_change_table_offense(receiver, node)
           method_name = node.method_name
           return if receiver != node.receiver &&
-                    !IRREVERSIBLE_CHANGE_TABLE_CALLS.include?(method_name) &&
-                    !irreversible_change_default?(node)
+                    reversible_change_table_call?(node)

           add_offense(
             node,
@@ -253,9 +249,15 @@ module RuboCop
           )
         end
-        def irreversible_change_default?(node)
-          node.method_name == :change_default &&
-            !all_hash_key?(node.arguments.last, :from, :to)
+        def reversible_change_table_call?(node)
+          case node.method_name
+          when :change, :remove
+            false
+          when :change_default
+            all_hash_key?(node.arguments.last, :from, :to)
+          else
+            true
+          end
         end

         def within_change_method?(node)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated. Thank you for your review. ❤️


def within_change_method?(node)
node.each_ancestor(:def).any? do |ancestor|
ancestor.method?(:change)
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/rails/reversible_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ def change
end
RUBY

it_behaves_like 'accepts',
'change_table(with reversible change_default)', <<-RUBY
change_table :users do |t|
t.change_default :authorized, from: nil, to: 1
end
RUBY

it_behaves_like 'offense', 'change_table(with change_default)', <<-RUBY
change_table :users do |t|
t.change_default :authorized, 1
Expand Down