Class: RuboCop::Cop::Spbtv::Postgres::AddColumnWithNotNull

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

Overview

Do not add a NOT NULL column.

This will have the same problem, as “Add a column with a default”. To make this operation without locking, you can create a new table with the addition of the non-nullable column, write to both tables, backfill, and then switch to the new table. This workaround is incredibly onerous and need two times more space than is a table takes.

Examples:

@bad
add_column :users, :name, :string, null: false

@good
add_column :users, :name, :string

Constant Summary collapse

MSG =
'Do not add a NOT NULL column.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/spbtv/postgres/add_column_with_not_null.rb', line 33

def on_send(node)
  pairs = add_not_null_column?(node)
  return unless pairs

  null_false = pairs.detect { |pair| null_false?(pair) }
  return unless null_false

  add_offense(null_false, :expression)
end