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
adrianna-chang-shopify authored and lawrencewong committed Apr 29, 2023
1 parent 0cbd313 commit aecdbf6
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
@@ -37,19 +37,16 @@ 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, <<~MSG.squish
#{@task.class.name}#collection cannot support
a batch enumerator with the "start" or "finish" options.
MSG
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,
)
@run.update!(tick_total: enumerator.size)
enumerator
2 changes: 2 additions & 0 deletions lib/maintenance_tasks.rb
Original file line number Diff line number Diff line change
@@ -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.
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 aecdbf6

Please sign in to comment.