summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/count/exact_count_strategy.rb
blob: 0b8fe640bf8f7177b0812351a2aba8a94028cfa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

module Gitlab
  module Database
    module Count
      # This strategy performs an exact count on the model.
      #
      # This is guaranteed to be accurate, however it also scans the
      # whole table. Hence, there are no guarantees with respect
      # to runtime.
      #
      # Note that for very large tables, this may even timeout.
      class ExactCountStrategy
        attr_reader :models
        def initialize(models)
          @models = models
        end

        def count
          models.each_with_object({}) do |model, data|
            data[model] = model.count
          end
        rescue *CONNECTION_ERRORS
          {}
        end
      end
    end
  end
end