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 keeps failing even if the issue has been resolved #227

Closed
tijn opened this issue Apr 2, 2020 · 2 comments · Fixed by #229
Closed

Rails/UniqueValidationWithoutIndex keeps failing even if the issue has been resolved #227

tijn opened this issue Apr 2, 2020 · 2 comments · Fixed by #229
Labels
bug Something isn't working

Comments

@tijn
Copy link

tijn commented Apr 2, 2020

Expected behavior

I expect the cop to detect that schema.rb has changed and I expect it to run the cop again.

Actual behavior

The cop keeps failing until I change the offending file. Adding or removing any character will do the trick. I'm pretty confident that [caching])https://github.com/rubocop-hq/rubocop/blob/master/manual/caching.md) is the issue. My guess is that it stores a result with a key that is a hash of the file it is testing; but I think that should be a hash of [the_file_that_is_being_tested, schema.rb] for this cop since the outcome is dependent on both files.

I can resolve the issue by deleting the rubocop cache:

rm -rf ~/.cache/rubocop_cache/*

Steps to reproduce the problem

  1. Create a class to describe a world wherein books cannot have multiple authors somehow, but don't create the index in the migration yet.
class Author < ApplicationRecord
  validates :book, uniqueness: true
end
  1. Run rubocop; see that it fails the test.

  2. Create a migration to add the missing index.

add_index :authors, :books, unique: true
  1. Run the migration

  2. Run rubocop; see that it still fails the test.

  3. Remove the cache

On macOs without XDG-DIRS set:

rm -rf ~/.cache/rubocop_cache/*
  1. Run rubocop; see that it still passes the test.

RuboCop version

$ [bundle exec] rubocop -V
0.81.0 (using Parser 2.7.0.5, running on ruby 2.6.5 x86_64-darwin19)
@paulofelisbino
Copy link

Same problem with scoped uniqueness.
Can i help in any way?

@koic koic added the bug Something isn't working label Apr 8, 2020
koic added a commit to koic/rubocop-rails that referenced this issue Apr 8, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following API:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 8, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following API:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 8, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following method:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 8, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following method:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 8, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following method:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 9, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following method:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
koic added a commit to koic/rubocop-rails that referenced this issue Apr 9, 2020
…updating schema.rb

Fixes rubocop#227.

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

`Rails/UniqueValidationWithoutIndex` cop needs to know both model
and db/schema.rb changes to register an offense. However, with
default RuboCop, only changes to the model affect cache behavior.

This PR ensures that changes to db/schema.rb affect the cache by
overriding the following method:

```ruby
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
#   (1) the file under inspection
#   (2) the cop's source code
#   (3) the config (eg a .rubocop.yml file)
#
# For example, some cops may want to look at other parts of
# the codebase being inspected to find violations. A cop may
# use the presence or absence of file `foo.rb` to determine
# whether a certain violation exists in `bar.rb`.
#
# Overriding this method allows the cop to indicate to RuboCop's
# ResultCache system when those external dependencies change,
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
  nil
end
```

https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239

See for more details: rubocop/rubocop#7496
@koic koic closed this as completed in #229 Apr 9, 2020
koic added a commit that referenced this issue Apr 9, 2020
…x_aware_schema

[Fix #227] Make `Rails/UniqueValidationWithoutIndex` aware of updating schema.rb
@koic
Copy link
Member

koic commented Apr 9, 2020

RuboCop Rails 2.5.2 has been released to solve this issue. Thanks for the feedback.
https://rubygems.org/gems/rubocop-rails/versions/2.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants