Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/job-iteration/active_record_batch_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(relation, columns: nil, batch_size: 100, cursor: nil)
@columns.dup << @primary_key
end
@cursor = Array.wrap(cursor)
@initial_cursor = @cursor
raise ArgumentError, "Must specify at least one column" if @columns.empty?
if relation.joins_values.present? && !@columns.all? { |column| column.to_s.include?(".") }
raise ArgumentError, "You need to specify fully-qualified columns if you join a table"
Expand Down Expand Up @@ -56,7 +57,10 @@ def next_batch
end

cursor = cursor_values.last
return unless cursor.present?
unless cursor.present?
@cursor = @initial_cursor
return
end
# The primary key was plucked, but original cursor did not include it, so we should remove it
cursor.pop unless @primary_key_index
@cursor = Array.wrap(cursor)
Expand Down
6 changes: 6 additions & 0 deletions test/unit/active_record_batch_enumerator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class ActiveRecordBatchEnumeratorTest < IterationUnitTest
end
end

test "#each yields relations repeatedly" do
enum = build_enumerator(cursor: 2)
assert_equal 4, enum.to_a.size
assert_equal 4, enum.to_a.size
end

test "#each yields unloaded relations" do
enum = build_enumerator
relation, _ = enum.first
Expand Down