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 ...@@ -35,7 +35,7 @@ module RuboCop
) )
PATTERN PATTERN
def_node_search :argument_name, <<~PATTERN def_node_matcher :argument_name?, <<~PATTERN
{(sym $...) (str $...)} {(sym $...) (str $...)}
PATTERN PATTERN
...@@ -87,7 +87,11 @@ module RuboCop ...@@ -87,7 +87,11 @@ module RuboCop
end end
def arguments_to_table_names(arguments) 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 end
end end
......
...@@ -42,14 +42,28 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc ...@@ -42,14 +42,28 @@ RSpec.describe RuboCop::Cop::Migration::CreateTableWithForeignKeys, type: :ruboc
context 'with foreign key' do context 'with foreign key' do
context 'with just one 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 targets a high traffic table' do
it 'does not register any offenses' do context 'when the foreign_key has to_table option set' do
expect_no_offenses(<<~RUBY) it 'does not register any offenses' do
def up expect_no_offenses(<<~RUBY)
create_table(:foo) do |t| def up
t.references :project, "foreign_key" => { on_delete: 'cascade', to_table: 'projects' } create_table(:foo) do |t|
t.references :project, "foreign_key" => { on_delete: 'cascade', to_table: 'projects' }
end
end end
end RUBY
RUBY 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
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