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/lib/couchrest/mixins/attachments.rb b/lib/couchrest/mixins/attachments.rb index f009b3e..b4c1bfe 100644 --- a/lib/couchrest/mixins/attachments.rb +++ b/lib/couchrest/mixins/attachments.rb @@ -19,9 +19,9 @@ module CouchRest end # deletes an attachment directly from couchdb - def delete_attachment(name) + def delete_attachment(name, force=false) raise ArgumentError, "doc.database required to delete_attachment" unless database - result = database.delete_attachment(self, name) + result = database.delete_attachment(self, name, force) self['_rev'] = result['rev'] result['ok'] end 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