Skip to content

Commit 07bec34

Browse files
authored
Merge pull request #1284 from ccutrer/fix-change-column-null-in-bulk-change-table
[Fix #1280] Handle change_column_null for BulkChangeTable
2 parents 7c4ad0c + e71b373 commit 07bec34

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1280](https://github.com/rubocop/rubocop-rails/issues/1280): Look for change_column_null for `BulkChangeTable`. ([@ccutrer][])

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ class BulkChangeTable < Base
113113
MYSQL_COMBINABLE_ALTER_METHODS = %i[rename_column add_index remove_index].freeze
114114

115115
POSTGRESQL_COMBINABLE_TRANSFORMATIONS = %i[change_default].freeze
116+
POSTGRESQL_COMBINABLE_TRANSFORMATIONS_SINCE_6_1 = %i[change_null].freeze
116117

117118
POSTGRESQL_COMBINABLE_ALTER_METHODS = %i[change_column_default].freeze
119+
POSTGRESQL_COMBINABLE_ALTER_METHODS_SINCE_6_1 = %i[change_column_null].freeze
118120

119121
def on_def(node)
120122
return unless support_bulk_alter?
@@ -196,7 +198,9 @@ def combinable_alter_methods
196198
when MYSQL
197199
COMBINABLE_ALTER_METHODS + MYSQL_COMBINABLE_ALTER_METHODS
198200
when POSTGRESQL
199-
COMBINABLE_ALTER_METHODS + POSTGRESQL_COMBINABLE_ALTER_METHODS
201+
result = COMBINABLE_ALTER_METHODS + POSTGRESQL_COMBINABLE_ALTER_METHODS
202+
result += POSTGRESQL_COMBINABLE_ALTER_METHODS_SINCE_6_1 if target_rails_version >= 6.1
203+
result
200204
end
201205
end
202206

@@ -205,7 +209,9 @@ def combinable_transformations
205209
when MYSQL
206210
COMBINABLE_TRANSFORMATIONS + MYSQL_COMBINABLE_TRANSFORMATIONS
207211
when POSTGRESQL
208-
COMBINABLE_TRANSFORMATIONS + POSTGRESQL_COMBINABLE_TRANSFORMATIONS
212+
result = COMBINABLE_TRANSFORMATIONS + POSTGRESQL_COMBINABLE_TRANSFORMATIONS
213+
result += POSTGRESQL_COMBINABLE_TRANSFORMATIONS_SINCE_6_1 if target_rails_version >= 6.1
214+
result
209215
end
210216
end
211217

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,26 @@ def change
416416
end
417417
RUBY
418418
end
419+
420+
it 'does not register an offense for multiple `change_column_null`' do
421+
expect_no_offenses(<<~RUBY)
422+
def change
423+
change_column_null :users, :name, false
424+
change_column_null :users, :address, false
425+
end
426+
RUBY
427+
end
428+
429+
it 'does not register an offense for multiple `t.change_null`' do
430+
expect_no_offenses(<<~RUBY)
431+
def change
432+
change_table :users do |t|
433+
t.change_null :name, false
434+
t.change_null :address, false
435+
end
436+
end
437+
RUBY
438+
end
419439
end
420440

421441
context 'when database is PostgreSQL' do
@@ -430,13 +450,68 @@ def change
430450
it_behaves_like 'offense'
431451
it_behaves_like 'no offense for mysql'
432452
it_behaves_like 'offense for postgresql'
453+
454+
it 'does not register an offense for multiple `change_column_null`' do
455+
expect_no_offenses(<<~RUBY)
456+
def change
457+
change_column_null :users, :name, false
458+
change_column_null :users, :address, false
459+
end
460+
RUBY
461+
end
462+
463+
it 'does not register an offense for multiple `t.change_null`' do
464+
expect_no_offenses(<<~RUBY)
465+
def change
466+
change_table :users do |t|
467+
t.change_null :name, false
468+
t.change_null :address, false
469+
end
470+
end
471+
RUBY
472+
end
433473
end
434474

435475
context 'with Rails 5.1', :rails51 do
436476
it_behaves_like 'no offense'
437477
it_behaves_like 'no offense for mysql'
438478
it_behaves_like 'no offense for postgresql'
439479
end
480+
481+
context 'with Rails 6.1', :rails61 do
482+
it 'registers an offense for multiple `change_column_null`' do
483+
expect_offense(<<~RUBY)
484+
def change
485+
change_column_null :users, :name, false
486+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ You can use `change_table :users, bulk: true` to combine alter queries.
487+
change_column_null :users, :address, false
488+
end
489+
RUBY
490+
end
491+
492+
it 'registers an offense for multiple `t.change_null`' do
493+
expect_offense(<<~RUBY)
494+
def change
495+
change_table :users do |t|
496+
^^^^^^^^^^^^^^^^^^^ You can combine alter queries using `bulk: true` options.
497+
t.change_null :name, false
498+
t.change_null :address, false
499+
end
500+
end
501+
RUBY
502+
end
503+
504+
it 'does not register an offense for multiple `t.change_null` with `bulk: true`' do
505+
expect_no_offenses(<<~RUBY)
506+
def change
507+
change_table :users, bulk: true do |t|
508+
t.change_null :name, false
509+
t.change_null :address, false
510+
end
511+
end
512+
RUBY
513+
end
514+
end
440515
end
441516

442517
context 'when `database.yml` is exists' do

0 commit comments

Comments
 (0)