Skip to content

Commit

Permalink
Do not load association during initialization of cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
simi committed Jan 16, 2020
1 parent b2c9e4e commit b370459
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ desc "Setup testing database and table"
task :setup do
sh %q(createdb postgresql_cursor_test)
sh %Q<echo "create table products ( id serial primary key, data varchar);" | psql postgresql_cursor_test>
sh %Q<echo "create table prices ( id serial primary key, data varchar, product_id integer);" | psql postgresql_cursor_test>
end
9 changes: 6 additions & 3 deletions lib/postgresql_cursor/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ def initialize(sql, options={})
@batched = false
end

# Specify the type to instantiate, or reset to return a Hash
# Specify the type to instantiate, or reset to return a Hash.
#
# Explicitly check for type class to prevent calling equality
# operator on active record relation, which will load it.
def iterate_type(type=nil)
if type.nil? || type == Hash
if type.nil? || (type.class == Class && type == Hash)
@iterate = :each_row
elsif type == Array
elsif type.class == Class && type == Array
@iterate = :each_array
else
@iterate = :each_instance
Expand Down
6 changes: 6 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')

class Product < ActiveRecord::Base
has_many :prices

# create table records (id serial primary key);
def self.generate(max=1_000)
max.times do |i|
Expand All @@ -18,5 +20,9 @@ def self.generate(max=1_000)
end
end

class Price < ActiveRecord::Base
belongs_to :product
end

Product.destroy_all
Product.generate(1000)
5 changes: 5 additions & 0 deletions test/test_postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ def test_bad_sql
assert_match(/bad_table/, e.message)
end
end

def test_relation_association_is_not_loaded
cursor = Product.first.prices.each_instance
refute cursor.instance_variable_get(:@type).loaded?
end
end

0 comments on commit b370459

Please sign in to comment.