Module: Gitlab::Database::DynamicModelHelpers

Constant Summary collapse

BATCH_SIZE =
1_000

Instance Method Summary collapse

Instance Method Details

#define_batchable_model(table_name, connection:) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 8

def define_batchable_model(table_name, connection:)
  klass = Class.new(ActiveRecord::Base) do
    include EachBatch

    self.table_name = table_name
    self.inheritance_column = :_type_disabled
  end

  klass.connection = connection
  klass
end

#each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 20

def each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE)
  if transaction_open?
    raise <<~MSG.squish
      each_batch should not run inside a transaction, you can disable
      transactions by calling disable_ddl_transaction! in the body of
      your migration class
    MSG
  end

  scope.call(define_batchable_model(table_name, connection: connection))
    .each_batch(of: of) { |batch| yield batch }
end

#each_batch_range(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE) ⇒ Object


33
34
35
36
37
# File 'lib/gitlab/database/dynamic_model_helpers.rb', line 33

def each_batch_range(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE)
  each_batch(table_name, connection: connection, scope: scope, of: of) do |batch|
    yield batch.pluck('MIN(id), MAX(id)').first
  end
end