Class: Gitlab::BackgroundMigration::BackfillNamespaceTraversalIdsRoots

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/backfill_namespace_traversal_ids_roots.rb

Overview

A job to set namespaces.traversal_ids in sub-batches, of all namespaces without a parent and not already set. rubocop:disable Style/Documentation

Defined Under Namespace

Classes: Namespace

Constant Summary collapse

PAUSE_SECONDS =
0.1

Instance Method Summary collapse

Instance Method Details

#perform(start_id, end_id, sub_batch_size) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/background_migration/backfill_namespace_traversal_ids_roots.rb', line 19

def perform(start_id, end_id, sub_batch_size)
  ranged_query = Namespace.base_query
    .where(id: start_id..end_id)
    .where("traversal_ids = '{}'")

  ranged_query.each_batch(of: sub_batch_size) do |sub_batch|
    first, last = sub_batch.pluck(Arel.sql('min(id), max(id)')).first

    # The query need to be reconstructed because .each_batch modifies the default scope
    # See: https://gitlab.com/gitlab-org/gitlab/-/issues/330510
    Namespace.unscoped
             .base_query
             .where(id: first..last)
             .where("traversal_ids = '{}'")
             .update_all('traversal_ids = ARRAY[id]')

    sleep PAUSE_SECONDS
  end

  mark_job_as_succeeded(start_id, end_id, sub_batch_size)
end