diff options
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | activerecord/CHANGELOG.md | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/batches.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/batches_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/queueing.rb | 2 |
6 files changed, 15 insertions, 31 deletions
@@ -39,6 +39,7 @@ instance_eval File.read local_gemfile if File.exists? local_gemfile platforms :mri do group :test do gem 'ruby-prof', '~> 0.11.2' + gem 'debugger' if !ENV['TRAVIS'] && RUBY_VERSION < "2.0" end end diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index aee8f8d1f7..04b67cdf3a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -76,21 +76,6 @@ *kennyj* -* Fix `find_in_batches` when primary_key is set other than id. - You can now use this method with the primary key which is not integer-based. - - Example: - - class Post < ActiveRecord::Base - self.primary_key = :title - end - - Post.find_in_batches(start: 'My First Post') do |batch| - batch.each { |post| post.author.greeting } - end - - *Toshiyuki Kawanishi* - * You can now override the generated accessor methods for stored attributes and reuse the original behavior with `read_store_attribute` and `write_store_attribute`, which are counterparts to `read_attribute` and `write_attribute`. diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index d32048cce1..28aab6d92b 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -36,12 +36,12 @@ module ActiveRecord # 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+ - # option on that worker). You can also use non-integer-based primary keys - # if start point is set. + # option on that worker). # # It's not possible to set the order. That is automatically set to - # ascending on the primary key (e.g. "id ASC") to make the batch ordering - # work. You can't set the limit either, that's used to control + # ascending on the primary key ("id ASC") to make the batch ordering + # work. This also mean that this method only works with integer-based + # primary keys. You can't set the limit either, that's used to control # the batch sizes. # # Person.where("age > 21").find_in_batches do |group| @@ -62,8 +62,7 @@ module ActiveRecord ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size") end - start = options.delete(:start) - start ||= 0 + start = options.delete(:start) || 0 batch_size = options.delete(:batch_size) || 1000 relation = relation.reorder(batch_order).limit(batch_size) @@ -71,7 +70,7 @@ module ActiveRecord while records.any? records_size = records.size - primary_key_offset = records.last.send(primary_key) + primary_key_offset = records.last.id yield records diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 3b4ff83725..4016f5f309 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -125,14 +125,18 @@ class EachTest < ActiveRecord::TestCase end def test_find_in_batches_should_use_any_column_as_primary_key + old_primary_key = Post.primary_key + Post.primary_key = :title title_order_posts = Post.order('title asc') - start_title = title_order_posts.first.title + start_title = title_order_posts.second.title posts = [] - PostWithTitlePrimaryKey.find_in_batches(:batch_size => 1, :start => start_title) do |batch| + Post.find_in_batches(:batch_size => 1, :start => start_title) do |batch| posts.concat(batch) end - assert_equal title_order_posts.map(&:id), posts.map(&:id) + assert_equal title_order_posts[1..-1].map(&:id), posts.map(&:id) + ensure + Post.primary_key = old_primary_key end end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 9858f68c4a..c995f59a15 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -186,8 +186,3 @@ class SpecialPostWithDefaultScope < ActiveRecord::Base self.table_name = 'posts' default_scope { where(:id => [1, 5,6]) } end - -class PostWithTitlePrimaryKey < ActiveRecord::Base - self.table_name = 'posts' - self.primary_key = :title -end diff --git a/activesupport/lib/active_support/queueing.rb b/activesupport/lib/active_support/queueing.rb index 0a4ab05b78..7c352886f3 100644 --- a/activesupport/lib/active_support/queueing.rb +++ b/activesupport/lib/active_support/queueing.rb @@ -126,7 +126,7 @@ module ActiveSupport end def handle_exception(job, exception) - raise unless @logger + raise exception unless @logger @logger.error "Job Error: #{exception.message}\n#{exception.backtrace.join("\n")}" end end |