Class: RuboCop::Cop::Migration::AddForeignKey

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/migration/add_foreign_key.rb

Overview

Activate foreign key validation in a separate migration in PostgreSQL.

To avoid blocking writes on both tables.

Examples:

# bad
class AddForeignKeyFromArticlesToUsers < ActiveRecord::Migration[7.0]
  def change
    add_foreign_key :articles, :users
  end
end

# good
class AddForeignKeyFromArticlesToUsersWithoutValidation < ActiveRecord::Migration[7.0]
  def change
    add_foreign_key :articles, :users, validate: false
  end
end

class ActivateForeignKeyValidationFromArticlesToUsers < ActiveRecord::Migration[7.0]
  def change
    validate_foreign_key :articles, :users
  end
end

Constant Summary collapse

MSG =
'Activate foreign key validation in a separate migration in PostgreSQL.'
RESTRICT_ON_SEND =
%i[
  add_foreign_key
  add_reference
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


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

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

  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end