summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb
blob: 1d96872d44540eccda2209061d37c7647f903622 (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 BackgroundMigration
    SUB_BATCH_SIZE = 1_000

    # The class to populates the total projects counter cache of topics
    class PopulateTopicsTotalProjectsCountCache
      # Temporary AR model for topics
      class Topic < ActiveRecord::Base
        include EachBatch

        self.table_name = 'topics'
      end

      def perform(start_id, stop_id)
        Topic.where(id: start_id..stop_id).each_batch(of: SUB_BATCH_SIZE) do |batch|
          ActiveRecord::Base.connection.execute(<<~SQL)
            WITH batched_relation AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (#{batch.select(:id).limit(SUB_BATCH_SIZE).to_sql})
            UPDATE topics
            SET total_projects_count = (SELECT COUNT(*) FROM project_topics WHERE topic_id = batched_relation.id)
            FROM batched_relation
            WHERE topics.id = batched_relation.id
          SQL
        end
      end
    end
  end
end