Skip to content

Commit

Permalink
Bring in patch from Rails to expose public attr readers on BatchEnume…
Browse files Browse the repository at this point in the history
…rator
  • Loading branch information
adrianna-chang-shopify committed May 31, 2021
1 parent 46b4f5c commit 63ab1b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 3 additions & 6 deletions app/jobs/concerns/maintenance_tasks/task_job_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ def build_enumerator(_run, cursor:)
when ActiveRecord::Relation
enumerator_builder.active_record_on_records(collection, cursor: cursor)
when ActiveRecord::Batches::BatchEnumerator
if collection.instance_variable_get(:@start) ||
collection.instance_variable_get(:@finish)
if collection.start || collection.finish
raise ArgumentError, "#{@task.class.name}#collection cannot support "\
"a batch enumerator with the +start+ or +finish+ options."
end
relation = collection.instance_variable_get(:@relation)
batch_size = collection.instance_variable_get(:@of)
enumerator_builder.active_record_on_batch_relations(
relation,
collection.relation,
cursor: cursor,
batch_size: batch_size,
batch_size: collection.batch_size,
)
when Array
enumerator_builder.build_array_enumerator(collection, cursor: cursor)
Expand Down
2 changes: 2 additions & 0 deletions lib/maintenance_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require "job-iteration"
require "maintenance_tasks/engine"

require "patches/active_record_batch_enumerator"

# The engine's namespace module. It provides isolation between the host
# application's code and the engine-specific code. Top-level engine constants
# and variables are defined under this module.
Expand Down
23 changes: 23 additions & 0 deletions lib/patches/active_record_batch_enumerator.rb
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)

0 comments on commit 63ab1b1

Please sign in to comment.