RUBY-141 raise exception if attempting to overwrite with Grid#put (only in safe mode)

This commit is contained in:
Kyle Banker 2010-07-09 13:33:26 -04:00
parent 24e8b690ed
commit f22e81414b
3 changed files with 16 additions and 2 deletions

View File

@ -41,10 +41,11 @@ module Mongo
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end
# Store a file in the file store.
# Store a file in the file store. This method is designed only for writing new files;
# if you need to update a given file, first delete it using #Grid#delete.
#
# Note that arbitary metadata attributes can be saved to the file by passing
# them is as options.
# them in as options.
#
# @param [String, #read] data a string or io-like object to store.
#

View File

@ -315,11 +315,18 @@ module Mongo
@aliases = opts.delete(:aliases) if opts[:aliases]
@file_length = 0
opts.each {|k, v| self[k] = v}
check_existing_file if @safe
@current_chunk = create_chunk(0)
@file_position = 0
end
def check_existing_file
if @files.find_one('_id' => @files_id)
raise GridError, "Attempting to overwrite with Grid#put. You must delete the file first."
end
end
def to_mongo_object
h = BSON::OrderedHash.new
h['_id'] = @files_id

View File

@ -27,6 +27,12 @@ class GridTest < Test::Unit::TestCase
assert_equal 'sample', file['filename']
end
should "not be able to overwrite an exising file" do
assert_raise GridError do
@grid.put(@data, :filename => 'sample', :_id => @id, :safe => true)
end
end
should "return nil if it doesn't exist" do
assert_nil @grid.exist?(:metadata => 'foo')
end