diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-08-01 17:52:54 +0200 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-08-08 23:30:48 +0200 |
commit | e3ff3909862d81036a64f3eab02d5e3e4802f5e6 (patch) | |
tree | 7a27eb67467652ebb2adf77fb2ef6cf57a8612ba /rubocop | |
parent | 3d2a3e5782aaff37c4b27dc9d3858031ab0c9059 (diff) | |
download | gitlab-ce-e3ff3909862d81036a64f3eab02d5e3e4802f5e6.tar.gz |
Add rubocop check for add_reference to require index.
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/migration/add_reference.rb | 49 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 |
2 files changed, 50 insertions, 0 deletions
diff --git a/rubocop/cop/migration/add_reference.rb b/rubocop/cop/migration/add_reference.rb new file mode 100644 index 00000000000..4b67270c97a --- /dev/null +++ b/rubocop/cop/migration/add_reference.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks if a foreign key constraint is added and require a index for it + class AddReference < RuboCop::Cop::Cop + include MigrationHelpers + + MSG = '`add_reference` requires `index: true`' + + def on_send(node) + return unless in_migration?(node) + + name = node.children[1] + + return unless name == :add_reference + + opts = node.children.last + + add_offense(node, location: :selector) unless opts && opts.type == :hash + + index_present = false + + opts.each_node(:pair) do |pair| + index_present ||= index_enabled?(pair) + end + + add_offense(node, location: :selector) unless index_present + end + + private + + def index_enabled?(pair) + hash_key_type(pair) == :sym && hash_key_name(pair) == :index && pair.children[1].true_type? + end + + def hash_key_type(pair) + pair.children[0].type + end + + def hash_key_name(pair) + pair.children[0].children[0] + end + end + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index aa7ae601f75..a427208cdab 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -11,6 +11,7 @@ require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_concurrent_foreign_key' require_relative 'cop/migration/add_concurrent_index' require_relative 'cop/migration/add_index' +require_relative 'cop/migration/add_reference' require_relative 'cop/migration/add_timestamps' require_relative 'cop/migration/datetime' require_relative 'cop/migration/hash_index' |