RUBY-193 don't create gridfs indexes when slave_ok

This commit is contained in:
Kyle Banker 2010-10-21 13:01:32 -04:00
parent 048e9e6fbe
commit afe8fe3167
3 changed files with 56 additions and 3 deletions

View File

@ -38,8 +38,11 @@ module Mongo
@chunks = @db["#{fs_name}.chunks"] @chunks = @db["#{fs_name}.chunks"]
@fs_name = fs_name @fs_name = fs_name
# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true) @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end end
end
# Store a file in the file store. This method is designed only for writing new files; # 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. # if you need to update a given file, first delete it using #Grid#delete.

View File

@ -39,9 +39,12 @@ module Mongo
@default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1} @default_query_opts = {:sort => [['filename', 1], ['uploadDate', -1]], :limit => 1}
# Ensure indexes only if not connected to slave.
unless db.connection.slave_ok?
@files.create_index([['filename', 1], ['uploadDate', -1]]) @files.create_index([['filename', 1], ['uploadDate', -1]])
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true) @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
end end
end
# Open a file for reading or writing. Note that the options for this method only apply # Open a file for reading or writing. Note that the options for this method only apply
# when opening in 'w' mode. # when opening in 'w' mode.

47
test/unit/grid_test.rb Normal file
View File

@ -0,0 +1,47 @@
require './test/test_helper'
class GridTest < Test::Unit::TestCase
context "GridFS: " do
setup do
@conn = stub()
@db = DB.new("testing", @conn)
@files = mock()
@chunks = mock()
@db.expects(:[]).with('fs.files').returns(@files)
@db.expects(:[]).with('fs.chunks').returns(@chunks)
end
context "Grid classe with standard connections" do
setup do
@conn.expects(:slave_ok?).returns(false)
end
should "create indexes for Grid" do
@chunks.expects(:create_index)
Grid.new(@db)
end
should "create indexes for GridFileSystem" do
@files.expects(:create_index)
@chunks.expects(:create_index)
GridFileSystem.new(@db)
end
end
context "Grid classes with slave connection" do
setup do
@conn.expects(:slave_ok?).returns(true)
end
should "not create indexes for Grid" do
Grid.new(@db)
end
should "not create indexes for GridFileSystem" do
GridFileSystem.new(@db)
end
end
end
end