Class: RuboCop::Cop::Spbtv::Postgres::AddColumnWithDefault

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/spbtv/postgres/add_column_with_default.rb

Overview

Do not add a column with a default value.

Adding a column with a default requires updating each row of the table (to store the new column value). For big table this will create long running operation that locks it.

So if you intend to fill the column with mostly non default values, it’s best to add the column with no default, insert the correct values using UPDATE (correct way is to do batched updates, for example, update 1000 rows at a time, because big update will create table-wide lock), and then add any desired default.

Examples:

@bad
add_column :users, :name, :string, default: "Peter"

@good
add_column :users, :name, :string
User.find_in_batches do |batch|
  batch.update_all(name: 'Peter')
end

Constant Summary collapse

MSG =
'Do not add a column with a default value.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/spbtv/postgres/add_column_with_default.rb', line 40

def on_send(node)
  pairs = add_column_with_default?(node)
  return unless pairs
  has_default = pairs.detect { |pair| has_default?(pair) }

  return unless has_default

  add_offense(has_default, :expression)
end