Class: RuboCop::Cop::Rails::UnusedIgnoredColumns

Inherits:
Base
  • Object
show all
Includes:
ActiveRecordHelper
Defined in:
lib/rubocop/cop/rails/unused_ignored_columns.rb

Overview

Suggests you remove a column that does not exist in the schema from ‘ignored_columns`. `ignored_columns` is necessary to drop a column from RDBMS, but you don’t need it after the migration to drop the column. You avoid forgetting to remove ‘ignored_columns` by this cop.

IMPORTANT: This cop can’t be used to effectively check for unused columns because the development and production schema can be out of sync until the migration has been run on production. As such, this cop can cause ‘ignored_columns` to be removed even though the production schema still contains the column, which can lead to downtime when the migration is actually executed. Only enable this cop if you know your migrations will be run before any of your Rails applications boot with the modified code.

Examples:

# bad
class User < ApplicationRecord
  self.ignored_columns = [:already_removed_column]
end

# good
class User < ApplicationRecord
  self.ignored_columns = [:still_existing_column]
end

Constant Summary collapse

MSG =
'Remove `%<column_name>s` from `ignored_columns` because the column does not exist.'
RESTRICT_ON_SEND =
%i[ignored_columns=].freeze

Constants included from ActiveRecordHelper

ActiveRecordHelper::WHERE_METHODS

Instance Method Summary collapse

Methods included from ActiveRecordHelper

#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_op_asgn



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/rails/unused_ignored_columns.rb', line 45

def on_send(node)
  return unless (columns = ignored_columns(node) || appended_ignored_columns(node))
  return unless schema

  table = table(node)
  return unless table

  columns.children.each do |column_node|
    check_column_existence(column_node, table)
  end
end