Class: JobIteration::ActiveRecordBatchEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/job-iteration/active_record_batch_enumerator.rb

Overview

Builds Batch Enumerator based on ActiveRecord Relation.

See Also:

Constant Summary collapse

SQL_DATETIME_WITH_NSEC =
"%Y-%m-%d %H:%M:%S.%N"

Instance Method Summary collapse

Constructor Details

#initialize(relation, columns: nil, batch_size: 100, timezone: nil, cursor: nil) ⇒ ActiveRecordBatchEnumerator

Returns a new instance of ActiveRecordBatchEnumerator.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/job-iteration/active_record_batch_enumerator.rb', line 11

def initialize(relation, columns: nil, batch_size: 100, timezone: nil, cursor: nil)
  @batch_size = batch_size
  @timezone = timezone
  @primary_key = "#{relation.table_name}.#{relation.primary_key}"
  @columns = Array(columns&.map(&:to_s) || @primary_key)
  @primary_key_index = @columns.index(@primary_key) || @columns.index(relation.primary_key)
  @pluck_columns = if @primary_key_index
    @columns
  else
    @columns.dup << @primary_key
  end
  @cursor = Array.wrap(cursor)
  @initial_cursor = @cursor
  raise ArgumentError, "Must specify at least one column" if @columns.empty?
  if relation.joins_values.present? && !@columns.all? { |column| column.to_s.include?(".") }
    raise ArgumentError, "You need to specify fully-qualified columns if you join a table"
  end

  if relation.arel.orders.present? || relation.arel.taken.present?
    raise JobIteration::ActiveRecordCursor::ConditionNotSupportedError
  end

  @base_relation = relation.reorder(@columns.join(","))
end

Instance Method Details

#eachObject



36
37
38
39
40
41
42
# File 'lib/job-iteration/active_record_batch_enumerator.rb', line 36

def each
  return to_enum { size } unless block_given?

  while (relation = next_batch)
    yield relation, cursor_value
  end
end

#sizeObject



44
45
46
# File 'lib/job-iteration/active_record_batch_enumerator.rb', line 44

def size
  (@base_relation.count(:all) + @batch_size - 1) / @batch_size # ceiling division
end