ActiveRecordBatchEnumerator#each should rewind at the end#95
Merged
Conversation
Calling each twice in a row should yield all the batches again, like:
results = []
array = [1, 2]
array.each { |i| results << i }
array.each { |i| results << i }
results # => [1, 2, 1, 2]
adrianna-chang-shopify
approved these changes
May 27, 2021
adrianna-chang-shopify
left a comment
Contributor
There was a problem hiding this comment.
Yeah, I had the feeling having an enum with state tracked inside @cursor was going to throw something off. 😆 Since we've bothered to make this a proper Ruby Enumerator it definitely makes sense to me to make this correct and rewind properly based on the cursor we first built the enumerator with. Nice catch! 🚀
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While looking at the code for #94, I found out that our
eachis a bit surprising since it remembers its state (because it keeps the cursor). Callingeachagain on a enumerator doesn't iterate again, it doesn't yield.Calling each twice in a row should yield all the batches again, like an Array or any Enumerator does:
I tested that by creating an enumerator with a cursor, and checking that we only get the remaining batches, twice. At first I reset the cursor entirely, but that would cause 4 batches first, then the full 5 batches on the second call to
each, which was also an unexpected behavior. I don't really expect users to use this a lot but it's more correct at very little cost so worth it!