2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
2019-12-04 20:38:33 +05:30
# add_reference can only be used with newly created tables.
# Additionally, the cop here checks that we create an index for the foreign key, too.
2022-10-11 01:57:18 +05:30
class AddReference < RuboCop :: Cop :: Base
2018-11-20 20:47:30 +05:30
include MigrationHelpers
2019-12-04 20:38:33 +05:30
MSG = '`add_reference` requires downtime for existing tables, use `add_concurrent_foreign_key` instead. When used for new tables, `index: true` or `index: { options... } is required.`'
2018-11-20 20:47:30 +05:30
2019-12-04 20:38:33 +05:30
def on_def ( node )
2018-11-20 20:47:30 +05:30
return unless in_migration? ( node )
2019-12-04 20:38:33 +05:30
new_tables = [ ]
2018-11-20 20:47:30 +05:30
2019-12-04 20:38:33 +05:30
node . each_descendant ( :send ) do | send_node |
first_arg = first_argument ( send_node )
2018-11-20 20:47:30 +05:30
2019-12-04 20:38:33 +05:30
# The first argument of "create_table" / "add_reference" is the table
# name.
new_tables << first_arg if create_table? ( send_node )
next if method_name ( send_node ) != :add_reference
# Using "add_reference" is fine for newly created tables as there's no
# data in these tables yet.
if existing_table? ( new_tables , first_arg )
2022-10-11 01:57:18 +05:30
add_offense ( send_node . loc . selector )
2019-12-04 20:38:33 +05:30
end
# We require an index on the foreign key column.
if index_missing? ( node )
2022-10-11 01:57:18 +05:30
add_offense ( send_node . loc . selector )
2019-12-04 20:38:33 +05:30
end
end
end
private
def existing_table? ( new_tables , table )
2023-05-27 22:25:52 +05:30
! new_tables . include? ( table ) # rubocop:disable Rails/NegateInclude
2019-12-04 20:38:33 +05:30
end
def create_table? ( node )
method_name ( node ) == :create_table
end
def method_name ( node )
node . children [ 1 ]
end
def first_argument ( node )
node . children [ 2 ]
end
def index_missing? ( node )
2018-11-20 20:47:30 +05:30
opts = node . children . last
2019-12-04 20:38:33 +05:30
return true if opts && opts . type == :hash
2018-11-20 20:47:30 +05:30
index_present = false
opts . each_node ( :pair ) do | pair |
index_present || = index_enabled? ( pair )
end
2019-12-04 20:38:33 +05:30
! index_present
2018-11-20 20:47:30 +05:30
end
def index_enabled? ( pair )
2019-02-15 15:39:39 +05:30
return unless hash_key_type ( pair ) == :sym
return unless hash_key_name ( pair ) == :index
index = pair . children [ 1 ]
index . true_type? || index . hash_type?
2018-11-20 20:47:30 +05:30
end
def hash_key_type ( pair )
pair . children [ 0 ] . type
end
def hash_key_name ( pair )
pair . children [ 0 ] . children [ 0 ]
end
end
end
end
end