Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #419] Fix an error for Rails/UniqueValidationWithoutIndex #480

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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