Skip to content

Commit

Permalink
fixing issue where if the index contained a nil, queries would fail
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondew committed May 11, 2012
1 parent 4c9c3c3 commit 93251f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/dynamoid/criteria/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,8 @@ def records
#
# @since 0.2.0
def records_with_index
ids = if index.range_key?
Dynamoid::Adapter.query(index.table_name, index_query).collect{|r| r[:ids]}.inject(Set.new) {|set, result| set + result}
else
results = Dynamoid::Adapter.read(index.table_name, index_query[:hash_value], consistent_opts)
if results
results[:ids]
else
[]
end
end
ids = ids_from_index

if ids.nil? || ids.empty?
[]
else
Expand All @@ -121,6 +113,20 @@ def records_with_index
end
end

# Returns the Set of IDs from the index table.
#
# @return [Set] a Set containing the IDs from the index.
def ids_from_index
if index.range_key?
Dynamoid::Adapter.query(index.table_name, index_query).inject(Set.new) do |all, record|
all + Set.new(record[:ids])
end
else
results = Dynamoid::Adapter.read(index.table_name, index_query[:hash_value], consistent_opts)
results ? results[:ids] : []
end
end

def records_with_range
Dynamoid::Adapter.query(source.table_name, range_query).collect {|hash| source.new(hash).tap { |r| r.new_record = false } }
end
Expand Down
8 changes: 8 additions & 0 deletions spec/dynamoid/criteria/chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
@chain.send(:records_without_index).should == [@user]
end

it "doesn't crash if it finds a nil id in the index" do
@chain.query = {:name => 'Josh', "created_at.gt" => @time - 1.day}
Dynamoid::Adapter.expects(:query).
with("dynamoid_tests_index_user_created_ats_and_names", kind_of(Hash)).
returns([{ids: nil}, {ids: Set.new([42])}])
@chain.send(:ids_from_index).should == Set.new([42])
end

it 'defines each' do
@chain.query = {:name => 'Josh'}
@chain.each {|u| u.update_attribute(:name, 'Justin')}
Expand Down

0 comments on commit 93251f2

Please sign in to comment.