-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #409 from Shopify/batch-collections-w-batch-enumer…
…ator Support `ActiveRecord::Batches::BatchEnumerator` collections
- Loading branch information
Showing
10 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,7 @@ def process(_item) | |
# | ||
# @return [Integer, nil] | ||
def count | ||
:no_count | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# TODO: Remove this patch once all supported Rails versions include the changes | ||
# upstream - https://github.com/rails/rails/pull/42312/commits/a031a43d969c87542c4ee8d0d338d55fcbb53376 | ||
module ActiveRecordBatchEnumerator | ||
# The primary key value from which the BatchEnumerator starts, | ||
# inclusive of the value. | ||
attr_reader :start | ||
|
||
# The primary key value at which the BatchEnumerator ends, | ||
# inclusive of the value. | ||
attr_reader :finish | ||
|
||
# The relation from which the BatchEnumerator yields batches. | ||
attr_reader :relation | ||
|
||
# The size of the batches yielded by the BatchEnumerator. | ||
def batch_size | ||
@of | ||
end | ||
end | ||
|
||
ActiveRecord::Batches::BatchEnumerator.include(ActiveRecordBatchEnumerator) |
12 changes: 12 additions & 0 deletions
12
test/dummy/app/tasks/maintenance/update_posts_in_batches_task.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
module Maintenance | ||
class UpdatePostsInBatchesTask < MaintenanceTasks::Task | ||
def collection | ||
Post.in_batches(of: 5) | ||
end | ||
|
||
def process(batch_of_posts) | ||
batch_of_posts.update_all(content: "New content added on #{Time.now.utc}") | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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