From 0ee4ef5e36303ac0d7d82073dbe163b4d7035c0c Mon Sep 17 00:00:00 2001 From: fatkodima Date: Wed, 28 Jun 2023 12:29:40 +0300 Subject: [PATCH] Fix an error for `UniqueValidationWithoutIndex` when `db/schema.rb` is empty --- ...que_validation_without_index_when_schema_empty.md | 1 + lib/rubocop/rails/schema_loader.rb | 2 +- .../rails/unique_validation_without_index_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_for_unique_validation_without_index_when_schema_empty.md diff --git a/changelog/fix_error_for_unique_validation_without_index_when_schema_empty.md b/changelog/fix_error_for_unique_validation_without_index_when_schema_empty.md new file mode 100644 index 0000000000..1f1094f7a0 --- /dev/null +++ b/changelog/fix_error_for_unique_validation_without_index_when_schema_empty.md @@ -0,0 +1 @@ +* [#1036](https://github.com/rubocop/rubocop-rails/issues/1036): Fix an error for `UniqueValidationWithoutIndex` when `db/schema.rb` is empty. ([@fatkodima][]) diff --git a/lib/rubocop/rails/schema_loader.rb b/lib/rubocop/rails/schema_loader.rb index 86041d6931..a3f457f2c2 100644 --- a/lib/rubocop/rails/schema_loader.rb +++ b/lib/rubocop/rails/schema_loader.rb @@ -43,7 +43,7 @@ def load!(target_ruby_version) return unless path ast = parse(path, target_ruby_version) - Schema.new(ast) + Schema.new(ast) if ast end def parse(path, target_ruby_version) diff --git a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb index 86acb14537..5994c077d7 100644 --- a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb +++ b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb @@ -690,5 +690,17 @@ module User RUBY end end + + context 'when db/schema.rb file is empty' do + let(:schema) { '' } + + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + class User + validates :account, uniqueness: true + end + RUBY + end + end end end