Class: RuboCop::Cop::Migration::AlwaysBulkChangeTable

Inherits:
Rails::BulkChangeTable
  • Object
show all
Defined in:
lib/rubocop/cop/migration/always_bulk_change_table.rb

Overview

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.

Constant Summary collapse

MSG =
'Add `bulk: true` when using change_table.'
RESTRICT_ON_SEND =
%i[change_table].freeze

Instance Method Summary collapse

Instance Method Details

#bulk_options_true?(node) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/rubocop/cop/migration/always_bulk_change_table.rb', line 38

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

#on_send(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/migration/always_bulk_change_table.rb', line 27

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