Commit c2745ad6 authored by Peter Leitzen's avatar Peter Leitzen

RuboCop: Allow `add_foreign_key` within `with_lock_retries`

parent 1343456a
...@@ -14,15 +14,21 @@ module RuboCop ...@@ -14,15 +14,21 @@ module RuboCop
(false) (false)
PATTERN PATTERN
def_node_matcher :with_lock_retries?, <<~PATTERN
(:send nil? :with_lock_retries)
PATTERN
def on_send(node) def on_send(node)
return unless in_migration?(node) return unless in_migration?(node)
name = node.children[1] name = node.children[1]
if name == :add_foreign_key && !not_valid_fk?(node) return unless name == :add_foreign_key
return if in_with_lock_retries?(node)
return if not_valid_fk?(node)
add_offense(node, location: :selector) add_offense(node, location: :selector)
end end
end
def method_name(node) def method_name(node)
node.children.first node.children.first
...@@ -33,6 +39,12 @@ module RuboCop ...@@ -33,6 +39,12 @@ module RuboCop
pair.children[0].children[0] == :validate && false_node?(pair.children[1]) pair.children[0].children[0] == :validate && false_node?(pair.children[1])
end end
end end
def in_with_lock_retries?(node)
node.each_ancestor(:block).any? do |parent|
with_lock_retries?(parent.to_a.first)
end
end
end end
end end
end end
......
...@@ -36,5 +36,15 @@ RSpec.describe RuboCop::Cop::Migration::AddConcurrentForeignKey, type: :rubocop ...@@ -36,5 +36,15 @@ RSpec.describe RuboCop::Cop::Migration::AddConcurrentForeignKey, type: :rubocop
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
it 'does not register an offense when `add_foreign_key` is within `with_lock_retries`' do
inspect_source <<~RUBY
with_lock_retries do
add_foreign_key :key, :projects, column: :project_id, on_delete: :cascade
end
RUBY
expect(cop.offenses).to be_empty
end
end end
end end
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