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

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Oct 8, 2019

The change_table method is reversible when using change_default with the :from and :to options.
However, RuboCop detects Rails/ReversibleMigration incorrectly, so it fixes.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

def irreversible_change_default?(node)
node.method_name == :change_default &&
!all_hash_key?(node.arguments.last, :from, :to)
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. ❤️

The `change_table` method is reversible when using `change_default` with
the :from and :to options.
However, RuboCop detects `Rails/ReversibleMigration` incorrectly, so it fixes.
@sinsoku sinsoku force-pushed the patch/reversible_change_default branch from b4ee0fe to 37ec353 Compare November 2, 2019 12:59
@koic koic merged commit 7e540be into rubocop:master Nov 3, 2019
@koic
Copy link
Member

koic commented Nov 3, 2019

Thanks!

@sinsoku sinsoku deleted the patch/reversible_change_default branch November 3, 2019 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants