~cytrogen/masto-fe

ref: 19ebf7797ebb64002c6b5ea8fcf2fcb8bba760bc masto-fe/lib/active_record/batches.rb -rw-r--r-- 997 bytes
19ebf779 — Claire Merge commit 'e6b903b21e0682c1156ffec583ac2836fda7d657' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module ActiveRecord
  module Batches
    def pluck_each(*column_names)
      relation = self

      options = column_names.extract_options!

      flatten     = column_names.size == 1
      batch_limit = options[:batch_limit] || 1_000
      order       = options[:order] || :asc

      column_names.unshift(primary_key)

      relation = relation.reorder(batch_order(order)).limit(batch_limit)
      relation.skip_query_cache!

      batch_relation = relation

      loop do
        batch = batch_relation.pluck(*column_names)

        break if batch.empty?

        primary_key_offset = batch.last[0]

        batch.each do |record|
          if flatten
            yield record[1]
          else
            yield record[1..]
          end
        end

        break if batch.size < batch_limit

        batch_relation = relation.where(
          predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
        )
      end
    end
  end
end