Module: SuperSpreader::BatchHelper

Defined in:
lib/super_spreader/batch_helper.rb

Overview

Methods in this module are suitable for use in Rails migrations. It is expected that their interface will remain stable. If breaking changes are introduced, a new module will be introduced so existing migrations will not be affected.

Instance Method Summary collapse

Instance Method Details

#batch_execute(table_name:, step_size:) {|minimum_id, maximum_id| ... } ⇒ Object

Execute SQL in small batches for an entire table.

It is assumed that the table has a primary key named id.

Recommendation for migrations: Use this in combination with disable_ddl_transaction!. See also: github.com/ankane/strong_migrations#backfilling-data

Parameters:

  • table_name (String)

    the name of the table

  • step_size (Integer)

    how many records to process in each batch

Yields:

  • (minimum_id, maximum_id)

    block that returns SQL to migrate records between minimum_id and maximum_id



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/super_spreader/batch_helper.rb', line 21

def batch_execute(table_name:, step_size:)
  result = execute(<<~SQL).to_a.flatten
    SELECT MIN(id) AS min_id, MAX(id) AS max_id FROM #{quote_table_name(table_name)}
  SQL
  min_id = result[0]["min_id"]
  max_id = result[0]["max_id"]
  return unless min_id && max_id

  lower_id = min_id
  loop do
    sql = yield(lower_id, lower_id + step_size)

    execute(sql)

    lower_id += step_size
    break if lower_id > max_id
  end
end