2018-08-01 11:52:54 -04:00
# frozen_string_literal: true
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
2019-09-13 14:06:03 -04:00
# 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.
2018-08-01 11:52:54 -04:00
class AddReference < RuboCop :: Cop :: Cop
include MigrationHelpers
2019-09-13 14:06:03 -04:00
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-08-01 11:52:54 -04:00
2019-09-13 14:06:03 -04:00
def on_def ( node )
2018-08-01 11:52:54 -04:00
return unless in_migration? ( node )
2019-09-13 14:06:03 -04:00
new_tables = [ ]
2018-08-01 11:52:54 -04:00
2019-09-13 14:06:03 -04:00
node . each_descendant ( :send ) do | send_node |
first_arg = first_argument ( send_node )
2018-08-01 11:52:54 -04:00
2019-09-13 14:06:03 -04:00
# 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 )
add_offense ( send_node , location : :selector )
end
# We require an index on the foreign key column.
if index_missing? ( node )
add_offense ( send_node , location : :selector )
end
end
end
private
def existing_table? ( new_tables , table )
! new_tables . include? ( table )
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-08-01 11:52:54 -04:00
opts = node . children . last
2019-09-13 14:06:03 -04:00
return true if opts && opts . type == :hash
2018-08-01 11:52:54 -04:00
index_present = false
opts . each_node ( :pair ) do | pair |
index_present || = index_enabled? ( pair )
end
2019-09-13 14:06:03 -04:00
! index_present
2018-08-01 11:52:54 -04:00
end
def index_enabled? ( pair )
2018-11-22 15:13:36 -05:00
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-08-01 11:52:54 -04:00
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