added an option to force the deletion of a attachment
This commit is contained in:
parent
beb801d1bd
commit
5270fde0fa
|
@ -113,10 +113,21 @@ module CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
# DELETE an attachment directly from CouchDB
|
# DELETE an attachment directly from CouchDB
|
||||||
def delete_attachment doc, name
|
def delete_attachment(doc, name, force=false)
|
||||||
uri = url_for_attachment(doc, name)
|
uri = url_for_attachment(doc, name)
|
||||||
# this needs a rev
|
# 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
|
end
|
||||||
|
|
||||||
# Save a document to CouchDB. This will use the <tt>_id</tt> field from
|
# Save a document to CouchDB. This will use the <tt>_id</tt> field from
|
||||||
|
|
|
@ -19,9 +19,9 @@ module CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
# deletes an attachment directly from couchdb
|
# 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
|
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']
|
self['_rev'] = result['rev']
|
||||||
result['ok']
|
result['ok']
|
||||||
end
|
end
|
||||||
|
|
|
@ -372,6 +372,15 @@ describe CouchRest::Database do
|
||||||
@doc = @db.get('mydocwithattachment') # avoid getting a 409
|
@doc = @db.get('mydocwithattachment') # avoid getting a 409
|
||||||
lambda{ @db.fetch_attachment(@doc,'test.html')}.should raise_error
|
lambda{ @db.fetch_attachment(@doc,'test.html')}.should raise_error
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "POST document with attachment (with funky name)" do
|
describe "POST document with attachment (with funky name)" do
|
||||||
|
|
Loading…
Reference in a new issue