debian-mirror-gitlab/rubocop/cop/migration/add_column.rb

56 lines
1.4 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require_relative '../../migration_helpers'
2016-08-24 12:49:21 +05:30
module RuboCop
module Cop
module Migration
# Cop that checks if columns are added in a way that doesn't require
# downtime.
2017-08-17 22:00:37 +05:30
class AddColumn < RuboCop::Cop::Cop
2016-08-24 12:49:21 +05:30
include MigrationHelpers
2020-01-01 13:55:28 +05:30
WHITELISTED_TABLES = %i[
application_settings
plan_limits
].freeze
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
MSG = '`add_column` with a default value requires downtime, ' \
'use `add_column_with_default` instead'.freeze
2016-08-24 12:49:21 +05:30
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
2018-03-17 18:26:18 +05:30
add_offense(node, location: :selector)
2016-08-24 12:49:21 +05:30
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