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

Rails/UniqueValidationWithoutIndex: False positive when there's no t.index #221

Closed
igorlvicente opened this issue Mar 27, 2020 · 5 comments · Fixed by #222
Closed

Rails/UniqueValidationWithoutIndex: False positive when there's no t.index #221

igorlvicente opened this issue Mar 27, 2020 · 5 comments · Fixed by #222
Labels
enhancement New feature or request

Comments

@igorlvicente
Copy link

I don't really know what is the problem, but I saw the spec of this cop and found no example that uses add_index rather than t.index on schema.rb, so I guess that could be the reason.

I tried the rubocop-rails in the versions 2.4 and 2.5


Expected behavior

No errors raised

Actual behavior

Output:

rubocop -d -c ./.rubocop.yml app/models/MODEL_NAMESPACE/MODEL_NAME.rb 
configuration from /home/USER/REPO/.rubocop.yml
configuration from /home/USER/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rubocop-rails-2.5.0/config/default.yml
configuration from /home/USER/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rubocop-rails-2.5.0/config/default.yml
Default configuration from /home/USER/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rubocop-0.78.0/config/default.yml
configuration from /home/USER/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rubocop-rspec-1.37.1/config/default.yml
configuration from /home/USER/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rubocop-rspec-1.37.1/config/default.yml
Inspecting 1 file
Scanning /home/USER/REPO/app/models/customer_success/contact.rb
C

Offenses:

app/models/NAMESPACE/MODEL.rb:2:1: C: Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments. (https://rubystyle.guide#separate-magic-comments-from-code)
# == Schema Information
^
app/models/NAMESPACE/MODEL.rb:34:5: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should be with a unique index.
    validates :phone, phone: true, allow_blank: true, uniqueness: true
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected
Finished in 1.7771203410002272 seconds

My schema.rb related to the model:

create_table "TABLE_NAME", force: :cascade do |t|
    t.string   "name",              limit: 255
    t.datetime "created_at",                                      null: false
    t.datetime "updated_at",                                      null: false
    t.integer  "salon_id",          limit: 4
    t.boolean  "in_charge_finance",               default: false
    t.text     "observation",       limit: 65535
  end

  add_index "TABLE_NAME", ["phone"], name: "index_TABLE_NAME_on_phone", unique: true, using: :btree
  add_index "TABLE_NAME", ["salon_id"], name: "index_TABLE_NAME_on_salon_id", using: :btree

Steps to reproduce the problem

Having a add_index instead of a t.index on schema.rb, I guess.

RuboCop version

$ [bundle exec] rubocop -V
0.78.0 (using Parser 2.6.5.0, running on ruby 2.5.5 x86_64-linux)
@rodrigomanhaes
Copy link

rodrigomanhaes commented Mar 27, 2020

Same issue here using add_index or t.index.

@koic
Copy link
Member

koic commented Mar 27, 2020

Thank you for the feedback. AFAIK, the expression of index written in db/schema.rb changes between Rails 4 and Rails 5. And The Rails core team currently maintains versions Rails 6.0.Z and 5.2.Z.
https://guides.rubyonrails.org/maintenance_policy.html

Therefore, RuboCop Rails will drop support for Rails 4 in near future (Maybe 5.0 and 5.1 will be supported for a while). So, I will consider whether to implement the feature whose support will be cut in near future.

koic added a commit to koic/rubocop-rails that referenced this issue Mar 28, 2020
…`add_index`

Fixes rubocop#221.

This PR makes `Rails/UniqueValidationWithoutIndex` aware of `add_index`
in db/schema.rb.

Rails 4.0 and Rails 4.1 support will drop shortly, but Rails 4.2
may be a bit ahead. There may be changes to these support after
the bug fix release of RoboCop Rails 2.5 series.
@koic
Copy link
Member

koic commented Mar 28, 2020

This issue resolved by #222 because Rails 4.0 and Rails 4.1 support will drop shortly, but Rails 4.2 may be a bit ahead. Thank you.

@koic koic added the enhancement New feature or request label Mar 28, 2020
koic added a commit to koic/rubocop-rails that referenced this issue Mar 28, 2020
…`add_index`

Fixes rubocop#221.

This PR makes `Rails/UniqueValidationWithoutIndex` aware of `add_index`
in db/schema.rb.

Rails 4.0 and Rails 4.1 support will drop shortly, but Rails 4.2
may be a bit ahead. There may be changes to these support after
the bug fix release of RoboCop Rails 2.5 series.
koic added a commit to koic/rubocop-rails that referenced this issue Mar 29, 2020
…`add_index`

Fixes rubocop#221.

This PR makes `Rails/UniqueValidationWithoutIndex` aware of `add_index`
in db/schema.rb.

Rails 4.0 and Rails 4.1 support will drop shortly, but Rails 4.2
may be a bit ahead. There may be changes to these support after
the bug fix release of RoboCop Rails 2.5 series.
koic added a commit to koic/rubocop-rails that referenced this issue Mar 29, 2020
…`add_index`

Fixes rubocop#221.

This PR makes `Rails/UniqueValidationWithoutIndex` aware of `add_index`
in db/schema.rb.

Rails 4.0 and Rails 4.1 support will drop shortly, but Rails 4.2
may be a bit ahead. There may be changes to these support after
the bug fix release of RoboCop Rails 2.5 series.
@koic koic closed this as completed in #222 Mar 31, 2020
koic added a commit that referenced this issue Mar 31, 2020
…x_aware_of_add_index

[Fix #221] Make `Rails/UniqueValidationWithoutIndex` aware of `add_index`
@GoudekettingRM
Copy link

I just ran into the same issue using the index: true option.

t.string :key, null: false, index: true

Not sure if it is because of old versions, could be the cause. I just cannot update them 🥲

versions:

  • ruby: 2.7.4
  • rails: 5.2.3
  • rubocop: 1.27.0
  • rubocop-rails: 2.5.2

@Piioo
Copy link

Piioo commented Apr 18, 2023

I just ran into the same issue using the index: true option.

t.string :key, null: false, index: true

Not sure if it is because of old versions, could be the cause. I just cannot update them 🥲

versions:

  • ruby: 2.7.4
  • rails: 5.2.3
  • rubocop: 1.27.0
  • rubocop-rails: 2.5.2

Have the same problem with:
Rails 7.0.4.3
ruby 3.2.2
rubocop 1.50.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants