From 65982727706b85379ac4ddfe791eea00ebab5144 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Tue, 17 Feb 2015 03:46:49 +0530 Subject: Deprecated passing of `start` value to `find_in_batches` and `find_each` in favour of `begin_at` value. --- activerecord/lib/active_record/relation/batches.rb | 48 ++++++++++++++-------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index a543341149..52b94db9b1 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -27,15 +27,15 @@ module ActiveRecord # # ==== Options # * :batch_size - Specifies the size of the batch. Default to 1000. - # * :start - Specifies the primary key value to start from, inclusive of the value. + # * :begin_at - Specifies the primary key value to start from, inclusive of the value. # * :end_at - Specifies the primary key value to end at, inclusive of the value. # This is especially useful if you want multiple workers dealing with # the same processing queue. You can make worker 1 handle all the records # between id 0 and 10,000 and worker 2 handle from 10,000 and beyond - # (by setting the +:start+ and +:end_at+ option on each worker). + # (by setting the +:begin_at+ and +:end_at+ option on each worker). # # # Let's process for a batch of 2000 records, skipping the first 2000 rows - # Person.find_each(start: 2000, batch_size: 2000) do |person| + # Person.find_each(begin_at: 2000, batch_size: 2000) do |person| # person.party_all_night! # end # @@ -46,15 +46,22 @@ module ActiveRecord # # NOTE: You can't set the limit either, that's used to control # the batch sizes. - def find_each(start: nil, end_at: nil, batch_size: 1000) + def find_each(begin_at: nil, end_at: nil, batch_size: 1000, start: nil) + if start + begin_at = start + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing `start` value to find_in_batches is deprecated, and will be removed in Rails 5.1. + Please pass `begin_at` instead. + MSG + end if block_given? - find_in_batches(start: start, end_at: end_at, batch_size: batch_size) do |records| + find_in_batches(begin_at: begin_at, end_at: end_at, batch_size: batch_size) do |records| records.each { |record| yield record } end else - enum_for(:find_each, start: start, end_at: end_at, batch_size: batch_size) do + enum_for(:find_each, begin_at: begin_at, end_at: end_at, batch_size: batch_size) do relation = self - apply_limits(relation, start, end_at).size + apply_limits(relation, begin_at, end_at).size end end end @@ -79,15 +86,15 @@ module ActiveRecord # # ==== Options # * :batch_size - Specifies the size of the batch. Default to 1000. - # * :start - Specifies the primary key value to start from, inclusive of the value. + # * :begin_at - Specifies the primary key value to start from, inclusive of the value. # * :end_at - Specifies the primary key value to end at, inclusive of the value. # This is especially useful if you want multiple workers dealing with # the same processing queue. You can make worker 1 handle all the records # between id 0 and 10,000 and worker 2 handle from 10,000 and beyond - # (by setting the +:start+ and +:end_at+ option on each worker). + # (by setting the +:begin_at+ and +:end_at+ option on each worker). # # # Let's process the next 2000 records - # Person.find_in_batches(start: 2000, batch_size: 2000) do |group| + # Person.find_in_batches(begin_at: 2000, batch_size: 2000) do |group| # group.each { |person| person.party_all_night! } # end # @@ -98,12 +105,19 @@ module ActiveRecord # # NOTE: You can't set the limit either, that's used to control # the batch sizes. - def find_in_batches(start: nil, end_at: nil, batch_size: 1000) - relation = self + def find_in_batches(begin_at: nil, end_at: nil, batch_size: 1000, start: nil) + if start + begin_at = start + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing `start` value to find_in_batches is deprecated, and will be removed in Rails 5.1. + Please pass `begin_at` instead. + MSG + end + relation = self unless block_given? - return to_enum(:find_in_batches, start: start, end_at: end_at, batch_size: batch_size) do - total = apply_limits(relation, start, end_at).size + return to_enum(:find_in_batches, begin_at: begin_at, end_at: end_at, batch_size: batch_size) do + total = apply_limits(relation, begin_at, end_at).size (total - 1).div(batch_size) + 1 end end @@ -113,7 +127,7 @@ module ActiveRecord end relation = relation.reorder(batch_order).limit(batch_size) - relation = apply_limits(relation, start, end_at) + relation = apply_limits(relation, begin_at, end_at) records = relation.to_a while records.any? @@ -131,8 +145,8 @@ module ActiveRecord private - def apply_limits(relation, start, end_at) - relation = relation.where(table[primary_key].gteq(start)) if start + def apply_limits(relation, begin_at, end_at) + relation = relation.where(table[primary_key].gteq(begin_at)) if begin_at relation = relation.where(table[primary_key].lteq(end_at)) if end_at relation end -- cgit v1.2.3