merged jchris/master

This commit is contained in:
Matt Aimonetti 2009-03-16 14:57:29 -07:00
commit aad6b8383d
12 changed files with 79 additions and 35 deletions

View file

@ -21,7 +21,7 @@ Gem::Specification.new do |s|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2 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<json>, [">= 1.1.2"]) s.add_runtime_dependency(%q<json>, [">= 1.1.2"])
s.add_runtime_dependency(%q<rest-client>, [">= 0.5"]) s.add_runtime_dependency(%q<rest-client>, [">= 0.5"])
s.add_runtime_dependency(%q<mime-types>, [">= 1.15"]) s.add_runtime_dependency(%q<mime-types>, [">= 1.15"])

View file

@ -40,6 +40,7 @@ module CouchRest
autoload :Pager, 'couchrest/helper/pager' autoload :Pager, 'couchrest/helper/pager'
autoload :FileManager, 'couchrest/helper/file_manager' autoload :FileManager, 'couchrest/helper/file_manager'
autoload :Streamer, 'couchrest/helper/streamer' autoload :Streamer, 'couchrest/helper/streamer'
autoload :Upgrade, 'couchrest/helper/upgrade'
autoload :ExtendedDocument, 'couchrest/more/extended_document' autoload :ExtendedDocument, 'couchrest/more/extended_document'
autoload :CastedModel, 'couchrest/more/casted_model' autoload :CastedModel, 'couchrest/more/casted_model'

View file

@ -77,13 +77,15 @@ module CouchRest
end end
# GET a document from CouchDB, by id. Returns a Ruby Hash. # GET a document from CouchDB, by id. Returns a Ruby Hash.
def get(id) def get(id, params = {})
slug = escape_docid(id) slug = escape_docid(id)
hash = CouchRest.get("#{@uri}/#{slug}") url = CouchRest.paramify_url("#{@uri}/#{slug}", params)
doc = if /^_design/ =~ hash["_id"] result = CouchRest.get(url)
Design.new(hash) return result unless result.is_a?(Hash)
doc = if /^_design/ =~ result["_id"]
Design.new(result)
else else
Document.new(hash) Document.new(result)
end end
doc.database = self doc.database = self
doc doc

View file

@ -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

View file

@ -205,8 +205,8 @@ module CouchRest
_run_destroy_callbacks do _run_destroy_callbacks do
result = database.delete_doc(self, bulk) result = database.delete_doc(self, bulk)
if result['ok'] if result['ok']
self['_rev'] = nil self.delete('_rev')
self['_id'] = nil self.delete('_id')
end end
result['ok'] result['ok']
end end

View file

@ -149,8 +149,8 @@ describe CouchRest::Database do
{"mild" => "yet local"}, {"mild" => "yet local"},
{"another" => ["set","of","keys"]} {"another" => ["set","of","keys"]}
]) ])
rs['new_revs'].each do |r| rs.each do |r|
@db.get(r['id']) @db.get(r['id']).rev.should == r["rev"]
end end
end end
@ -170,26 +170,10 @@ describe CouchRest::Database do
{"_id" => "twoB", "mild" => "yet local"}, {"_id" => "twoB", "mild" => "yet local"},
{"another" => ["set","of","keys"]} {"another" => ["set","of","keys"]}
]) ])
rs['new_revs'].each do |r| rs.each do |r|
@db.get(r['id']) @db.get(r['id']).rev.should == r["rev"]
end end
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 it "should empty the bulk save cache if no documents are given" do
@db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true) @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'}) @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
end end
it "should fail without a rev" do 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 lambda{@db.get(@r['id'])}.should_not raise_error
end end
it "should succeed with a rev" do it "should succeed with a rev" do

View file

@ -224,7 +224,8 @@ describe CouchRest::Document do
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'}) @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
end end
it "should fail without a rev" do 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 lambda{@db.get(@resp['id'])}.should_not raise_error
end end
it "should succeed with a rev" do it "should succeed with a rev" do

View file

@ -70,6 +70,7 @@ describe CouchRest::CastedModel do
describe "saved document with casted models" do describe "saved document with casted models" do
before(:each) do before(:each) do
reset_test_db!
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'}) @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
@obj.save.should be_true @obj.save.should be_true
@obj = DummyModel.get(@obj.id) @obj = DummyModel.get(@obj.id)
@ -94,4 +95,4 @@ describe CouchRest::CastedModel do
end end
end end

View file

@ -4,6 +4,7 @@ describe "ExtendedDocument attachments" do
describe "#has_attachment?" do describe "#has_attachment?" do
before(:each) do before(:each) do
reset_test_db!
@obj = Basic.new @obj = Basic.new
@obj.save.should == true @obj.save.should == true
@file = File.open(FIXTURE_PATH + '/attachments/test.html') @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}" @obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}"
end end
end end
end end

View file

@ -442,7 +442,7 @@ describe "ExtendedDocument" do
@dobj.destroy @dobj.destroy
@dobj.rev.should be_nil @dobj.rev.should be_nil
@dobj.id.should be_nil @dobj.id.should be_nil
@dobj.save.should == true @dobj.save.should == true
end end
it "should make it go away" do it "should make it go away" do
@dobj.destroy @dobj.destroy

View file

@ -190,6 +190,7 @@ describe "ExtendedDocument views" do
describe "with a lot of designs left around" do describe "with a lot of designs left around" do
before(:each) do before(:each) do
reset_test_db!
Article.by_date Article.by_date
Article.view_by :field Article.view_by :field
Article.by_field Article.by_field
@ -203,4 +204,4 @@ describe "ExtendedDocument views" do
ddocs["rows"].length.should == 1 ddocs["rows"].length.should == 1
end end
end end
end end

View file

@ -8,6 +8,7 @@ require File.join(FIXTURE_PATH, 'more', 'event')
describe "ExtendedDocument properties" do describe "ExtendedDocument properties" do
before(:each) do before(:each) do
reset_test_db!
@card = Card.new(:first_name => "matt") @card = Card.new(:first_name => "matt")
end end
@ -126,4 +127,4 @@ describe "ExtendedDocument properties" do
end end
end end
end end