merged jchris/master
This commit is contained in:
commit
aad6b8383d
|
@ -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"])
|
||||||
|
|
|
@ -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'
|
||||||
|
|
|
@ -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
|
||||||
|
|
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
|
_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
|
||||||
|
|
|
@ -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,27 +170,11 @@ 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)
|
||||||
lambda do
|
lambda do
|
||||||
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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')
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue