Skip to content

Commit 885ecef

Browse files
authored
Merge pull request #229 from koic/make_unique_validation_without_index_aware_schema
[Fix #227] Make `Rails/UniqueValidationWithoutIndex` aware of updating schema.rb
2 parents 02e73e4 + 330f874 commit 885ecef

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bug fixes
6+
7+
* [#227](https://github.com/rubocop-hq/rubocop-rails/issues/227): Make `Rails/UniqueValidationWithoutIndex` aware of updating db/schema.rb. ([@koic][])
8+
59
## 2.5.1 (2020-04-02)
610

711
### Bug fixes

lib/rubocop/cop/mixin/active_record_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ module ActiveRecordHelper
1414
(send nil? :belongs_to {str sym} ...)
1515
PATTERN
1616

17+
def external_dependency_checksum
18+
if defined?(@external_dependency_checksum)
19+
return @external_dependency_checksum
20+
end
21+
22+
schema_path = RuboCop::Rails::SchemaLoader.db_schema_path
23+
return nil if schema_path.nil?
24+
25+
schema_code = File.read(schema_path)
26+
27+
@external_dependency_checksum ||= Digest::SHA1.hexdigest(schema_code)
28+
end
29+
30+
def schema
31+
RuboCop::Rails::SchemaLoader.load(target_ruby_version)
32+
end
33+
1734
def table_name(class_node)
1835
table_name = find_set_table_name(class_node).to_a.last&.first_argument
1936
return table_name.value.to_s if table_name

lib/rubocop/cop/rails/unique_validation_without_index.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ def array_node_to_array(node)
149149
end
150150
end
151151
end
152-
153-
def schema
154-
RuboCop::Rails::SchemaLoader.load(target_ruby_version)
155-
end
156152
end
157153
end
158154
end

lib/rubocop/rails/schema_loader.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ def reset!
2424
remove_instance_variable(:@schema)
2525
end
2626

27-
private
28-
29-
def load!(target_ruby_version)
30-
path = db_schema_path
31-
return unless path
32-
33-
ast = parse(path, target_ruby_version)
34-
Schema.new(ast)
35-
end
36-
3727
def db_schema_path
3828
path = Pathname.pwd
3929
until path.root?
@@ -46,6 +36,16 @@ def db_schema_path
4636
nil
4737
end
4838

39+
private
40+
41+
def load!(target_ruby_version)
42+
path = db_schema_path
43+
return unless path
44+
45+
ast = parse(path, target_ruby_version)
46+
Schema.new(ast)
47+
end
48+
4949
def parse(path, target_ruby_version)
5050
klass_name = :"Ruby#{target_ruby_version.to_s.sub('.', '')}"
5151
klass = ::Parser.const_get(klass_name)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::ActiveRecordHelper, :isolated_environment do
4+
include FileHelper
5+
6+
module RuboCop
7+
module Cop
8+
class Example < Cop
9+
include ActiveRecordHelper
10+
end
11+
end
12+
end
13+
14+
let(:cop) do
15+
RuboCop::Cop::Example.new
16+
end
17+
18+
let(:schema_path) { 'db/schema.rb' }
19+
20+
describe '#external_dependency_checksum' do
21+
subject { cop.external_dependency_checksum }
22+
23+
context 'with db/schema.rb' do
24+
before do
25+
create_file(schema_path, <<~RUBY)
26+
ActiveRecord::Schema.define(version: 2020_04_08_082625) do
27+
create_table "articles" do |t|
28+
t.string "title", null: false
29+
end
30+
end
31+
RUBY
32+
end
33+
34+
it { is_expected.to eq '1f263bed5ada8f2292ce7ceebd3c518bac3d2d1d' }
35+
end
36+
37+
context 'with empty db/schema.rb' do
38+
before { create_empty_file(schema_path) }
39+
40+
it { is_expected.to eq 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' }
41+
end
42+
43+
context 'without db/schema.rb' do
44+
it { is_expected.to be_nil }
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)