Skip to content

Commit

Permalink
RUBY-537 fixing behavior of strict, adding deprecation notice
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Black committed Mar 15, 2013
1 parent b10d51c commit d3439f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
53 changes: 34 additions & 19 deletions lib/mongo/db.rb
Expand Up @@ -22,10 +22,24 @@ class DB
# collection that already exists, raises an error. # collection that already exists, raises an error.
# #
# Strict mode is disabled by default, but enabled (+true+) at any time. # 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. # 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. # The name of the database and the local write concern options.
attr_reader :name, :write_concern attr_reader :name, :write_concern
Expand Down Expand Up @@ -272,20 +286,19 @@ def collections_info(coll_name=nil)
# @return [Mongo::Collection] # @return [Mongo::Collection]
def create_collection(name, opts={}) def create_collection(name, opts={})
name = name.to_s name = name.to_s
if collection_names.include?(name) if strict? && collection_names.include?(name)
if strict? raise MongoDBError, "Collection '#{name}' already exists. (strict=true)"
raise MongoDBError, "Collection #{name} already exists. " +
"Currently in strict mode."
else
return Collection.new(name, self, opts)
end
end end


# Create a new collection. begin
oh = BSON::OrderedHash.new cmd = BSON::OrderedHash.new
oh[:create] = name cmd[:create] = name
doc = command(oh.merge(opts || {})) doc = command(cmd.merge(opts || {}))
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc) 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}" raise MongoDBError, "Error creating collection: #{doc.inspect}"
end end


Expand All @@ -300,8 +313,7 @@ def create_collection(name, opts={})
# @return [Mongo::Collection] # @return [Mongo::Collection]
def collection(name, opts={}) def collection(name, opts={})
if strict? && !collection_names.include?(name.to_s) if strict? && !collection_names.include?(name.to_s)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " + raise MongoDBError, "Collection '#{name}' doesn't exist. (strict=true)"
"Currently in strict mode."
else else
opts = opts.dup opts = opts.dup
opts.merge!(:pk => @pk_factory) unless opts[:pk] opts.merge!(:pk => @pk_factory) unless opts[:pk]
Expand All @@ -316,9 +328,12 @@ def collection(name, opts={})
# #
# @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist. # @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
def drop_collection(name) def drop_collection(name)
return true unless collection_names.include?(name.to_s) return false if strict? && !collection_names.include?(name.to_s)

begin
ok?(command(:drop => name)) ok?(command(:drop => name, :check_response => false))
rescue OperationFailure => e
false
end
end end


# Run the getlasterror command with the specified replication options. # Run the getlasterror command with the specified replication options.
Expand Down
11 changes: 5 additions & 6 deletions test/functional/db_api_test.rb
Expand Up @@ -92,7 +92,7 @@ def test_find_simple
# Can't compare _id values because at insert, an _id was added to @r1 by # 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 # the database but we don't know what it is without re-reading the record
# (which is what we are doing right now). # (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'] assert_equal doc['a'], @r1['a']
end end


Expand Down Expand Up @@ -194,7 +194,7 @@ def test_find_sorting_with_hash
@@coll.insert('a' => 2, 'b' => 1) @@coll.insert('a' => 2, 'b' => 1)
@@coll.insert('a' => 3, 'b' => 2) @@coll.insert('a' => 3, 'b' => 2)
@@coll.insert('a' => 4, 'b' => 1) @@coll.insert('a' => 4, 'b' => 1)

oh = BSON::OrderedHash.new oh = BSON::OrderedHash.new
oh['a'] = -1 oh['a'] = -1


Expand Down Expand Up @@ -312,7 +312,7 @@ def test_collection_options


begin begin
coll = @@db.create_collection('foobar', :capped => true, :size => 1024) coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
options = coll.options() options = coll.options
assert_equal 'foobar', options['create'] assert_equal 'foobar', options['create']
assert_equal true, options['capped'] assert_equal true, options['capped']
assert_equal 1024, options['size'] assert_equal 1024, options['size']
Expand Down Expand Up @@ -488,7 +488,7 @@ def test_strict_access_collection
fail "expected exception" fail "expected exception"
rescue => ex rescue => ex
assert_equal Mongo::MongoDBError, ex.class 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 ensure
@@db.strict = false @@db.strict = false
@@db.drop_collection('does-not-exist') @@db.drop_collection('does-not-exist')
Expand All @@ -500,8 +500,7 @@ def test_strict_create_collection
@@db.strict = true @@db.strict = true


begin begin
@@db.create_collection('foobar') assert @@db.create_collection('foobar')
assert true
rescue => ex rescue => ex
fail "did not expect exception \"#{ex}\"" fail "did not expect exception \"#{ex}\""
end end
Expand Down
1 change: 0 additions & 1 deletion test/unit/db_test.rb
Expand Up @@ -81,7 +81,6 @@ class DBTest < Test::Unit::TestCase
end end


should "raise an error if collection creation fails" do should "raise an error if collection creation fails" do
@db.expects(:collection_names).returns([])
@db.expects(:command).returns({'ok' => 0}) @db.expects(:command).returns({'ok' => 0})
assert_raise Mongo::MongoDBError do assert_raise Mongo::MongoDBError do
@db.create_collection("foo") @db.create_collection("foo")
Expand Down

0 comments on commit d3439f9

Please sign in to comment.