2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module DynamicModelHelpers
|
2021-09-30 23:02:18 +05:30
|
|
|
BATCH_SIZE = 1_000
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def define_batchable_model(table_name, connection:)
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
2020-07-28 23:09:34 +05:30
|
|
|
include EachBatch
|
|
|
|
|
|
|
|
self.table_name = table_name
|
|
|
|
self.inheritance_column = :_type_disabled
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
klass.connection = connection
|
|
|
|
klass
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def each_batch(table_name, connection:, scope: ->(table) { table.all }, of: BATCH_SIZE)
|
2021-09-04 01:27:46 +05:30
|
|
|
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
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
scope.call(define_batchable_model(table_name, connection: connection))
|
2021-09-04 01:27:46 +05:30
|
|
|
.each_batch(of: of) { |batch| yield batch }
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
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|
|
2022-08-27 11:52:29 +05:30
|
|
|
yield batch.pick('MIN(id), MAX(id)')
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|