Class: Gitlab::Database::NamespaceProjectIdsEachBatch

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/namespace_project_ids_each_batch.rb

Overview

This class utilizes NamespaceEachBatch to return batches of project IDs. It accepts a namespace_id and an optional resolver for narrowing the project IDs returned to a subset filtered per batch.

Usage:

# To invoke, pass any group_id Gitlab::Database::NamespaceProjectIdsEachBatch.new(group_id: 42).execute

> [1, 2, 3]

# To invoke, pass any group_id and an optional resolver resolver = ->(batch) { ProjectSetting.for_projects(batch).has_vulnerabilities.pluck_primary_key } Gitlab::Database::NamespaceProjectIdsEachBatch.new(group_id: 42, resolver: resolver).execute

> [1, 3]

Instance Method Summary collapse

Constructor Details

#initialize(group_id:, resolver: nil, batch_size: 100) ⇒ NamespaceProjectIdsEachBatch



21
22
23
24
25
# File 'lib/gitlab/database/namespace_project_ids_each_batch.rb', line 21

def initialize(group_id:, resolver: nil, batch_size: 100)
  @group_id = group_id
  @resolver = resolver
  @batch_size = batch_size
end

Instance Method Details

#direct_project_ids_for(sub_group_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/database/namespace_project_ids_each_batch.rb', line 46

def direct_project_ids_for(sub_group_id)
  project_ids = []

  Project.in_namespace(sub_group_id).each_batch(of: @batch_size) do |batch|
    project_ids += if @resolver
                     @resolver.call(batch)
                   else
                     batch.pluck_primary_key
                   end
  end

  project_ids
end

#executeObject



27
28
29
30
31
32
33
# File 'lib/gitlab/database/namespace_project_ids_each_batch.rb', line 27

def execute
  return [] unless @group_id

  subgroup_ids.flat_map do |sub_group_id|
    direct_project_ids_for(sub_group_id)
  end
end

#subgroup_idsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/database/namespace_project_ids_each_batch.rb', line 35

def subgroup_ids
  cursor = { current_id: @group_id, depth: [@group_id] }
  iterator = NamespaceEachBatch.new(namespace_class: Group, cursor: cursor)

  group_ids = []

  iterator.each_batch(of: @batch_size) { |ids| group_ids += ids }

  group_ids
end