Class: Gitlab::BackgroundMigration::BatchingStrategies::PrimaryKeyBatchingStrategy

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

Overview

Generic batching class for use with a BatchedBackgroundMigration. Batches over the given table and column combination, returning the MIN() and MAX() values for the next batch as an array.

If no more batches exist in the table, returns nil.

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

Methods inherited from BaseStrategy

#initialize

Constructor Details

This class inherits a constructor from Gitlab::BackgroundMigration::BatchingStrategies::BaseStrategy

Instance Method Details

#apply_additional_filters(relation, job_arguments: []) ⇒ Object

Strategies based on PrimaryKeyBatchingStrategy can use this method to easily apply additional filters.

Example:

class MatchingType < PrimaryKeyBatchingStrategy
  def apply_additional_filters(relation, job_arguments:)
    type = job_arguments.first

    relation.where(type: type)
  end
end

50
51
52
# File 'lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy.rb', line 50

def apply_additional_filters(relation, job_arguments: [])
  relation
end

#next_batch(table_name, column_name, batch_min_value:, batch_size:, job_arguments:) ⇒ Object

Finds and returns the next batch in the table.

table_name - The table to batch over column_name - The column to batch over batch_min_value - The minimum value which the next batch will start at batch_size - The size of the next batch job_arguments - The migration job arguments


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/background_migration/batching_strategies/primary_key_batching_strategy.rb', line 21

def next_batch(table_name, column_name, batch_min_value:, batch_size:, job_arguments:)
  model_class = define_batchable_model(table_name, connection: connection)

  quoted_column_name = model_class.connection.quote_column_name(column_name)
  relation = model_class.where("#{quoted_column_name} >= ?", batch_min_value)
  relation = apply_additional_filters(relation, job_arguments: job_arguments)
  next_batch_bounds = nil

  relation.each_batch(of: batch_size, column: column_name) do |batch| # rubocop:disable Lint/UnreachableLoop
    next_batch_bounds = batch.pluck(Arel.sql("MIN(#{quoted_column_name}), MAX(#{quoted_column_name})")).first

    break
  end

  next_batch_bounds
end