diff --git a/CHANGELOG.md b/CHANGELOG.md index 2364094ca00b..962bd6aab530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#5801](https://github.com/bbatsov/rubocop/pull/5801): Add new `Rails/RefuteMethods` cop. ([@koic][]) * [#5805](https://github.com/bbatsov/rubocop/pull/5805): Add new `Rails/AssertNot` cop. ([@composerinteralia][]) * [#4136](https://github.com/bbatsov/rubocop/issues/4136): Allow more robust `Layout/ClosingParenthesisIndentation` detection including method chaining. ([@jfelchner][]) +* [#5821](https://github.com/bbatsov/rubocop/pull/5821): Support `AR::Migration#up_only` for `Rails/ReversibleMigration` cop. ([@koic][]) ### Bug fixes diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index 97b70c4e90e4..8e75eb4df3bf 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -157,7 +157,7 @@ class ReversibleMigration < Cop def on_send(node) return unless within_change_method?(node) - return if within_reversible_block?(node) + return if within_reversible_or_up_only_block?(node) check_irreversible_schema_statement_node(node) check_drop_table_node(node) @@ -168,7 +168,7 @@ def on_send(node) def on_block(node) return unless within_change_method?(node) - return if within_reversible_block?(node) + return if within_reversible_or_up_only_block?(node) check_change_table_node(node.send_node, node.body) end @@ -261,9 +261,11 @@ def within_change_method?(node) end end - def within_reversible_block?(node) + def within_reversible_or_up_only_block?(node) node.each_ancestor(:block).any? do |ancestor| - ancestor.block_type? && ancestor.send_node.method?(:reversible) + ancestor.block_type? && + ancestor.send_node.method?(:reversible) || + ancestor.send_node.method?(:up_only) end end diff --git a/spec/rubocop/cop/rails/reversible_migration_spec.rb b/spec/rubocop/cop/rails/reversible_migration_spec.rb index 4e732ddfafbd..c1d40994d20c 100644 --- a/spec/rubocop/cop/rails/reversible_migration_spec.rb +++ b/spec/rubocop/cop/rails/reversible_migration_spec.rb @@ -43,6 +43,10 @@ def change execute "ALTER TABLE `pages_linked_pages` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)" RUBY + it_behaves_like :accepts, 'up_only', <<-RUBY + up_only { execute "UPDATE posts SET published = 'true'" } + RUBY + context 'within block' do it_behaves_like :accepts, 'create_table', <<-RUBY [:users, :articles].each do |table|