Permalink
Browse files

Uncached queries when using find_in_batches.

  • Loading branch information...
1 parent 5683946 commit 782e1935f44f87e79bb52d372ae2cf25079ded12 @kennyj committed Jun 18, 2012
Showing with 37 additions and 16 deletions.
  1. +21 −16 activerecord/lib/active_record/relation/batches.rb
  2. +16 −0 activerecord/test/cases/batches_test.rb
@@ -55,7 +55,7 @@ def find_each(options = {})
# group.each { |person| person.party_all_night! }
# end
def find_in_batches(options = {})
- options.assert_valid_keys(:start, :batch_size)
+ options.assert_valid_keys(:start, :batch_size, :uncached)
relation = self
@@ -65,24 +65,29 @@ def find_in_batches(options = {})
start = options.delete(:start).to_i
batch_size = options.delete(:batch_size) || 1000
+ uncached = options.delete(:uncached) || true
- relation = relation.reorder(batch_order).limit(batch_size)
- records = relation.where(table[primary_key].gteq(start)).all
-
- while records.any?
- records_size = records.size
- primary_key_offset = records.last.id
-
- yield records
-
- break if records_size < batch_size
-
- if primary_key_offset
- records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
- else
- raise "Primary key not included in the custom select clause"
+ block = Proc.new do
+ relation = relation.reorder(batch_order).limit(batch_size)
+ records = relation.where(table[primary_key].gteq(start)).all
+
+ while records.any?
+ records_size = records.size
+ primary_key_offset = records.last.id
+
+ yield records
+
+ break if records_size < batch_size
+
+ if primary_key_offset
+ records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
+ else
+ raise "Primary key not included in the custom select clause"
+ end
end
end
+
+ uncached ? connection.uncached { block.call } : block.call
end
private
@@ -124,4 +124,20 @@ def test_find_in_batches_should_not_ignore_the_default_scope_if_it_is_other_then
assert_equal special_posts_ids, posts.map(&:id)
end
+ def test_find_in_batches_should_uncached_queries_in_default
+ assert_no_difference "ActiveRecord::Base.connection.query_cache.size" do
+ Post.connection.cache do
+ Post.find_in_batches(:batch_size => Post.count + 1) { |batch| assert_kind_of Array, batch }
+ end
+ end
+ end
+
+ def test_find_in_batches_should_cached_queries_when_passing_false
+ assert_difference "ActiveRecord::Base.connection.query_cache.size" do
+ Post.connection.cache do
+ Post.find_in_batches(:batch_size => Post.count + 1, :uncached => false) { |batch| assert_kind_of Array, batch }
+ end
+ end
+ end
+
end

0 comments on commit 782e193

Please sign in to comment.