Class: RuboCop::Cop::Migration::RemoveColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/migration/remove_column.rb

Overview

Make sure the column is already ignored by the running app before removing it.

Active Record caches database columns at runtime, so if you drop a column, it can cause exceptions until your app reboots.

Examples:

# bad
class User < ApplicationRecord
end

class RemoveUsersSomeColumn < ActiveRecord::Migration[7.0]
  def change
    remove_column :users, :some_column
  end
end

# good
class User < ApplicationRecord
  self.ignored_columns += %w[some_column]
end

class RemoveUsersSomeColumn < ActiveRecord::Migration[7.0]
  def change
    remove_column :users, :some_column
  end
end

Defined Under Namespace

Classes: Parser

Constant Summary collapse

MSG =
'Make sure the column is already ignored by the running app before removing it.'
RESTRICT_ON_SEND =
%i[
  remove_column
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


46
47
48
49
50
# File 'lib/rubocop/cop/migration/remove_column.rb', line 46

def on_send(node)
  return unless bad?(node)

  add_offense(node)
end