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

Config to ignore old migrations for Rails/ThreeStateBooleanColumn #985

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_config_rails_three_state_boolean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#985](https://github.com/rubocop/rubocop-rails/pull/985): Add `StartAfter` config for `Rails/ThreeStateBooleanColumn` to ignore old migrations. ([@toydestroyer][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ Rails/ThreeStateBooleanColumn:
StyleGuide: 'https://rails.rubystyle.guide/#three-state-boolean'
Enabled: pending
VersionAdded: '2.19'
StartAfter: 0
Include:
- db/**/*.rb

Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/rails/three_state_boolean_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Rails
# t.boolean :active, default: true, null: false
#
class ThreeStateBooleanColumn < Base
include MigrationsHelper

MSG = 'Boolean columns should always have a default value and a `NOT NULL` constraint.'

RESTRICT_ON_SEND = %i[add_column column boolean].freeze
Expand All @@ -39,6 +41,8 @@ class ThreeStateBooleanColumn < Base
PATTERN

def on_send(node)
return if in_migration?(node) && excluded_migration?(processed_source.file_path)

three_state_boolean?(node) do |column_node, options_node|
options_node = options_node.first

Expand Down Expand Up @@ -67,6 +71,13 @@ def table_node(node)
ancestor&.send_node&.first_argument
end
end

def excluded_migration?(file_path)
basename = File.basename(file_path)
version = basename.split('_').first.to_i

version < cop_config['StartAfter'].to_i
end
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/cop/rails/three_state_boolean_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,31 @@ def change
RUBY
end
end

context 'with StartAfter config' do
let(:cop_config) do
{ 'StartAfter' => '20230415000000' }
end

it 'does not register an offense for old files' do
expect_no_offenses(<<~RUBY, 'db/migrate/20230414000000_create_users.rb')
class CreateUsers < ActiveRecord::Migration[7.0]
create_table(:users) do |t|
t.boolean :active
end
end
RUBY
end

it 'registers an offense for new files' do
expect_offense(<<~RUBY, 'db/migrate/20230415000000_create_users.rb')
class CreateUsers < ActiveRecord::Migration[7.0]
create_table(:users) do |t|
t.boolean :active
^^^^^^^^^^^^^^^^^ Boolean columns should always have a default value and a `NOT NULL` constraint.
end
end
RUBY
end
end
end