Class: RuboCop::Cop::DevDoc::Migration::NoBulkChangeTable

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
HashPairRemoval
Defined in:
lib/rubocop/cop/dev_doc/migration/no_bulk_change_table.rb

Overview

Avoid the bulk: option on change_table.

Rationale

bulk: true combines multiple ALTER TABLE sub-commands into a single statement. That is a MySQL/MariaDB optimization — it avoids repeated full-table rewrites there — but consuming projects run PostgreSQL, where ALTER sub-commands are cheap metadata operations and bulk: is a no-op that only adds noise.

Worse, the combined form conflicts with the standard add-column-then-drop-default backfill pattern: inside a bulk: true block it trips DevDoc/Migration/AvoidColumnDefault (on the backfill default:) and Rails/ReversibleMigration (on t.change_default), neither of which can be disabled inline.


change_table :users, bulk: true do |t|
t.string :token
t.datetime :closed_at
end

✔️
change_table :users do |t|
t.string :token
t.datetime :closed_at
end

Relationship with Rails/BulkChangeTable

This cop is the exact inverse of Rails/BulkChangeTable, which DEMANDS bulk: true whenever several ALTERs touch one table. The two cannot both be enabled. This gem's default config disables Rails/BulkChangeTable, but whether that default reaches a project is plugin-load-order dependent — the last-loaded extension's default wins, so a project listing rubocop-dev_doc before rubocop-rails still gets rubocop-rails' Enabled: true. Disable it explicitly in the project's .rubocop.yml rather than relying on load order.

NOTE: When a comment sits between change_table's arguments, the cop flags the option but does not autocorrect (removing the range would corrupt the comment) — delete the option manually.

Examples:

# bad
change_table :users, bulk: true do |t|
  t.string :token
end

# good
change_table :users do |t|
  t.string :token
end

Constant Summary collapse

MSG =
'Avoid `bulk:` on `change_table` — a MySQL optimization that is a no-op on PostgreSQL.'.freeze
RESTRICT_ON_SEND =
%i[change_table].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



65
66
67
68
69
70
# File 'lib/rubocop/cop/dev_doc/migration/no_bulk_change_table.rb', line 65

def on_send(node)
  options = node.arguments.find(&:hash_type?)
  return unless options

  options.pairs.each { |pair| check_pair(node, options, pair) }
end