Rubocop: Rubocop should ignore the schema in Rails projects

Created on 22 Jan 2014  Â·  8Comments  Â·  Source: rubocop-hq/rubocop

When rubocop -R is run, it takes into account the db/schema.rb file, which as you know is generated automatically when a migration is done so nothing can be done about its offences. Am I right?

Most helpful comment

A simple workaround is to create a .rubocop.yml file in the root of your Rails project with the following contents:

AllCops:
  Excludes:
    - db/schema.rb

I have the following in my Rails project:

AllCops:
  Excludes:
    - config/unicorn.rb
    - db/**
  RunRailsCops: true

All 8 comments

A simple workaround is to create a .rubocop.yml file in the root of your Rails project with the following contents:

AllCops:
  Excludes:
    - db/schema.rb

I have the following in my Rails project:

AllCops:
  Excludes:
    - config/unicorn.rb
    - db/**
  RunRailsCops: true

Ahh, I see. Never mind then. Thanks.

:+1:

Possible bug for me?

Versioning:

RuboCop version 0.52.0.
ruby 2.4.0
Rails 5.1.3

Concerns

I updated to Exclude as @dorian suggested, it still fails. I've also run it both with & without quotes.

ScreenShot

screen shot 2018-01-01 at 3 56 56 pm

What's interesting is when I run rubocop from the command line it passes, however I have a git hook on my commits and that's when it complains.

Screenshot

screen shot 2018-01-01 at 4 07 24 pm

@PrimeTimeTran Can you show me the command line that you executed?

If you specify db/schema.rb with command line, rubocop does not ignore it.

% cat .rubocop.yml| head
require: 'rubocop-rspec'

AllCops:
  Exclude:
    - 'vendor/**/*'
    - 'db/schema.rb'
  DisplayCopNames: true
  TargetRubyVersion: 2.4

Layout/AlignArray:

% rubocop | grep 'db/schema.rb'  # nothing displayed

% rubocop db/schema.rb | grep 'db/schema.rb'
db/schema.rb:13:1: C: Metrics/BlockLength: Block has too many lines. [496/30]
db/schema.rb:24:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:26:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:48:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:88:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:102:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:215:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:272:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:340:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:353:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:397:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:406:13: C: Style/WordArray: Use %w or %W for an array of words.
db/schema.rb:481:13: C: Style/WordArray: Use %w or %W for an array of words.

See also https://github.com/bbatsov/rubocop/issues/5139#issuecomment-348678866

@pocke it's a git hook =). What can I show you in the hook that could be useful? Sorry, I'm not super familiar with linux so I'm not sure what all this does.

if command -v rubocop > /dev/null; then
  git status --porcelain \
  | grep -E '^A|^M' \
  | grep '.rb' \
  | awk '{print $2}' \
  | xargs rubocop
fi

Update

When I ran cat .rubocop.yml| head I got this.
```
➜ railsbuuk git:(master) ✗ cat .rubocop.yml| head
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.4.0
DisplayCopNames: true
Exclude:
- 'app/assets/stylesheets/application.scss'
- 'config/routes.rb'
- 'db//.rb'
- 'node_modules/
/'

It looks executing rubocop with changed files.
Can you try updating your script with the following? It adds --force-exclusion option to avoid this problem.

if command -v rubocop > /dev/null; then
  git status --porcelain \
  | grep -E '^A|^M' \
  | grep '.rb' \
  | awk '{print $2}' \
  | xargs rubocop --force-exclusion
fi

@pocke that resolved it. Thanks for the help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikegee picture mikegee  Â·  3Comments

benoittgt picture benoittgt  Â·  3Comments

millisami picture millisami  Â·  3Comments

bbatsov picture bbatsov  Â·  3Comments

ecbrodie picture ecbrodie  Â·  3Comments