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
-
#batch_execute(table_name:, step_size:) {|minimum_id, maximum_id| ... } ⇒ Object
Execute SQL in small batches for an entire table.
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
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(" SELECT MIN(id) AS min_id, MAX(id) AS max_id FROM \#{quote_table_name(table_name)}\n SQL\n min_id = result[0][\"min_id\"]\n max_id = result[0][\"max_id\"]\n return unless min_id && max_id\n\n lower_id = min_id\n loop do\n sql = yield(lower_id, lower_id + step_size)\n\n execute(sql)\n\n lower_id += step_size\n break if lower_id > max_id\n end\nend\n").to_a.flatten |