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
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<rest-client>, [">= 0.5"])
s.add_runtime_dependency(%q<mime-types>, [">= 1.15"])

View file

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

View file

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

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

View file

@ -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,27 +170,11 @@ 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)
lambda do
@ -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

View file

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

View file

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

View file

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

View file

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

View file

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