Skip to content

Loading…

fixing issues we're seeing in production #49

Merged
merged 2 commits into from

2 participants

@jasondew
  • when the index has a nil value in it which makes queries fail. not sure the underlying cause but this at least cures the symptom
  • regression: new records when asked for their associations throw an exception
@Veraticus Veraticus merged commit 2229b6b into Veraticus:master
@Veraticus
Owner

Thanks Jason!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 29 additions and 11 deletions.
  1. +1 −1 lib/dynamoid/adapter/local.rb
  2. +16 −10 lib/dynamoid/criteria/chain.rb
  3. +8 −0 spec/dynamoid/criteria/chain_spec.rb
  4. +4 −0 spec/dynamoid_spec.rb
View
2 lib/dynamoid/adapter/local.rb
@@ -36,7 +36,7 @@ def batch_get_item(options)
Hash.new { |h, k| h[k] = Array.new }.tap do |hash|
options.each do |table_name, keys|
table = data[table_name]
- if table[:range_key]
+ if table and table[:range_key]
Array(keys).each do |hash_key, range_key|
hash[table_name] << get_item(table_name, hash_key, :range_key => range_key)
end
View
26 lib/dynamoid/criteria/chain.rb
@@ -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
@@ -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
View
8 spec/dynamoid/criteria/chain_spec.rb
@@ -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')}
View
4 spec/dynamoid_spec.rb
@@ -2,4 +2,8 @@
describe "Dynamoid" do
+ it "doesn't puke when asked for the assocations of a new record" do
+ User.new.books.should == []
+ end
+
end
Something went wrong with that request. Please try again.