Skip to content

Commit b040d84

Browse files
authored
Merge pull request #1038 from fatkodima/fix-unique_validation_without_index-empty-schema
Fix an error for `UniqueValidationWithoutIndex` when `db/schema.rb` is empty
2 parents 297e022 + 0ee4ef5 commit b040d84

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1036](https://github.com/rubocop/rubocop-rails/issues/1036): Fix an error for `UniqueValidationWithoutIndex` when `db/schema.rb` is empty. ([@fatkodima][])

lib/rubocop/rails/schema_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def load!(target_ruby_version)
4343
return unless path
4444

4545
ast = parse(path, target_ruby_version)
46-
Schema.new(ast)
46+
Schema.new(ast) if ast
4747
end
4848

4949
def parse(path, target_ruby_version)

spec/rubocop/cop/rails/unique_validation_without_index_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,5 +690,17 @@ module User
690690
RUBY
691691
end
692692
end
693+
694+
context 'when db/schema.rb file is empty' do
695+
let(:schema) { '' }
696+
697+
it 'does not register an offense' do
698+
expect_no_offenses(<<~RUBY)
699+
class User
700+
validates :account, uniqueness: true
701+
end
702+
RUBY
703+
end
704+
end
693705
end
694706
end

0 commit comments

Comments
 (0)