Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RUBY-537 fixing behavior of strict, adding deprecation notice

  • Loading branch information...
commit d3439f984430493ec31530b9447ae6dda1f5d280 1 parent b10d51c
@brandonblack brandonblack authored
View
53 lib/mongo/db.rb
@@ -22,10 +22,24 @@ class DB
# collection that already exists, raises an error.
#
# Strict mode is disabled by default, but enabled (+true+) at any time.
- attr_writer :strict
+ #
+ # @deprecated Support for strict mode has been deprecated and will be
+ # removed in version 2.0 of the driver.
+ def strict=(value)
+ unless ENV['TEST_MODE']
+ warn "Support for strict mode has been deprecated and will be " +
+ "removed in version 2.0 of the driver."
+ end
+ @strict = value
+ end
# Returns the value of the +strict+ flag.
- def strict?; @strict; end
+ #
+ # @deprecated Support for strict mode has been deprecated and will be
+ # removed in version 2.0 of the driver.
+ def strict?
+ @strict
+ end
# The name of the database and the local write concern options.
attr_reader :name, :write_concern
@@ -272,20 +286,19 @@ def collections_info(coll_name=nil)
# @return [Mongo::Collection]
def create_collection(name, opts={})
name = name.to_s
- if collection_names.include?(name)
- if strict?
- raise MongoDBError, "Collection #{name} already exists. " +
- "Currently in strict mode."
- else
- return Collection.new(name, self, opts)
- end
+ if strict? && collection_names.include?(name)
+ raise MongoDBError, "Collection '#{name}' already exists. (strict=true)"
end
- # Create a new collection.
- oh = BSON::OrderedHash.new
- oh[:create] = name
- doc = command(oh.merge(opts || {}))
- return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
+ begin
+ cmd = BSON::OrderedHash.new
+ cmd[:create] = name
+ doc = command(cmd.merge(opts || {}))
+ return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
+ rescue OperationFailure => e
+ return Collection.new(name, self, :pk => @pk_factory) if e.message =~ /exists/
+ raise e
+ end
raise MongoDBError, "Error creating collection: #{doc.inspect}"
end
@@ -300,8 +313,7 @@ def create_collection(name, opts={})
# @return [Mongo::Collection]
def collection(name, opts={})
if strict? && !collection_names.include?(name.to_s)
- raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " +
- "Currently in strict mode."
+ raise MongoDBError, "Collection '#{name}' doesn't exist. (strict=true)"
else
opts = opts.dup
opts.merge!(:pk => @pk_factory) unless opts[:pk]
@@ -316,9 +328,12 @@ def collection(name, opts={})
#
# @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
def drop_collection(name)
- return true unless collection_names.include?(name.to_s)
-
- ok?(command(:drop => name))
+ return false if strict? && !collection_names.include?(name.to_s)
+ begin
+ ok?(command(:drop => name, :check_response => false))
+ rescue OperationFailure => e
+ false
+ end
end
# Run the getlasterror command with the specified replication options.
View
11 test/functional/db_api_test.rb
@@ -92,7 +92,7 @@ def test_find_simple
# Can't compare _id values because at insert, an _id was added to @r1 by
# the database but we don't know what it is without re-reading the record
# (which is what we are doing right now).
-# assert_equal doc['_id'], @r1['_id']
+ # assert_equal doc['_id'], @r1['_id']
assert_equal doc['a'], @r1['a']
end
@@ -194,7 +194,7 @@ def test_find_sorting_with_hash
@@coll.insert('a' => 2, 'b' => 1)
@@coll.insert('a' => 3, 'b' => 2)
@@coll.insert('a' => 4, 'b' => 1)
-
+
oh = BSON::OrderedHash.new
oh['a'] = -1
@@ -312,7 +312,7 @@ def test_collection_options
begin
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
- options = coll.options()
+ options = coll.options
assert_equal 'foobar', options['create']
assert_equal true, options['capped']
assert_equal 1024, options['size']
@@ -488,7 +488,7 @@ def test_strict_access_collection
fail "expected exception"
rescue => ex
assert_equal Mongo::MongoDBError, ex.class
- assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
+ assert_equal "Collection 'does-not-exist' doesn't exist. (strict=true)", ex.to_s
ensure
@@db.strict = false
@@db.drop_collection('does-not-exist')
@@ -500,8 +500,7 @@ def test_strict_create_collection
@@db.strict = true
begin
- @@db.create_collection('foobar')
- assert true
+ assert @@db.create_collection('foobar')
rescue => ex
fail "did not expect exception \"#{ex}\""
end
View
1  test/unit/db_test.rb
@@ -81,7 +81,6 @@ class DBTest < Test::Unit::TestCase
end
should "raise an error if collection creation fails" do
- @db.expects(:collection_names).returns([])
@db.expects(:command).returns({'ok' => 0})
assert_raise Mongo::MongoDBError do
@db.create_collection("foo")
Please sign in to comment.
Something went wrong with that request. Please try again.