Class: OnlineMigrations::BatchIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/batch_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ BatchIterator

Returns a new instance of BatchIterator.



8
9
10
11
12
13
14
# File 'lib/online_migrations/batch_iterator.rb', line 8

def initialize(relation)
  if !relation.is_a?(ActiveRecord::Relation)
    raise ArgumentError, "relation is not an ActiveRecord::Relation"
  end

  @relation = relation
end

Instance Attribute Details

#relationObject (readonly)

Returns the value of attribute relation.



6
7
8
# File 'lib/online_migrations/batch_iterator.rb', line 6

def relation
  @relation
end

Instance Method Details

#each_batch(of: 1000, column: relation.primary_key, start: nil, finish: nil, order: :asc) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/online_migrations/batch_iterator.rb', line 16

def each_batch(of: 1000, column: relation.primary_key, start: nil, finish: nil, order: :asc)
  if ![:asc, :desc].include?(order)
    raise ArgumentError, ":order must be :asc or :desc, got #{order.inspect}"
  end

  relation = apply_limits(self.relation, column, start, finish, order)
  unscopes = Utils.ar_version < 7.1 ? [:includes] : [:includes, :preload, :eager_load]
  base_relation = relation.unscope(*unscopes).reselect(column).reorder(column => order)

  start_id = start || begin
    start_row = base_relation.uncached { base_relation.first }
    start_row[column] if start_row
  end

  arel_table = relation.arel_table

  while start_id
    if order == :asc
      start_cond = arel_table[column].gteq(start_id)
    else
      start_cond = arel_table[column].lteq(start_id)
    end

    last_row, stop_row = base_relation.uncached do
      base_relation
        .where(start_cond)
        .offset(of - 1)
        .first(2)
    end

    if last_row.nil?
      # We are at the end of the table.
      last_row, stop_row = base_relation.uncached do
        base_relation
          .where(start_cond)
          .last(2)
      end
    end

    batch_relation = relation.where(start_cond)

    if stop_row
      stop_id = stop_row[column]

      if order == :asc
        stop_cond = arel_table[column].lt(stop_id)
      else
        stop_cond = arel_table[column].gt(stop_id)
      end

      batch_relation = batch_relation.where(stop_cond)
    end

    # Any ORDER BYs are useless for this relation and can lead to less
    # efficient UPDATE queries, hence we get rid of it.
    batch_relation = batch_relation.except(:order)

    last_id = (last_row && last_row[column]) || finish

    # Retaining the results in the query cache would undermine the point of batching.
    batch_relation.uncached { yield batch_relation, start_id, last_id }

    break if last_id == finish

    start_id = stop_id
    stop_id = nil
  end
end