diff --git a/lib/couchrest.rb b/lib/couchrest.rb index e88d5a8..2f0c577 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -96,15 +96,18 @@ module CouchRest def parse url case url - when /^https?:\/\/(.*)\/(.*)\/(.*)/ - host = $1 - db = $2 - docid = $3 - when /^https?:\/\/(.*)\/(.*)/ - host = $1 - db = $2 - when /^https?:\/\/(.*)/ - host = $1 + when /^(https?:\/\/)(.*)\/(.*)\/(.*)/ + scheme = $1 + host = $2 + db = $3 + docid = $4 + when /^(https?:\/\/)(.*)\/(.*)/ + scheme = $1 + host = $2 + db = $3 + when /^(https?:\/\/)(.*)/ + scheme = $1 + host = $2 when /(.*)\/(.*)\/(.*)/ host = $1 db = $2 @@ -117,9 +120,9 @@ module CouchRest end db = nil if db && db.empty? - + { - :host => host || "127.0.0.1:5984", + :host => (scheme || "http://") + (host || "127.0.0.1:5984"), :database => db, :doc => docid } diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index 36597fc..12a23b5 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -297,15 +297,13 @@ module CouchRest end # Replicates via "pulling" from another database to this database. Makes no attempt to deal with conflicts. - def replicate_from other_db - raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) - CouchRest.post "#{@host}/_replicate", :source => other_db.root, :target => name + def replicate_from other_db, continuous=false + replicate other_db, continuous, :target => name end # Replicates via "pushing" to another database. Makes no attempt to deal with conflicts. - def replicate_to other_db - raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) - CouchRest.post "#{@host}/_replicate", :target => other_db.root, :source => name + def replicate_to other_db, continuous=false + replicate other_db, continuous, :source => name end # DELETE the database itself. This is not undoable and could be rather @@ -317,6 +315,19 @@ module CouchRest private + def replicate other_db, continuous, options + raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) + raise ArgumentError, "must provide a target or source option" unless (options.key?(:target) || options.key?(:source)) + payload = options + if options.has_key?(:target) + payload[:source] = other_db.root + else + payload[:target] = other_db.root + end + payload[:continuous] = continuous + CouchRest.post "#{@host}/_replicate", payload + end + def clear_extended_doc_fresh_cache ::CouchRest::ExtendedDocument.subclasses.each{|klass| klass.design_doc_fresh = false if klass.respond_to?(:design_doc_fresh=) } end diff --git a/spec/couchrest/core/couchrest_spec.rb b/spec/couchrest/core/couchrest_spec.rb index faf6847..0947fa1 100644 --- a/spec/couchrest/core/couchrest_spec.rb +++ b/spec/couchrest/core/couchrest_spec.rb @@ -46,137 +46,74 @@ describe CouchRest do it "should parse just a dbname" do db = CouchRest.parse "my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5984" + db[:host].should == "http://127.0.0.1:5984" end it "should parse a host and db" do db = CouchRest.parse "127.0.0.1/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1" - end - it "should parse a host and db with http" do - db = CouchRest.parse "https://127.0.0.1/my-db" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1" - end - it "should parse a host with a port and db" do - db = CouchRest.parse "127.0.0.1:5555/my-db" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - end - it "should parse a host with a port and db with http" do - db = CouchRest.parse "http://127.0.0.1:5555/my-db" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - end - it "should parse a host with a port and db with https" do - db = CouchRest.parse "https://127.0.0.1:5555/my-db" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - end - it "should parse just a host" do - db = CouchRest.parse "http://127.0.0.1:5555/" - db[:database].should be_nil - db[:host].should == "127.0.0.1:5555" - end - it "should parse just a host with https" do - db = CouchRest.parse "https://127.0.0.1:5555/" - db[:database].should be_nil - db[:host].should == "127.0.0.1:5555" - end - it "should parse just a host no slash" do - db = CouchRest.parse "http://127.0.0.1:5555" - db[:host].should == "127.0.0.1:5555" - db[:database].should be_nil - end - it "should parse just a host no slash and https" do - db = CouchRest.parse "https://127.0.0.1:5555" - db[:host].should == "127.0.0.1:5555" - db[:database].should be_nil - end - it "should get docid" do - db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - db[:doc].should == "my-doc" - end - it "should get docid with http" do - db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - db[:doc].should == "my-doc" - end - it "should get docid with https" do - db = CouchRest.parse "https://127.0.0.1:5555/my-db/my-doc" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" - db[:doc].should == "my-doc" - end - it "should parse a host and db" do - db = CouchRest.parse "127.0.0.1/my-db" - db[:database].should == "my-db" - db[:host].should == "127.0.0.1" + db[:host].should == "http://127.0.0.1" end it "should parse a host and db with http" do db = CouchRest.parse "http://127.0.0.1/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1" + db[:host].should == "http://127.0.0.1" end it "should parse a host and db with https" do db = CouchRest.parse "https://127.0.0.1/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1" + db[:host].should == "https://127.0.0.1" end it "should parse a host with a port and db" do db = CouchRest.parse "127.0.0.1:5555/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" end it "should parse a host with a port and db with http" do db = CouchRest.parse "http://127.0.0.1:5555/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" end it "should parse a host with a port and db with https" do - db = CouchRest.parse "http://127.0.0.1:5555/my-db" + db = CouchRest.parse "https://127.0.0.1:5555/my-db" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "https://127.0.0.1:5555" end it "should parse just a host" do db = CouchRest.parse "http://127.0.0.1:5555/" db[:database].should be_nil - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" end it "should parse just a host with https" do db = CouchRest.parse "https://127.0.0.1:5555/" db[:database].should be_nil - db[:host].should == "127.0.0.1:5555" + db[:host].should == "https://127.0.0.1:5555" end it "should parse just a host no slash" do db = CouchRest.parse "http://127.0.0.1:5555" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" db[:database].should be_nil end it "should parse just a host no slash and https" do db = CouchRest.parse "https://127.0.0.1:5555" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "https://127.0.0.1:5555" db[:database].should be_nil end it "should get docid" do db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" db[:doc].should == "my-doc" end it "should get docid with http" do db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "http://127.0.0.1:5555" db[:doc].should == "my-doc" end it "should get docid with https" do db = CouchRest.parse "https://127.0.0.1:5555/my-db/my-doc" db[:database].should == "my-db" - db[:host].should == "127.0.0.1:5555" + db[:host].should == "https://127.0.0.1:5555" db[:doc].should == "my-doc" end end @@ -185,7 +122,7 @@ describe CouchRest do it "should be possible without an explicit CouchRest instantiation" do db = CouchRest.database "http://127.0.0.1:5984/couchrest-test" db.should be_an_instance_of(CouchRest::Database) - db.host.should == "127.0.0.1:5984" + db.host.should == "http://127.0.0.1:5984" end # TODO add https support (need test environment...) # it "should work with https" # do diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 28a57dd..a9641f1 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -703,12 +703,12 @@ describe CouchRest::Database do end end - describe "replicating a database" do + describe "simply replicating a database" do before do @db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'}) - @other_db = @cr.database 'couchrest-test-replication' + @other_db = @cr.database REPLICATIONDB @other_db.delete! rescue nil - @other_db = @cr.create_db 'couchrest-test-replication' + @other_db = @cr.create_db REPLICATIONDB end describe "via pulling" do @@ -733,6 +733,53 @@ describe CouchRest::Database do end end end + + describe "continuously replicating a database" do + before do + @db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'}) + @other_db = @cr.database REPLICATIONDB + @other_db.delete! rescue nil + @other_db = @cr.create_db REPLICATIONDB + end + + describe "via pulling" do + before do + @other_db.replicate_from @db, true + end + + it "contains the document from the original database" do + sleep(1) # Allow some time to replicate + doc = @other_db.get('test_doc') + doc['some-value'].should == 'foo' + end + + it "contains documents saved after replication initiated" do + @db.save_doc({'_id' => 'test_doc_after', 'some-value' => 'bar'}) + sleep(1) # Allow some time to replicate + doc = @other_db.get('test_doc_after') + doc['some-value'].should == 'bar' + end + end + + describe "via pushing" do + before do + @db.replicate_to @other_db, true + end + + it "copies the document to the other database" do + sleep(1) # Allow some time to replicate + doc = @other_db.get('test_doc') + doc['some-value'].should == 'foo' + end + + it "copies documents saved after replication initiated" do + @db.save_doc({'_id' => 'test_doc_after', 'some-value' => 'bar'}) + sleep(1) # Allow some time to replicate + doc = @other_db.get('test_doc_after') + doc['some-value'].should == 'bar' + end + end + end describe "creating a database" do before(:each) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e750258..921b808 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,7 @@ unless defined?(FIXTURE_PATH) COUCHHOST = "http://127.0.0.1:5984" TESTDB = 'couchrest-test' + REPLICATIONDB = 'couchrest-test-replication' TEST_SERVER = CouchRest.new TEST_SERVER.default_database = TESTDB DB = TEST_SERVER.database(TESTDB)