Class: Rubocop::Cop::Migration::UpdateLargeTable

Inherits:
RuboCop::Cop::Base
  • Object
show all
Includes:
Gitlab::Styles::Rubocop::MigrationHelpers
Defined in:
lib/rubocop/cop/migration/update_large_table.rb

Overview

Checks for methods that may lead to batch type issues on a table that’s been explicitly denied because of its size.

Even though these methods perform functions to avoid downtime, using it with tables with millions of rows still causes a significant delay in the deploy process and is best avoided.

See gitlab.com/gitlab-com/infrastructure/issues/1602 for more information.

Examples:


# bad
class ExampleMigration < ActiveRecord::Migration[7.0]
  def change
    denied_method(:denied_table, column: :value)
  end
end

# good
class ExampleMigration < ActiveRecord::Migration[7.0]
  def change
    denied_method(:allowed_table, column: :value)
  end
end

# good
class ExampleMigration < ActiveRecord::Migration[7.0]
  def change
    allowed_method(:denied_table, column: :value)
  end
end

Constant Summary collapse

MSG =
'Using `%s` on the `%s` table will take a long time to ' \
'complete, and should be avoided unless absolutely ' \
'necessary'

Instance Method Summary collapse

Methods included from Gitlab::Styles::Rubocop::MigrationHelpers

#in_migration?

Instance Method Details

#batch_update?(node) ⇒ Object



48
49
50
51
52
# File 'lib/rubocop/cop/migration/update_large_table.rb', line 48

def_node_matcher :batch_update?, <<~PATTERN
  (send nil? ${#denied_method?}
    (sym $...)
    ...)
PATTERN

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/migration/update_large_table.rb', line 54

def on_send(node)
  return if denied_tables.empty? || denied_methods.empty?
  return unless in_migration?(node)

  matches = batch_update?(node)
  return unless matches

  update_method = matches.first
  table = matches.last.to_a.first

  return unless denied_tables.include?(table)

  add_offense(node, message: format(MSG, update_method, table))
end