debian-mirror-gitlab/lib/gitlab/database/dynamic_model_helpers.rb

36 lines
1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module DynamicModelHelpers
def define_batchable_model(table_name)
Class.new(ActiveRecord::Base) do
include EachBatch
self.table_name = table_name
self.inheritance_column = :_type_disabled
end
end
2021-09-04 01:27:46 +05:30
def each_batch(table_name, scope: ->(table) { table.all }, of: 1000)
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))
.each_batch(of: of) { |batch| yield batch }
end
def each_batch_range(table_name, scope: ->(table) { table.all }, of: 1000)
each_batch(table_name, scope: scope, of: of) do |batch|
yield batch.pluck('MIN(id), MAX(id)').first
end
end
2020-07-28 23:09:34 +05:30
end
end
end