Sha256: 7b5065a7153df516760a9f8f5a6566ea52f6f28a4c89541281acc60a8fb2a950

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require_relative '../../../gitlab/styles/rubocop/migration_helpers'

module Rubocop
  module Cop
    module Migration
      # Checks for methods that may lead to batch type issues on a table that's been
      # explicitly denied because of its size.
      #
      # Even though though these methods perform functions 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.
      class UpdateLargeTable < RuboCop::Cop::Base
        include Gitlab::Styles::Rubocop::MigrationHelpers

        MSG = 'Using `%s` on the `%s` table will take a long time to ' \
          'complete, and should be avoided unless absolutely ' \
          'necessary'

        # @!method batch_update?(node)
        def_node_matcher :batch_update?, <<~PATTERN
          (send nil? ${#denied_method?}
            (sym $...)
            ...)
        PATTERN

        def on_send(node)
          return if denied_tables.empty? || denied_methods.empty?
          return unless in_migration?(node)

          matches = batch_update?(node)
          return unless matches

          update_method = matches.first
          table = matches.last.to_a.first

          return unless denied_tables.include?(table)

          add_offense(node, message: format(MSG, update_method, table))
        end

        private

        def denied_tables
          cop_config['DeniedTables'] || []
        end

        def denied_method?(method_name)
          denied_methods.include?(method_name)
        end

        def denied_methods
          cop_config['DeniedMethods'] || []
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/migration/update_large_table.rb
gitlab-styles-13.0.0 lib/rubocop/cop/migration/update_large_table.rb