Class: Gitlab::BackgroundMigration::BackfillNamespaceIdForProjectRoute

Inherits:
Object
  • Object
show all
Includes:
Database::DynamicModelHelpers
Defined in:
lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb

Overview

Backfills the ‘routes.namespace_id` column, by setting it to project.project_namespace_id

Constant Summary

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods included from Database::DynamicModelHelpers

#define_batchable_model, #each_batch, #each_batch_range

Instance Method Details

#batch_metricsObject



32
33
34
# File 'lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb', line 32

def batch_metrics
  @batch_metrics ||= Gitlab::Database::BackgroundMigration::BatchMetrics.new
end

#perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb', line 9

def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms)
  parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)

  parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
    cleanup_gin_index('routes')

    batch_metrics.time_operation(:update_all) do
      ApplicationRecord.connection.execute <<~SQL
        WITH route_and_ns(route_id, project_namespace_id) AS MATERIALIZED (
          #{sub_batch.to_sql}
        )
        UPDATE routes
        SET namespace_id = route_and_ns.project_namespace_id
        FROM route_and_ns
        WHERE id = route_and_ns.route_id
      SQL
    end

    pause_ms = [0, pause_ms].max
    sleep(pause_ms * 0.001)
  end
end