2017-02-08 17:31:15 -05:00
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
2016-06-24 11:26:28 -04:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
# Cop that checks if columns are added in a way that doesn't require
|
|
|
|
# downtime.
|
2017-02-08 17:17:15 -05:00
|
|
|
class AddColumn < RuboCop::Cop::Cop
|
2016-06-24 11:26:28 -04:00
|
|
|
include MigrationHelpers
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
WHITELISTED_TABLES = [:application_settings].freeze
|
2016-06-24 11:26:28 -04:00
|
|
|
|
2017-02-08 17:17:15 -05:00
|
|
|
MSG = '`add_column` with a default value requires downtime, ' \
|
2017-02-21 18:32:18 -05:00
|
|
|
'use `add_column_with_default` instead'.freeze
|
2016-06-24 11:26:28 -04:00
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
|
|
|
name = node.children[1]
|
|
|
|
|
|
|
|
return unless name == :add_column
|
|
|
|
|
|
|
|
# Ignore whitelisted tables.
|
|
|
|
return if table_whitelisted?(node.children[2])
|
|
|
|
|
|
|
|
opts = node.children.last
|
|
|
|
|
|
|
|
return unless opts && opts.type == :hash
|
|
|
|
|
|
|
|
opts.each_node(:pair) do |pair|
|
|
|
|
if hash_key_type(pair) == :sym && hash_key_name(pair) == :default
|
|
|
|
add_offense(node, :selector)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def table_whitelisted?(symbol)
|
|
|
|
symbol && symbol.type == :sym &&
|
|
|
|
WHITELISTED_TABLES.include?(symbol.children[0])
|
|
|
|
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
|