Skip to content

Commit 187e6c8

Browse files
committed
New Migration/UpdateColumnInBatches cop
Signed-off-by: Rémy Coutable <[email protected]>
1 parent c9e9c2f commit 187e6c8

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require_relative '../../migration_helpers'
2+
3+
module RuboCop
4+
module Cop
5+
module Migration
6+
# Cop that checks if a spec file exists for any migration using
7+
# `update_column_in_batches`.
8+
class UpdateColumnInBatches < RuboCop::Cop::Cop
9+
include MigrationHelpers
10+
11+
MSG = 'Migration running `update_column_in_batches` must have a spec file at' \
12+
' `%s`.'.freeze
13+
14+
def on_send(node)
15+
return unless in_migration?(node)
16+
return unless node.children[1] == :update_column_in_batches
17+
18+
spec_path = spec_filename(node)
19+
20+
unless File.exist?(File.expand_path(spec_path, rails_root))
21+
add_offense(node, :expression, format(MSG, spec_path))
22+
end
23+
end
24+
25+
private
26+
27+
def spec_filename(node)
28+
source_name = node.location.expression.source_buffer.name
29+
path = Pathname.new(source_name).relative_path_from(rails_root)
30+
dirname = File.dirname(path)
31+
.sub(%r{\Adb/(migrate|post_migrate)}, 'spec/migrations')
32+
filename = File.basename(source_name, '.rb').sub(%r{\A\d+_}, '')
33+
34+
File.join(dirname, "#{filename}_spec.rb")
35+
end
36+
37+
def rails_root
38+
Pathname.new(File.expand_path('../../..', __dir__))
39+
end
40+
end
41+
end
42+
end
43+
end

rubocop/migration_helpers.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module RuboCop
33
module MigrationHelpers
44
# Returns true if the given node originated from the db/migrate directory.
55
def in_migration?(node)
6-
File.dirname(node.location.expression.source_buffer.name).
7-
end_with?('db/migrate')
6+
dirname = File.dirname(node.location.expression.source_buffer.name)
7+
8+
dirname.end_with?('db/migrate', 'db/post_migrate')
89
end
910
end
1011
end

rubocop/rubocop.rb

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
require_relative 'cop/migration/remove_concurrent_index'
99
require_relative 'cop/migration/remove_index'
1010
require_relative 'cop/migration/reversible_add_column_with_default'
11+
require_relative 'cop/migration/update_column_in_batches'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
require 'spec_helper'
2+
require 'rubocop'
3+
require 'rubocop/rspec/support'
4+
require_relative '../../../../rubocop/cop/migration/update_column_in_batches'
5+
6+
describe RuboCop::Cop::Migration::UpdateColumnInBatches do
7+
let(:cop) { described_class.new }
8+
let(:tmp_rails_root) { Rails.root.join('tmp', 'rails_root') }
9+
let(:migration_code) do
10+
<<-END
11+
def up
12+
update_column_in_batches(:projects, :name, "foo") do |table, query|
13+
query.where(table[:name].eq(nil))
14+
end
15+
end
16+
END
17+
end
18+
19+
before do
20+
allow(cop).to receive(:rails_root).and_return(tmp_rails_root)
21+
end
22+
after do
23+
FileUtils.rm_rf(tmp_rails_root)
24+
end
25+
26+
context 'outside of a migration' do
27+
it 'does not register any offenses' do
28+
inspect_source(cop, migration_code)
29+
30+
expect(cop.offenses).to be_empty
31+
end
32+
end
33+
34+
let(:spec_filepath) { tmp_rails_root.join('spec', 'migrations', 'my_super_migration_spec.rb') }
35+
36+
shared_context 'with a migration file' do
37+
before do
38+
FileUtils.mkdir_p(File.dirname(migration_filepath))
39+
@migration_file = File.new(migration_filepath, 'w+')
40+
end
41+
after do
42+
@migration_file.close
43+
end
44+
end
45+
46+
shared_examples 'a migration file with no spec file' do
47+
include_context 'with a migration file'
48+
49+
let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) }
50+
51+
it 'registers an offense when using update_column_in_batches' do
52+
inspect_source(cop, migration_code, @migration_file)
53+
54+
aggregate_failures do
55+
expect(cop.offenses.size).to eq(1)
56+
expect(cop.offenses.map(&:line)).to eq([2])
57+
expect(cop.offenses.first.message).
58+
to include("`#{relative_spec_filepath}`")
59+
end
60+
end
61+
end
62+
63+
shared_examples 'a migration file with a spec file' do
64+
include_context 'with a migration file'
65+
66+
before do
67+
FileUtils.mkdir_p(File.dirname(spec_filepath))
68+
@spec_file = File.new(spec_filepath, 'w+')
69+
end
70+
after do
71+
@spec_file.close
72+
end
73+
74+
it 'does not register any offenses' do
75+
inspect_source(cop, migration_code, @migration_file)
76+
77+
expect(cop.offenses).to be_empty
78+
end
79+
end
80+
81+
context 'in a migration' do
82+
let(:migration_filepath) { tmp_rails_root.join('db', 'migrate', '20121220064453_my_super_migration.rb') }
83+
84+
it_behaves_like 'a migration file with no spec file'
85+
it_behaves_like 'a migration file with a spec file'
86+
end
87+
88+
context 'in a post migration' do
89+
let(:migration_filepath) { tmp_rails_root.join('db', 'post_migrate', '20121220064453_my_super_migration.rb') }
90+
91+
it_behaves_like 'a migration file with no spec file'
92+
it_behaves_like 'a migration file with a spec file'
93+
end
94+
end

0 commit comments

Comments
 (0)