Welcome to mirror list, hosted at ThFree Co, Russian Federation.

backfill_group_features.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35b5282360f5d581a5fa9bad5afed2a1d91769f6 (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
30
31
32
33
34
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Backfill group_features for an array of groups
    class BackfillGroupFeatures < ::Gitlab::BackgroundMigration::BatchedMigrationJob
      job_arguments :batch_size

      def perform
        each_sub_batch(
          operation_name: :upsert_group_features,
          batching_arguments: { order_hint: :type },
          batching_scope: ->(relation) { relation.where(type: 'Group') }
        ) do |sub_batch|
          upsert_group_features(sub_batch)
        end
      end

      private

      def upsert_group_features(relation)
        connection.execute(
          <<~SQL
          INSERT INTO group_features (group_id, created_at, updated_at)
          SELECT namespaces.id as group_id, now(), now()
          FROM namespaces
          WHERE namespaces.type = 'Group' AND namespaces.id IN(#{relation.select(:id).limit(batch_size).to_sql})
          ON CONFLICT (group_id) DO NOTHING;
          SQL
        )
      end
    end
  end
end