added an option to force the deletion of a attachment

This commit is contained in:
Matt Aimonetti 2009-07-29 20:07:02 -07:00
parent beb801d1bd
commit 41055fd5ad
2 changed files with 22 additions and 2 deletions

View file

@ -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 <tt>_id</tt> field from

View file

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