merged jchris/master
This commit is contained in:
commit
aad6b8383d
12 changed files with 79 additions and 35 deletions
|
@ -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"])
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
51
lib/couchrest/helper/upgrade.rb
Normal file
51
lib/couchrest/helper/upgrade.rb
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue