2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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`.
|
2022-10-11 01:57:18 +05:30
|
|
|
class UpdateColumnInBatches < RuboCop::Cop::Base
|
2017-09-10 17:25:29 +05:30
|
|
|
include MigrationHelpers
|
|
|
|
|
|
|
|
MSG = 'Migration running `update_column_in_batches` must have a spec file at' \
|
2021-04-29 21:17:54 +05:30
|
|
|
' `%s`.'
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
return unless node.children[1] == :update_column_in_batches
|
|
|
|
|
|
|
|
spec_path = spec_filename(node)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
return if File.exist?(File.expand_path(spec_path, rails_root))
|
|
|
|
|
|
|
|
add_offense(node, message: format(MSG, spec_path))
|
2017-09-10 17:25:29 +05:30
|
|
|
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)
|
2019-07-07 11:18:12 +05:30
|
|
|
.sub(%r{db/(migrate|post_migrate)}, 'spec/migrations')
|
2018-03-17 18:26:18 +05:30
|
|
|
filename = File.basename(source_name, '.rb').sub(/\A\d+_/, '')
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
File.join(dirname, "#{filename}_spec.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
def rails_root
|
|
|
|
Pathname.new(File.expand_path('../../..', __dir__))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|