-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLTF-826 - Added Cop
Migration/UseChangeTableBulk
(#7)
* add bulk_change_table fix cop name default.yml fix rubocop offenses * change name to avoid collision & change default change name in changelog
- Loading branch information
1 parent
0b4f0d7
commit 40f1043
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Migration | ||
# Use `bulk: true` with `change_table`. | ||
# # bad | ||
# | ||
# change_table :my_table | ||
# | ||
# # good | ||
# | ||
# change_table :my_table, bulk: true | ||
class UseChangeTableBulk < Base | ||
MSG = 'Use `change_table` with `bulk: true`.' | ||
RESTRICT_ON_SEND = %i[change_table].freeze | ||
|
||
def_node_matcher :use_bulk?, <<~PATTERN | ||
(send nil? :change_table _ (hash <(pair (sym :bulk) true) ...>) ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if use_bulk?(node) | ||
|
||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Migration::UseChangeTableBulk, :config do | ||
context 'without change_table' do | ||
it 'does not register an offense' do | ||
expect_no_offenses <<~RUBY | ||
class MyMigration < ActiveRecord::Migration[5.2] | ||
def up | ||
my_variable = :test | ||
some_function() | ||
MyClass.stuff() | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with change_table' do | ||
context 'when using bulk: true' do | ||
it 'does not register an offense' do | ||
expect_no_offenses <<~RUBY | ||
class MyMigration < ActiveRecord::Migration[5.2] | ||
def up | ||
change_table :users, some_other_param: 'stuff', bulk: true, another_param: :some_sym do | ||
do_stuff() | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when using bulk: false' do | ||
it 'registers an offense' do | ||
expect_offense <<~RUBY | ||
class MyMigration < ActiveRecord::Migration[5.2] | ||
def up | ||
change_table :users, some_other_param: 'stuff', bulk: false, another_param: :some_sym do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `change_table` with `bulk: true`. | ||
do_stuff() | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when bulk is omitted' do | ||
it 'registers an offense' do | ||
expect_offense <<~RUBY | ||
class MyMigration < ActiveRecord::Migration[5.2] | ||
def up | ||
change_table :users, some_other_param: 'stuff', another_param: :some_sym do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `change_table` with `bulk: true`. | ||
do_stuff() | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |