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

An error occurred while Rails/UniqueValidationWithoutIndex cop #226

Closed
jamesst20 opened this issue Apr 2, 2020 · 9 comments
Closed

An error occurred while Rails/UniqueValidationWithoutIndex cop #226

jamesst20 opened this issue Apr 2, 2020 · 9 comments

Comments

@jamesst20
Copy link

jamesst20 commented Apr 2, 2020

Information

Same as #216 that was closed by wrong assumption.
Error is still present:

rubocop-rails 2.5.1
Mention the following information in the issue report:
0.81.0 (using Parser 2.7.0.5, running on ruby 2.6.3 x86_64-linux)

Error

An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /opt/atlassian/pipelines/agent/build/app/models/user.rb:11:2.
To see the complete backtrace run rubocop -d.
1 error occurred:
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /opt/atlassian/pipelines/agent/build/app/models/user.rb:11:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues
Mention the following information in the issue report:
0.80.1 (using Parser 2.7.0.5, running on ruby 2.6.3 x86_64-linux)
Inspecting 56 files
........................................................
56 files inspected, no offenses detected

The file in error

# User of the app
class User < ApplicationRecord
  attr_writer :login # Used for devise authentication by username

  devise :invitable, :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable, timeout_in: 2.days

  belongs_to :legacy_user, foreign_key: :id, inverse_of: :user

  validates :username, presence: true, uniqueness: { case_sensitive: false }
  validates :username, format: { with: /^[a-zA-Z0-9_\.]*$/, multiline: true }

  def login
    @login || username
  end

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:login)
    if login.present?
      where(conditions).find_by(["lower(username) = :value ", { value: login.downcase }])
    elsif conditions[:username].nil?
      find_by(conditions)
    else
      find_by(username: conditions[:username])
    end
  end
end

Extra information

I am not able to replicate the issue on my PC (Mac OS Catalina). I'm guessing cloning the docker image would be enough to replicate the issue, it probably isn't BitBucket-Pipeline related.

Error happens under BitBucket-Pipeline in Ruby 2.6.3 docker image

Thank you

@koic
Copy link
Member

koic commented Apr 3, 2020

Rails/UniqueValidationWithoutIndex cop works with db/schema.rb.
Could you show me db/schema.rb? I will try to reproduce based on that information.

@jamesst20
Copy link
Author

@koic

I believe this should be all you need https://pastebin.com/gSdGA2a2
Otherwise full DB: https://pastebin.com/PRrBgJkt

@koic
Copy link
Member

koic commented Apr 3, 2020

Thank you for showing db/schema.rb.
But I could not reproduce error with the following code. Could you provide me with the code to reproduce error?

% tree
.
├── Gemfile
├── Gemfile.lock
├── app
│   └── models
│       └── user.rb
└── db
    └── schema.rb

3 directories, 4 files
% cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rubocop-rails"
% cat Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    activesupport (6.0.2.2)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
      zeitwerk (~> 2.2)
    ast (2.4.0)
    concurrent-ruby (1.1.6)
    i18n (1.8.2)
      concurrent-ruby (~> 1.0)
    jaro_winkler (1.5.4)
    minitest (5.14.0)
    parallel (1.19.1)
    parser (2.7.0.5)
      ast (~> 2.4.0)
    rack (2.2.2)
    rainbow (3.0.0)
    rexml (3.2.4)
    rubocop (0.81.0)
      jaro_winkler (~> 1.5.1)
      parallel (~> 1.10)
      parser (>= 2.7.0.1)
      rainbow (>= 2.2.2, < 4.0)
      rexml
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 1.4.0, < 2.0)
    rubocop-rails (2.5.1)
      activesupport
      rack (>= 1.1)
      rubocop (>= 0.72.0)
    ruby-progressbar (1.10.1)
    thread_safe (0.3.6)
    tzinfo (1.2.7)
      thread_safe (~> 0.1)
    unicode-display_width (1.7.0)
    zeitwerk (2.3.0)

PLATFORMS
  ruby

DEPENDENCIES
  rubocop-rails

BUNDLED WITH
   2.1.4
% cat app/models/user.rb
# User of the app
class User < ApplicationRecord
  attr_writer :login # Used for devise authentication by username

  devise :invitable, :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable, timeout_in: 2.days

  belongs_to :legacy_user, foreign_key: :id, inverse_of: :user

  validates :username, presence: true, uniqueness: { case_sensitive: false }
  validates :username, format: { with: /^[a-zA-Z0-9_\.]*$/, multiline: true }

  def login
    @login || username
  end

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:login)
    if login.present?
      where(conditions).find_by(["lower(username) = :value ", { value: login.downcase }])
    elsif conditions[:username].nil?
      find_by(conditions)
    else
      find_by(username: conditions[:username])
    end
  end
end
% cat db/schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_03_25_143346) do

  create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "username"
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "failed_attempts", default: 0, null: false
    t.string "unlock_token"
    t.datetime "locked_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "invitation_token"
    t.datetime "invitation_created_at"
    t.datetime "invitation_sent_at"
    t.datetime "invitation_accepted_at"
    t.integer "invitation_limit"
    t.string "invited_by_type"
    t.bigint "invited_by_id"
    t.integer "invitations_count", default: 0
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email"
    t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
    t.index ["invitations_count"], name: "index_users_on_invitations_count"
    t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
    t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by_type_and_invited_by_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

end
% bundle exec rubocop --require rubocop-rails --only Rails/ApplicationRecord
Inspecting 3 files
...

3 files inspected, no offenses detected

Otherwise full DB: https://pastebin.com/PRrBgJkt

Also I tried replacing db/schema.rb with this full db/schema.rb, but no error occurred.

@jamesst20
Copy link
Author

@koic

Thank you for taking the time to fix it. It sadly isn't reproduceable on my development computer.

I am not able to replicate the issue on my PC (Mac OS Catalina). I'm guessing cloning the docker image would be enough to replicate the issue, it probably isn't BitBucket-Pipeline related.

Did you try in the docker image (Ruby 2.6.3 docker image) ? This is where it happens, actually in BitBucket Pipeline: https://pastebin.com/g5M5F2Nq

@jamesst20
Copy link
Author

@koic

Just letting you know that 2.5.2 update resolved the issue !

Thank you ! :)

@koic
Copy link
Member

koic commented Apr 13, 2020

@jamesst20 Yeah, I'm relieved to hear the issue is solved. Thank you too!

@rylanb
Copy link

rylanb commented Aug 3, 2020

Hrm I have a dependabot upgrade to rubcop-rails 2.5.2 in process and I'm getting this same error for 12 of my models.

An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting app/models/county.rb

Running with -d for one model spits out:
undefined method method?' for #RuboCop::AST::BlockNode:0x00007fee2707bdf0`

This may be a valid failure of a validates :name, uniqueness: { scope: :state_id, case_sensitive: false }, presence: true validation where the index doesn't have a unique on it, but the error is not clear.

1 error occurred:
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting app/models/county.rb:5:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker ... 

Also:

Mention the following information in the issue report:
0.73.0 (using Parser 2.7.1.4, running on ruby 2.5.7 x86_64-darwin17)
Finished in 1.2637889999896288 seconds

@koic
Copy link
Member

koic commented Aug 4, 2020

@rylanb Can you open a new issue with an example schema.rb that reproduces the error?

@rylanb
Copy link

rylanb commented Aug 4, 2020

@koic thanks for reaching out! I added a new issue here: #318

ngouy pushed a commit to ngouy/rubocop-rails that referenced this issue Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants