diff options
author | Rémy Coutable <remy@rymai.me> | 2017-05-22 18:31:22 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-05-29 11:28:23 +0200 |
commit | 187e6c8d7c9eea51358bb9b653a512ac98fa5746 (patch) | |
tree | 7a175f7695d11f4b625067e170532b9d7ab3587d /rubocop | |
parent | c9e9c2ffaac773ef85267e558fe345171d2ce979 (diff) | |
download | gitlab-ce-187e6c8d7c9eea51358bb9b653a512ac98fa5746.tar.gz |
New Migration/UpdateColumnInBatches cop
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/migration/update_column_in_batches.rb | 43 | ||||
-rw-r--r-- | rubocop/migration_helpers.rb | 5 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 |
3 files changed, 47 insertions, 2 deletions
diff --git a/rubocop/cop/migration/update_column_in_batches.rb b/rubocop/cop/migration/update_column_in_batches.rb new file mode 100644 index 00000000000..3f886cbfea3 --- /dev/null +++ b/rubocop/cop/migration/update_column_in_batches.rb @@ -0,0 +1,43 @@ +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks if a spec file exists for any migration using + # `update_column_in_batches`. + class UpdateColumnInBatches < RuboCop::Cop::Cop + include MigrationHelpers + + MSG = 'Migration running `update_column_in_batches` must have a spec file at' \ + ' `%s`.'.freeze + + def on_send(node) + return unless in_migration?(node) + return unless node.children[1] == :update_column_in_batches + + spec_path = spec_filename(node) + + unless File.exist?(File.expand_path(spec_path, rails_root)) + add_offense(node, :expression, format(MSG, spec_path)) + end + end + + private + + def spec_filename(node) + source_name = node.location.expression.source_buffer.name + path = Pathname.new(source_name).relative_path_from(rails_root) + dirname = File.dirname(path) + .sub(%r{\Adb/(migrate|post_migrate)}, 'spec/migrations') + filename = File.basename(source_name, '.rb').sub(%r{\A\d+_}, '') + + File.join(dirname, "#{filename}_spec.rb") + end + + def rails_root + Pathname.new(File.expand_path('../../..', __dir__)) + end + end + end + end +end diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb index 3160a784a04..c3473771178 100644 --- a/rubocop/migration_helpers.rb +++ b/rubocop/migration_helpers.rb @@ -3,8 +3,9 @@ module RuboCop module MigrationHelpers # Returns true if the given node originated from the db/migrate directory. def in_migration?(node) - File.dirname(node.location.expression.source_buffer.name). - end_with?('db/migrate') + dirname = File.dirname(node.location.expression.source_buffer.name) + + dirname.end_with?('db/migrate', 'db/post_migrate') end end end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 4ff204f939e..b65efbc41f4 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -8,3 +8,4 @@ require_relative 'cop/migration/add_index' require_relative 'cop/migration/remove_concurrent_index' require_relative 'cop/migration/remove_index' require_relative 'cop/migration/reversible_add_column_with_default' +require_relative 'cop/migration/update_column_in_batches' |