Commit 648abf78 authored by Markus Koller's avatar Markus Koller

Merge branch 'fix_create_table_with_two_foreign_keys_cop' into 'master'

Fix create table with two foreign keys cop

See merge request gitlab-org/gitlab!42131
parents cb3da621 518a2fae
......@@ -35,7 +35,7 @@ module RuboCop
)
PATTERN
def_node_search :argument_name, <<~PATTERN
def_node_matcher :argument_name?, <<~PATTERN
{(sym $...) (str $...)}
PATTERN
......@@ -87,7 +87,11 @@ module RuboCop
end
def arguments_to_table_names(arguments)
arguments.flat_map { |argument| argument_name(argument).to_a.flatten.first.to_s.pluralize.to_sym }
arguments.select { |argument| argument_name?(argument) }
.map(&:value)
.map(&:to_s)
.map(&:pluralize)
.map(&:to_sym)
end
end
end
......
......@@ -42,6 +42,7 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc
context 'with foreign key' do
context 'with just one foreign key' do
context 'when the foreign_key targets a high traffic table' do
context 'when the foreign_key has to_table option set' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
def up
......@@ -53,6 +54,19 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc
end
end
context 'when the foreign_key does not have to_table option set' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
def up
create_table(:foo) do |t|
t.references :project, foreign_key: { on_delete: 'cascade' }
end
end
RUBY
end
end
end
context 'when the foreign_key does not target a high traffic table' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
......
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