2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
# Cop that checks if columns are added in a way that doesn't require
|
|
|
|
# downtime.
|
|
|
|
class AddColumnWithDefault < RuboCop::Cop::Cop
|
|
|
|
include MigrationHelpers
|
|
|
|
|
|
|
|
MSG = '`add_column_with_default` without `allow_null: true` may cause prolonged lock situations and downtime, ' \
|
|
|
|
'see https://gitlab.com/gitlab-org/gitlab/issues/38060'.freeze
|
|
|
|
|
|
|
|
def_node_matcher :add_column_with_default?, <<~PATTERN
|
|
|
|
(send _ :add_column_with_default $_ ... (hash $...))
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
|
|
|
add_column_with_default?(node) do |table, options|
|
2020-04-08 14:13:33 +05:30
|
|
|
add_offense(node, location: :selector) if offensive?(table, options)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def offensive?(table, options)
|
|
|
|
table_blacklisted?(table) && !nulls_allowed?(options)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def nulls_allowed?(options)
|
|
|
|
options.find { |opt| opt.key.value == :allow_null && opt.value.true_type? }
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def table_blacklisted?(symbol)
|
2020-03-13 15:44:24 +05:30
|
|
|
symbol && symbol.type == :sym &&
|
2020-04-08 14:13:33 +05:30
|
|
|
BLACKLISTED_TABLES.include?(symbol.children[0])
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|