Skip to content

False positive offense with Rails/UniqueValidationWithoutIndex using expression indexes #215

Closed
@ZilvinasKucinskas

Description

Rails 5 added support for expression indexes as follows:

def change
  add_index :emails,
            'lower(address)',
            name: "index_emails_on_address_unique",
            unique: true
end

However, it does not seem to be recognised by Rails/UniqueValidationWithoutIndex rule. I have added a test case below that fails, but should pass instead.

Expected behavior

No offense triggered.

Actual behavior

Offense is being triggered with Uniqueness validation should be with a unique index message.

Steps to reproduce the problem

Here is the test case we can put into spec/rubocop/cop/rails/unique_validation_without_index_spec.rb:

    context 'with expression indexes' do
      let(:schema) { <<~RUBY }
        ActiveRecord::Schema.define(version: 2020_02_02_075409) do
          create_table "emails", force: :cascade do |t|
            t.string "address", null: false
            t.index "lower(address)", name: "index_emails_on_LOWER_address", unique: true
          end
        end
      RUBY

      it 'registers no offense' do
        expect_no_offenses(<<~RUBY)
          class Email
            validates :address, presence: true, uniqueness: { case_sensitive: false }, email: true
          end
        RUBY
      end
    end

RuboCop version

$ [bundle exec] rubocop -V
0.80.1 (using Parser 2.7.0.5, running on ruby 2.6.5 x86_64-darwin18)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions