2017-08-17 22:00:37 +05:30
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
# This cop checks for `add_column_with_default` on a table that's been
|
|
|
|
# explicitly blacklisted because of its size.
|
|
|
|
#
|
|
|
|
# Even though this helper performs the update in batches to avoid
|
|
|
|
# downtime, using it with tables with millions of rows still causes a
|
|
|
|
# significant delay in the deploy process and is best avoided.
|
|
|
|
#
|
|
|
|
# See https://gitlab.com/gitlab-com/infrastructure/issues/1602 for more
|
|
|
|
# information.
|
2018-03-17 18:26:18 +05:30
|
|
|
class UpdateLargeTable < RuboCop::Cop::Cop
|
2017-08-17 22:00:37 +05:30
|
|
|
include MigrationHelpers
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
MSG = 'Using `%s` on the `%s` table will take a long time to ' \
|
|
|
|
'complete, and should be avoided unless absolutely ' \
|
|
|
|
'necessary'.freeze
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
BATCH_UPDATE_METHODS = %w[
|
|
|
|
:add_column_with_default
|
|
|
|
:change_column_type_concurrently
|
|
|
|
:rename_column_concurrently
|
|
|
|
:update_column_in_batches
|
|
|
|
].join(' ').freeze
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def_node_matcher :batch_update?, <<~PATTERN
|
2018-11-08 19:23:39 +05:30
|
|
|
(send nil? ${#{BATCH_UPDATE_METHODS}} $(sym ...) ...)
|
2017-08-17 22:00:37 +05:30
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
matches = batch_update?(node)
|
|
|
|
return unless matches
|
|
|
|
|
|
|
|
update_method = matches.first
|
|
|
|
table = matches.last.to_a.first
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
return unless BLACKLISTED_TABLES.include?(table)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
add_offense(node, location: :expression, message: format(MSG, update_method, table))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|