-
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.
Add Migration/AlwaysBulkChangeTable (#46)
- Loading branch information
Showing
4 changed files
with
83 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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Migration | ||
# Using `bulk: true` with `change_table` is recommended. | ||
# Without `bulk: true`, `change_table` generates multiple | ||
# `ALTER TABLE` statements and the migration process will | ||
# be duplicated. | ||
# | ||
# @bad | ||
# change_table :users do |t| | ||
# t.string :first_name | ||
# t.string :last_name | ||
# end | ||
# | ||
# @good | ||
# change_table :users, bulk: true do |t| | ||
# t.string :first_name | ||
# t.string :last_name | ||
# end | ||
# | ||
class AlwaysBulkChangeTable < Rails::BulkChangeTable | ||
MSG = 'Add `bulk: true` when using change_table.' | ||
RESTRICT_ON_SEND = %i[change_table].freeze | ||
|
||
def on_send(node) | ||
return unless node.command?(:change_table) | ||
|
||
unless include_bulk_options?(node) | ||
add_offense(node) | ||
return | ||
end | ||
|
||
add_offense(node) unless bulk_options_true?(node) | ||
end | ||
|
||
def bulk_options_true?(node) | ||
options = node.arguments[1] | ||
options.each_pair do |key, value| | ||
return true if key.value == :bulk && value.true_type? | ||
end | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/rubocop/cop/migration/always_bulk_change_table_spec.rb
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Migration::AlwaysBulkChangeTable, :config do | ||
it 'registers an offense when not using bulk: true' do | ||
expect_offense(<<~RUBY) | ||
change_table :users do |t| | ||
^^^^^^^^^^^^^^^^^^^ Add `bulk: true` when using change_table. | ||
t.string :name | ||
end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
change_table :users, bulk: false do |t| | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add `bulk: true` when using change_table. | ||
t.string :name | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using change_table and bulk true' do | ||
expect_no_offenses(<<~RUBY) | ||
change_table :users, bulk: true do |t| | ||
t.string :name | ||
end | ||
RUBY | ||
end | ||
end |