Skip to content

Commit

Permalink
PLTF-826 - Added Cop Migration/UseChangeTableBulk (#7)
Browse files Browse the repository at this point in the history
* 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
mduchenesavard authored Jan 10, 2022
1 parent 0b4f0d7 commit 40f1043
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# main

* Added Cop `Migration/UseChangeTableBulk` ([#7](https://github.com/petalmd/rubocop-petal/pull/7))
* Added cop `Grape/HelpersIncludeModule` ([#1](https://github.com/petalmd/rubocop-petal/pull/1))

# v0.1.0
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Migration/UseChangeTableBulk:
Description: 'Enforces the use of option `bulk: true` with `change_table`'
Enabled: true
Include:
- db/migrate/**

Grape/HelpersIncludeModule:
Description: 'Prevent using helpers with block to include module'
Enabled: true
Expand Down
30 changes: 30 additions & 0 deletions lib/rubocop/cop/migration/use_change_table_bulk.rb
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
63 changes: 63 additions & 0 deletions spec/rubocop/cop/migration/use_change_table_bulk_spec.rb
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

0 comments on commit 40f1043

Please sign in to comment.