You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
999 B

  1. # frozen_string_literal: true
  2. module ActiveRecord
  3. module Batches
  4. def pluck_each(*column_names)
  5. relation = self
  6. options = column_names.extract_options!
  7. flatten = column_names.size == 1
  8. batch_limit = options[:batch_limit] || 1_000
  9. order = options[:order] || :asc
  10. column_names.unshift(primary_key)
  11. relation = relation.reorder(batch_order(order)).limit(batch_limit)
  12. relation.skip_query_cache!
  13. batch_relation = relation
  14. loop do
  15. batch = batch_relation.pluck(*column_names)
  16. break if batch.empty?
  17. primary_key_offset = batch.last[0]
  18. batch.each do |record|
  19. if flatten
  20. yield record[1]
  21. else
  22. yield record[1..-1]
  23. end
  24. end
  25. break if batch.size < batch_limit
  26. batch_relation = relation.where(
  27. predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
  28. )
  29. end
  30. end
  31. end
  32. end