diff --git a/couchrest.gemspec b/couchrest.gemspec index a957edb..b290b73 100644 --- a/couchrest.gemspec +++ b/couchrest.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 2 - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + if current_version >= 3 then s.add_runtime_dependency(%q, [">= 1.1.2"]) s.add_runtime_dependency(%q, [">= 0.5"]) s.add_runtime_dependency(%q, [">= 1.15"]) diff --git a/lib/couchrest.rb b/lib/couchrest.rb index d20771d..df5a447 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -40,6 +40,7 @@ module CouchRest autoload :Pager, 'couchrest/helper/pager' autoload :FileManager, 'couchrest/helper/file_manager' autoload :Streamer, 'couchrest/helper/streamer' + autoload :Upgrade, 'couchrest/helper/upgrade' autoload :ExtendedDocument, 'couchrest/more/extended_document' autoload :CastedModel, 'couchrest/more/casted_model' diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index f7bbfcc..82fba7d 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -77,13 +77,15 @@ module CouchRest end # GET a document from CouchDB, by id. Returns a Ruby Hash. - def get(id) + def get(id, params = {}) slug = escape_docid(id) - hash = CouchRest.get("#{@uri}/#{slug}") - doc = if /^_design/ =~ hash["_id"] - Design.new(hash) + url = CouchRest.paramify_url("#{@uri}/#{slug}", params) + result = CouchRest.get(url) + return result unless result.is_a?(Hash) + doc = if /^_design/ =~ result["_id"] + Design.new(result) else - Document.new(hash) + Document.new(result) end doc.database = self doc diff --git a/lib/couchrest/helper/upgrade.rb b/lib/couchrest/helper/upgrade.rb new file mode 100644 index 0000000..3f8126f --- /dev/null +++ b/lib/couchrest/helper/upgrade.rb @@ -0,0 +1,51 @@ +module CouchRest + class Upgrade + attr_accessor :olddb, :newdb, :dbname + def initialize dbname, old_couch, new_couch + @dbname = dbname + @olddb = old_couch.database dbname + @newdb = new_couch.database!(dbname) + @bulk_docs = [] + end + def clone! + puts "#{dbname} - #{olddb.info['doc_count']} docs" + streamer = CouchRest::Streamer.new(olddb) + streamer.view("_all_docs_by_seq") do |row| + load_row_docs(row) if row + maybe_flush_bulks + end + flush_bulks! + end + + private + + def maybe_flush_bulks + flush_bulks! if (@bulk_docs.length > 99) + end + + def flush_bulks! + url = CouchRest.paramify_url "#{@newdb.uri}/_bulk_docs", {:all_or_nothing => true} + puts "posting #{@bulk_docs.length} bulk docs to #{url}" + begin + CouchRest.post url, {:docs => @bulk_docs} + @bulk_docs = [] + rescue Exception => e + puts e.response + raise e + end + end + + def load_row_docs(row) + results = @olddb.get(row["id"], {:open_revs => "all", :attachments => true}) + results.select{|r|r["ok"]}.each do |r| + doc = r["ok"] + if /^_/.match(doc["_id"]) && !/^_design/.match(doc["_id"]) + puts "invalid docid #{doc["_id"]} -- trimming" + doc["_id"] = doc["_id"].sub('_','') + end + doc.delete('_rev') + @bulk_docs << doc + end + end + end +end diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index 826b286..acb6b83 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -205,8 +205,8 @@ module CouchRest _run_destroy_callbacks do result = database.delete_doc(self, bulk) if result['ok'] - self['_rev'] = nil - self['_id'] = nil + self.delete('_rev') + self.delete('_id') end result['ok'] end diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index dd8c3df..e7f03cc 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -149,8 +149,8 @@ describe CouchRest::Database do {"mild" => "yet local"}, {"another" => ["set","of","keys"]} ]) - rs['new_revs'].each do |r| - @db.get(r['id']) + rs.each do |r| + @db.get(r['id']).rev.should == r["rev"] end end @@ -170,26 +170,10 @@ describe CouchRest::Database do {"_id" => "twoB", "mild" => "yet local"}, {"another" => ["set","of","keys"]} ]) - rs['new_revs'].each do |r| - @db.get(r['id']) + rs.each do |r| + @db.get(r['id']).rev.should == r["rev"] end end - - it "in the case of an id conflict should not insert anything" do - @r = @db.save_doc({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"}) - - lambda do - rs = @db.bulk_save([ - {"_id" => "oneB", "wild" => "and random"}, - {"_id" => "twoB", "mild" => "yet local"}, - {"another" => ["set","of","keys"]} - ]) - end.should raise_error(RestClient::RequestFailed) - - lambda do - @db.get('twoB') - end.should raise_error(RestClient::ResourceNotFound) - end it "should empty the bulk save cache if no documents are given" do @db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true) @@ -593,7 +577,8 @@ describe CouchRest::Database do @db.save_doc({'_id' => @docid, 'will-exist' => 'here'}) end it "should fail without a rev" do - lambda{@db.move_doc @doc, @docid}.should raise_error(RestClient::RequestFailed) + @doc.delete("_rev") + lambda{@db.move_doc @doc, @docid}.should raise_error(ArgumentError) lambda{@db.get(@r['id'])}.should_not raise_error end it "should succeed with a rev" do diff --git a/spec/couchrest/core/document_spec.rb b/spec/couchrest/core/document_spec.rb index dfb2ab9..da76714 100644 --- a/spec/couchrest/core/document_spec.rb +++ b/spec/couchrest/core/document_spec.rb @@ -224,7 +224,8 @@ describe CouchRest::Document do @db.save_doc({'_id' => @docid, 'will-exist' => 'here'}) end it "should fail without a rev" do - lambda{@doc.move @docid}.should raise_error(RestClient::RequestFailed) + @doc.delete("_rev") + lambda{@doc.move @docid}.should raise_error(ArgumentError) lambda{@db.get(@resp['id'])}.should_not raise_error end it "should succeed with a rev" do diff --git a/spec/couchrest/more/casted_model_spec.rb b/spec/couchrest/more/casted_model_spec.rb index 880e0ee..a2b9216 100644 --- a/spec/couchrest/more/casted_model_spec.rb +++ b/spec/couchrest/more/casted_model_spec.rb @@ -70,6 +70,7 @@ describe CouchRest::CastedModel do describe "saved document with casted models" do before(:each) do + reset_test_db! @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'}) @obj.save.should be_true @obj = DummyModel.get(@obj.id) @@ -94,4 +95,4 @@ describe CouchRest::CastedModel do end -end \ No newline at end of file +end diff --git a/spec/couchrest/more/extended_doc_attachment_spec.rb b/spec/couchrest/more/extended_doc_attachment_spec.rb index c75fc0c..a0213a6 100644 --- a/spec/couchrest/more/extended_doc_attachment_spec.rb +++ b/spec/couchrest/more/extended_doc_attachment_spec.rb @@ -4,6 +4,7 @@ describe "ExtendedDocument attachments" do describe "#has_attachment?" do before(:each) do + reset_test_db! @obj = Basic.new @obj.save.should == true @file = File.open(FIXTURE_PATH + '/attachments/test.html') @@ -126,4 +127,4 @@ describe "ExtendedDocument attachments" do @obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}" end end -end \ No newline at end of file +end diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index f36394a..190baba 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -442,7 +442,7 @@ describe "ExtendedDocument" do @dobj.destroy @dobj.rev.should be_nil @dobj.id.should be_nil - @dobj.save.should == true + @dobj.save.should == true end it "should make it go away" do @dobj.destroy diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index 9045021..4a3e6ee 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -190,6 +190,7 @@ describe "ExtendedDocument views" do describe "with a lot of designs left around" do before(:each) do + reset_test_db! Article.by_date Article.view_by :field Article.by_field @@ -203,4 +204,4 @@ describe "ExtendedDocument views" do ddocs["rows"].length.should == 1 end end -end \ No newline at end of file +end diff --git a/spec/couchrest/more/property_spec.rb b/spec/couchrest/more/property_spec.rb index ec81d50..723c9ac 100644 --- a/spec/couchrest/more/property_spec.rb +++ b/spec/couchrest/more/property_spec.rb @@ -8,6 +8,7 @@ require File.join(FIXTURE_PATH, 'more', 'event') describe "ExtendedDocument properties" do before(:each) do + reset_test_db! @card = Card.new(:first_name => "matt") end @@ -126,4 +127,4 @@ describe "ExtendedDocument properties" do end end -end \ No newline at end of file +end