Skip to content

Commit

Permalink
[Fix rubocop#419] Fix an error for Rails/UniqueValidationWithoutIndex
Browse files Browse the repository at this point in the history
Fixes rubocop#419.

This PR fixes an error for `Rails/UniqueValidationWithoutIndex` when using
a unique index and `check_constraint` that has `nil` first argument.
  • Loading branch information
koic committed May 7, 2021
1 parent 981d7d7 commit 98059c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#419](https://github.com/rubocop/rubocop-rails/issues/419): Fix an error for `Rails/UniqueValidationWithoutIndex` when using a unique index and `check_constraint` that has `nil` first argument. ([@koic][])

## 2.10.1 (2021-05-06)

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rails/schema_loader/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Column
attr_reader :name, :type, :not_null

def initialize(node)
@name = node.first_argument.value
@name = node.first_argument.str_content
@type = node.method_name
@not_null = nil

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ class User
end
end

context 'with a unique index and `check_constraint` that has `nil` first argument' do
let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
create_table "users", force: :cascade do |t|
t.string "account", null: false
t.index ["account"], name: "index_users_on_account", unique: true
t.check_constraint nil, 'expression', name: "constraint_name"
end
end
RUBY

it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class User
validates :account, uniqueness: true
end
RUBY
end
end

context 'when the validation is for two columns' do
context 'without proper index' do
let(:schema) { <<~RUBY }
Expand Down

0 comments on commit 98059c3

Please sign in to comment.