Commit 673f13a6 authored by pbair's avatar pbair

Don't consider unique index as complex

When detecting "complex" indexes to require an explicit index name,
don't raise a rubocop offense for indexes with a unique option as the
only option.
parent ad20fb49
...@@ -15,12 +15,11 @@ class RemoveOldExternalDiffMigrationIndex < ActiveRecord::Migration[6.0] ...@@ -15,12 +15,11 @@ class RemoveOldExternalDiffMigrationIndex < ActiveRecord::Migration[6.0]
end end
def down def down
# rubocop:disable Migration/ComplexIndexesRequireName
add_concurrent_index( add_concurrent_index(
:merge_request_diffs, :merge_request_diffs,
[:merge_request_id, :id], [:merge_request_id, :id],
where: { stored_externally: [nil, false] } where: 'NOT stored_externally OR stored_externally IS NULL',
name: 'index_merge_request_diffs_on_merge_request_id_and_id_partial'
) )
# rubocop:enable Migration/ComplexIndexesRequireName
end end
end end
...@@ -7,9 +7,7 @@ class AddUniqueIndexToProjectIdOnProjectRegistry < ActiveRecord::Migration[4.2] ...@@ -7,9 +7,7 @@ class AddUniqueIndexToProjectIdOnProjectRegistry < ActiveRecord::Migration[4.2]
def up def up
remove_concurrent_index :project_registry, :project_id if index_exists? :project_registry, :project_id remove_concurrent_index :project_registry, :project_id if index_exists? :project_registry, :project_id
# rubocop:disable Migration/ComplexIndexesRequireName
add_concurrent_index :project_registry, :project_id, unique: true add_concurrent_index :project_registry, :project_id, unique: true
# rubocop:enable Migration/ComplexIndexesRequireName
end end
def down def down
......
...@@ -18,6 +18,10 @@ module RuboCop ...@@ -18,6 +18,10 @@ module RuboCop
(pair {(sym :name) (str "name")} _) (pair {(sym :name) (str "name")} _)
PATTERN PATTERN
def_node_matcher :unique_option?, <<~PATTERN
(pair {(:sym :unique) (str "unique")} _)
PATTERN
def on_def(node) def on_def(node)
return unless in_migration?(node) return unless in_migration?(node)
...@@ -35,8 +39,14 @@ module RuboCop ...@@ -35,8 +39,14 @@ module RuboCop
end end
def needs_name_option?(option_nodes) def needs_name_option?(option_nodes)
return false if only_unique_option?(option_nodes)
option_nodes.none? { |node| name_option?(node) } option_nodes.none? { |node| name_option?(node) }
end end
def only_unique_option?(option_nodes)
option_nodes.size == 1 && unique_option?(option_nodes.first)
end
end end
end end
end end
......
...@@ -35,6 +35,8 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName, type: :ruboco ...@@ -35,6 +35,8 @@ RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName, type: :ruboco
def down def down
add_concurrent_index :test_indexes, :column4, 'unique' => true add_concurrent_index :test_indexes, :column4, 'unique' => true
add_concurrent_index :test_indexes, :column4, 'unique' => true, where: 'column4 IS NOT NULL'
^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG} ^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG}
add_concurrent_index :test_indexes, :column5, using: :gin, name: INDEX_NAME add_concurrent_index :test_indexes, :column5, using: :gin, name: INDEX_NAME
......
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