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.

Note that since this cop goes to read arbitrary model files, false negatives occur in situations where RuboCop’s cache is being used.

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 Also known as: on_csend

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


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

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

  add_offense(node)
end