Patching in fixes for missing uri scheme in CouchRest.parse and adding support for continuous replication

This commit is contained in:
sauy7 2010-03-10 10:24:28 +01:00 committed by Will Leinweber
parent 0d0a8ae6b8
commit 3c314e14d0
5 changed files with 98 additions and 99 deletions

View file

@ -96,15 +96,18 @@ module CouchRest
def parse url def parse url
case url case url
when /^https?:\/\/(.*)\/(.*)\/(.*)/ when /^(https?:\/\/)(.*)\/(.*)\/(.*)/
host = $1 scheme = $1
db = $2 host = $2
docid = $3 db = $3
when /^https?:\/\/(.*)\/(.*)/ docid = $4
host = $1 when /^(https?:\/\/)(.*)\/(.*)/
db = $2 scheme = $1
when /^https?:\/\/(.*)/ host = $2
host = $1 db = $3
when /^(https?:\/\/)(.*)/
scheme = $1
host = $2
when /(.*)\/(.*)\/(.*)/ when /(.*)\/(.*)\/(.*)/
host = $1 host = $1
db = $2 db = $2
@ -117,9 +120,9 @@ module CouchRest
end end
db = nil if db && db.empty? db = nil if db && db.empty?
{ {
:host => host || "127.0.0.1:5984", :host => (scheme || "http://") + (host || "127.0.0.1:5984"),
:database => db, :database => db,
:doc => docid :doc => docid
} }

View file

@ -297,15 +297,13 @@ module CouchRest
end end
# Replicates via "pulling" from another database to this database. Makes no attempt to deal with conflicts. # Replicates via "pulling" from another database to this database. Makes no attempt to deal with conflicts.
def replicate_from other_db def replicate_from other_db, continuous=false
raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) replicate other_db, continuous, :target => name
CouchRest.post "#{@host}/_replicate", :source => other_db.root, :target => name
end end
# Replicates via "pushing" to another database. Makes no attempt to deal with conflicts. # Replicates via "pushing" to another database. Makes no attempt to deal with conflicts.
def replicate_to other_db def replicate_to other_db, continuous=false
raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) replicate other_db, continuous, :source => name
CouchRest.post "#{@host}/_replicate", :target => other_db.root, :source => name
end end
# DELETE the database itself. This is not undoable and could be rather # DELETE the database itself. This is not undoable and could be rather
@ -317,6 +315,19 @@ module CouchRest
private 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 def clear_extended_doc_fresh_cache
::CouchRest::ExtendedDocument.subclasses.each{|klass| klass.design_doc_fresh = false if klass.respond_to?(:design_doc_fresh=) } ::CouchRest::ExtendedDocument.subclasses.each{|klass| klass.design_doc_fresh = false if klass.respond_to?(:design_doc_fresh=) }
end end

View file

@ -46,137 +46,74 @@ describe CouchRest do
it "should parse just a dbname" do it "should parse just a dbname" do
db = CouchRest.parse "my-db" db = CouchRest.parse "my-db"
db[:database].should == "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 end
it "should parse a host and db" do it "should parse a host and db" do
db = CouchRest.parse "127.0.0.1/my-db" db = CouchRest.parse "127.0.0.1/my-db"
db[:database].should == "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 "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"
end end
it "should parse a host and db with http" do it "should parse a host and db with http" do
db = CouchRest.parse "http://127.0.0.1/my-db" db = CouchRest.parse "http://127.0.0.1/my-db"
db[:database].should == "my-db" db[:database].should == "my-db"
db[:host].should == "127.0.0.1" db[:host].should == "http://127.0.0.1"
end end
it "should parse a host and db with https" do it "should parse a host and db with https" do
db = CouchRest.parse "https://127.0.0.1/my-db" db = CouchRest.parse "https://127.0.0.1/my-db"
db[:database].should == "my-db" db[:database].should == "my-db"
db[:host].should == "127.0.0.1" db[:host].should == "https://127.0.0.1"
end end
it "should parse a host with a port and db" do it "should parse a host with a port and db" do
db = CouchRest.parse "127.0.0.1:5555/my-db" db = CouchRest.parse "127.0.0.1:5555/my-db"
db[:database].should == "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 end
it "should parse a host with a port and db with http" do 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 = CouchRest.parse "http://127.0.0.1:5555/my-db"
db[:database].should == "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 end
it "should parse a host with a port and db with https" do 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[:database].should == "my-db"
db[:host].should == "127.0.0.1:5555" db[:host].should == "https://127.0.0.1:5555"
end end
it "should parse just a host" do it "should parse just a host" do
db = CouchRest.parse "http://127.0.0.1:5555/" db = CouchRest.parse "http://127.0.0.1:5555/"
db[:database].should be_nil db[:database].should be_nil
db[:host].should == "127.0.0.1:5555" db[:host].should == "http://127.0.0.1:5555"
end end
it "should parse just a host with https" do it "should parse just a host with https" do
db = CouchRest.parse "https://127.0.0.1:5555/" db = CouchRest.parse "https://127.0.0.1:5555/"
db[:database].should be_nil db[:database].should be_nil
db[:host].should == "127.0.0.1:5555" db[:host].should == "https://127.0.0.1:5555"
end end
it "should parse just a host no slash" do it "should parse just a host no slash" do
db = CouchRest.parse "http://127.0.0.1:5555" 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 db[:database].should be_nil
end end
it "should parse just a host no slash and https" do it "should parse just a host no slash and https" do
db = CouchRest.parse "https://127.0.0.1:5555" 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 db[:database].should be_nil
end end
it "should get docid" do it "should get docid" do
db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc" db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db" 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" db[:doc].should == "my-doc"
end end
it "should get docid with http" do it "should get docid with http" do
db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc" db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db" 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" db[:doc].should == "my-doc"
end end
it "should get docid with https" do it "should get docid with https" do
db = CouchRest.parse "https://127.0.0.1:5555/my-db/my-doc" db = CouchRest.parse "https://127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db" 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" db[:doc].should == "my-doc"
end end
end end
@ -185,7 +122,7 @@ describe CouchRest do
it "should be possible without an explicit CouchRest instantiation" do it "should be possible without an explicit CouchRest instantiation" do
db = CouchRest.database "http://127.0.0.1:5984/couchrest-test" db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
db.should be_an_instance_of(CouchRest::Database) 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 end
# TODO add https support (need test environment...) # TODO add https support (need test environment...)
# it "should work with https" # do # it "should work with https" # do

View file

@ -703,12 +703,12 @@ describe CouchRest::Database do
end end
end end
describe "replicating a database" do describe "simply replicating a database" do
before do before do
@db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'}) @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.delete! rescue nil
@other_db = @cr.create_db 'couchrest-test-replication' @other_db = @cr.create_db REPLICATIONDB
end end
describe "via pulling" do describe "via pulling" do
@ -733,6 +733,53 @@ describe CouchRest::Database do
end end
end 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 describe "creating a database" do
before(:each) do before(:each) do

View file

@ -10,6 +10,7 @@ unless defined?(FIXTURE_PATH)
COUCHHOST = "http://127.0.0.1:5984" COUCHHOST = "http://127.0.0.1:5984"
TESTDB = 'couchrest-test' TESTDB = 'couchrest-test'
REPLICATIONDB = 'couchrest-test-replication'
TEST_SERVER = CouchRest.new TEST_SERVER = CouchRest.new
TEST_SERVER.default_database = TESTDB TEST_SERVER.default_database = TESTDB
DB = TEST_SERVER.database(TESTDB) DB = TEST_SERVER.database(TESTDB)