Skip to content

Commit

Permalink
Add database role support for enumerator in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
etravassos committed Feb 1, 2024
1 parent 922ee56 commit ad7d29a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/job-iteration/active_record_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def update_from_record(record)
end
end

def next_batch(batch_size)
def next_batch(batch_size, database_role: nil)
return if @reached_end

relation = @base_relation.limit(batch_size)
Expand All @@ -74,7 +74,13 @@ def next_batch(batch_size)
end

records = relation.uncached do
relation.to_a
if database_role.present?
ActiveRecord::Base.connected_to(role: database_role) do
relation.to_a
end
else
relation.to_a
end
end

update_from_record(records.last) unless records.empty?
Expand Down
5 changes: 3 additions & 2 deletions lib/job-iteration/active_record_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module JobIteration
class ActiveRecordEnumerator
SQL_DATETIME_WITH_NSEC = "%Y-%m-%d %H:%M:%S.%N"

def initialize(relation, columns: nil, batch_size: 100, cursor: nil)
def initialize(relation, columns: nil, batch_size: 100, cursor: nil, database_role: nil)
@relation = relation
@batch_size = batch_size
@database_role = database_role
@columns = if columns
Array(columns)
else
Expand All @@ -31,7 +32,7 @@ def records
def batches
cursor = finder_cursor
Enumerator.new(method(:size)) do |yielder|
while (records = cursor.next_batch(@batch_size))
while (records = cursor.next_batch(@batch_size, database_role: @database_role))
yielder.yield(records, cursor_value(records.last)) if records.any?
end
end
Expand Down

0 comments on commit ad7d29a

Please sign in to comment.