From 41055fd5adeb8bcf4e43ed56428d86d1c5cad260 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Wed, 29 Jul 2009 20:07:02 -0700 Subject: [PATCH] added an option to force the deletion of a attachment --- lib/couchrest/core/database.rb | 15 +++++++++++++-- spec/couchrest/core/database_spec.rb | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index 08eb4cb..ce7e1a6 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -113,10 +113,21 @@ module CouchRest end # DELETE an attachment directly from CouchDB - def delete_attachment doc, name + def delete_attachment(doc, name, force=false) uri = url_for_attachment(doc, name) # this needs a rev - JSON.parse(HttpAbstraction.delete(uri)) + begin + JSON.parse(HttpAbstraction.delete(uri)) + rescue Exception => error + if force + # get over a 409 + doc = get(doc['_id']) + uri = url_for_attachment(doc, name) + JSON.parse(HttpAbstraction.delete(uri)) + else + error + end + end end # Save a document to CouchDB. This will use the _id field from diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 632ba52..d1c58ad 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -372,6 +372,15 @@ describe CouchRest::Database do @doc = @db.get('mydocwithattachment') # avoid getting a 409 lambda{ @db.fetch_attachment(@doc,'test.html')}.should raise_error end + + it "should force a delete even if we get a 409" do + @doc['new_attribute'] = 'something new' + @db.put_attachment(@doc, 'test', File.open(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'attachments', 'test.html')).read) + # at this point the revision number changed, if we try to save doc one more time + # we would get a 409. + lambda{ @db.save_doc(@doc) }.should raise_error + lambda{ @db.delete_attachment(@doc, "test", true) }.should_not raise_error + end end describe "POST document with attachment (with funky name)" do