diff --git a/lib/mongo/gridfs/grid_file_system.rb b/lib/mongo/gridfs/grid_file_system.rb index 41a18ca..2cd1ae4 100644 --- a/lib/mongo/gridfs/grid_file_system.rb +++ b/lib/mongo/gridfs/grid_file_system.rb @@ -97,7 +97,12 @@ module Mongo def open(filename, mode, opts={}) opts = opts.dup opts.merge!(default_grid_io_opts(filename)) - versions = opts.delete(:versions) && mode == 'w' + if mode == 'w' + versions = opts.delete(:versions) + if opts.delete(:delete_old) || (versions && versions < 1) + versions = 1 + end + end file = GridIO.new(@files, @chunks, filename, mode, opts) return file unless block_given? result = nil diff --git a/test/grid_file_system_test.rb b/test/grid_file_system_test.rb index bac67f4..0cae6b0 100644 --- a/test/grid_file_system_test.rb +++ b/test/grid_file_system_test.rb @@ -165,6 +165,18 @@ class GridFileSystemTest < Test::Unit::TestCase assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count end + should "delete all versions which exceed the number of versions to keep specified by the option :versions" do + @versions = 1 + rand(4-1) + @grid.open('sample', 'w', :versions => @versions) do |f| + f.write @new_data + end + @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']} + assert_equal @versions, @new_ids.length + id = @new_ids.first + assert !@ids.include?(id) + assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count + end + should "delete old versions on write with :delete_old is passed in" do @grid.open('sample', 'w', :delete_old => true) do |f| f.write @new_data