Fix loaded relation batching with limits and reverse order

This commit is contained in:
fatkodima 2024-07-13 13:42:52 +03:00
parent 4867559a10
commit d21f95940c
2 changed files with 24 additions and 1 deletions

@ -341,7 +341,13 @@ def batch_on_loaded_relation(relation:, start:, finish:, order:, batch_limit:)
if start || finish if start || finish
records = records.filter do |record| records = records.filter do |record|
(start.nil? || record.id >= start) && (finish.nil? || record.id <= finish) id = record.id
if order == :asc
(start.nil? || id >= start) && (finish.nil? || id <= finish)
else
(start.nil? || id <= start) && (finish.nil? || id >= finish)
end
end end
end end

@ -506,6 +506,23 @@ def test_in_batches_when_loaded_runs_no_queries_with_start_and_end_arguments
assert_equal posts.size - 2, batch_count assert_equal posts.size - 2, batch_count
end end
def test_in_batches_when_loaded_runs_no_queries_with_start_and_end_arguments_and_reverse_order
posts = Post.all.order(id: :asc)
posts.load
batch_count = 0
start_id = posts.map(&:id)[-2]
finish_id = posts.map(&:id)[1]
assert_queries_count(0) do
posts.in_batches(of: 1, start: start_id, finish: finish_id, order: :desc) do |relation|
batch_count += 1
assert_kind_of ActiveRecord::Relation, relation
end
end
assert_equal posts.size - 2, batch_count
end
def test_in_batches_when_loaded_can_return_an_enum def test_in_batches_when_loaded_can_return_an_enum
posts = Post.all posts = Post.all
posts.load posts.load