diff --git a/lib/couchrest/model/design_doc.rb b/lib/couchrest/model/design_doc.rb index d4e46ea..36c3940 100644 --- a/lib/couchrest/model/design_doc.rb +++ b/lib/couchrest/model/design_doc.rb @@ -10,7 +10,7 @@ module CouchRest @design_doc ||= if auto_update_design_doc ::CouchRest::Design.new(default_design_doc) else - stored_design_doc + stored_design_doc || ::CouchRest::Design.new(default_design_doc) end end @@ -79,16 +79,25 @@ module CouchRest # If auto updates enabled, check checksum cache return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum - # Load up the stored doc (if present), update, and save - saved = stored_design_doc(db) - if saved - if force || saved['couchrest-hash'] != checksum - saved.merge!(design_doc) - db.save_doc(saved) + retries = 1 + begin + # Load up the stored doc (if present), update, and save + saved = stored_design_doc(db) + if saved + if force || saved['couchrest-hash'] != checksum + saved.merge!(design_doc) + db.save_doc(saved) + @design_doc = saved # update memo to point to the document we actually saved + end + else + design_doc.delete('_rev') # This is a new document and so doesn't have a revision yet + db.save_doc(design_doc) end - else - db.save_doc(design_doc) - design_doc.delete('_rev') # Prevent conflicts, never store rev as DB specific + rescue RestClient::Conflict + # if we get a conflict retry the operation... + raise if retries < 1 + retries -= 1 + retry end # Ensure checksum cached for next attempt if using auto updates diff --git a/lib/couchrest/model/views.rb b/lib/couchrest/model/views.rb index 89dffbd..f8331e5 100644 --- a/lib/couchrest/model/views.rb +++ b/lib/couchrest/model/views.rb @@ -72,9 +72,12 @@ module CouchRest # spec/couchrest/more/extended_doc_spec.rb. def view_by(*keys) + return unless auto_update_design_doc + opts = keys.pop if keys.last.is_a?(Hash) opts ||= {} ducktype = opts.delete(:ducktype) + unless ducktype || opts[:map] opts[:guards] ||= [] opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"