From a54dbefad09ee4032d942f17177821072f460e74 Mon Sep 17 00:00:00 2001 From: Sergey Toy Date: Sat, 15 Apr 2023 17:46:27 +0800 Subject: [PATCH 1/2] Add `StartAfter` config for `ThreeStateBooleanColumn` --- config/default.yml | 1 + .../cop/rails/three_state_boolean_column.rb | 11 ++++++++ .../rails/three_state_boolean_column_spec.rb | 27 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/config/default.yml b/config/default.yml index 477bcd7c1d..d097781ced 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1002,6 +1002,7 @@ Rails/ThreeStateBooleanColumn: StyleGuide: 'https://rails.rubystyle.guide/#three-state-boolean' Enabled: pending VersionAdded: '2.19' + StartAfter: 0 Include: - db/**/*.rb diff --git a/lib/rubocop/cop/rails/three_state_boolean_column.rb b/lib/rubocop/cop/rails/three_state_boolean_column.rb index fd27e4d9d3..69ec742ebc 100644 --- a/lib/rubocop/cop/rails/three_state_boolean_column.rb +++ b/lib/rubocop/cop/rails/three_state_boolean_column.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/rubocop/cop/rails/three_state_boolean_column_spec.rb b/spec/rubocop/cop/rails/three_state_boolean_column_spec.rb index 2ff28c861d..d240e7077e 100644 --- a/spec/rubocop/cop/rails/three_state_boolean_column_spec.rb +++ b/spec/rubocop/cop/rails/three_state_boolean_column_spec.rb @@ -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 From a7cedafa595e9ca122ad5685130b368bfaa9bb5b Mon Sep 17 00:00:00 2001 From: Sergey Toy Date: Sat, 15 Apr 2023 19:19:42 +0800 Subject: [PATCH 2/2] Add changelog record --- changelog/change_config_rails_three_state_boolean.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/change_config_rails_three_state_boolean.md diff --git a/changelog/change_config_rails_three_state_boolean.md b/changelog/change_config_rails_three_state_boolean.md new file mode 100644 index 0000000000..feba7ac1fc --- /dev/null +++ b/changelog/change_config_rails_three_state_boolean.md @@ -0,0 +1 @@ +* [#985](https://github.com/rubocop/rubocop-rails/pull/985): Add `StartAfter` config for `Rails/ThreeStateBooleanColumn` to ignore old migrations. ([@toydestroyer][])