Skip to content

Commit d3439f9

Browse files
author
Brandon Black
committed
RUBY-537 fixing behavior of strict, adding deprecation notice
1 parent b10d51c commit d3439f9

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

lib/mongo/db.rb

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ class DB
2222
# collection that already exists, raises an error.
2323
#
2424
# Strict mode is disabled by default, but enabled (+true+) at any time.
25-
attr_writer :strict
25+
#
26+
# @deprecated Support for strict mode has been deprecated and will be
27+
# removed in version 2.0 of the driver.
28+
def strict=(value)
29+
unless ENV['TEST_MODE']
30+
warn "Support for strict mode has been deprecated and will be " +
31+
"removed in version 2.0 of the driver."
32+
end
33+
@strict = value
34+
end
2635

2736
# Returns the value of the +strict+ flag.
28-
def strict?; @strict; end
37+
#
38+
# @deprecated Support for strict mode has been deprecated and will be
39+
# removed in version 2.0 of the driver.
40+
def strict?
41+
@strict
42+
end
2943

3044
# The name of the database and the local write concern options.
3145
attr_reader :name, :write_concern
@@ -272,20 +286,19 @@ def collections_info(coll_name=nil)
272286
# @return [Mongo::Collection]
273287
def create_collection(name, opts={})
274288
name = name.to_s
275-
if collection_names.include?(name)
276-
if strict?
277-
raise MongoDBError, "Collection #{name} already exists. " +
278-
"Currently in strict mode."
279-
else
280-
return Collection.new(name, self, opts)
281-
end
289+
if strict? && collection_names.include?(name)
290+
raise MongoDBError, "Collection '#{name}' already exists. (strict=true)"
282291
end
283292

284-
# Create a new collection.
285-
oh = BSON::OrderedHash.new
286-
oh[:create] = name
287-
doc = command(oh.merge(opts || {}))
288-
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
293+
begin
294+
cmd = BSON::OrderedHash.new
295+
cmd[:create] = name
296+
doc = command(cmd.merge(opts || {}))
297+
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
298+
rescue OperationFailure => e
299+
return Collection.new(name, self, :pk => @pk_factory) if e.message =~ /exists/
300+
raise e
301+
end
289302
raise MongoDBError, "Error creating collection: #{doc.inspect}"
290303
end
291304

@@ -300,8 +313,7 @@ def create_collection(name, opts={})
300313
# @return [Mongo::Collection]
301314
def collection(name, opts={})
302315
if strict? && !collection_names.include?(name.to_s)
303-
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " +
304-
"Currently in strict mode."
316+
raise MongoDBError, "Collection '#{name}' doesn't exist. (strict=true)"
305317
else
306318
opts = opts.dup
307319
opts.merge!(:pk => @pk_factory) unless opts[:pk]
@@ -316,9 +328,12 @@ def collection(name, opts={})
316328
#
317329
# @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
318330
def drop_collection(name)
319-
return true unless collection_names.include?(name.to_s)
320-
321-
ok?(command(:drop => name))
331+
return false if strict? && !collection_names.include?(name.to_s)
332+
begin
333+
ok?(command(:drop => name, :check_response => false))
334+
rescue OperationFailure => e
335+
false
336+
end
322337
end
323338

324339
# Run the getlasterror command with the specified replication options.

test/functional/db_api_test.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_find_simple
9292
# Can't compare _id values because at insert, an _id was added to @r1 by
9393
# the database but we don't know what it is without re-reading the record
9494
# (which is what we are doing right now).
95-
# assert_equal doc['_id'], @r1['_id']
95+
# assert_equal doc['_id'], @r1['_id']
9696
assert_equal doc['a'], @r1['a']
9797
end
9898

@@ -194,7 +194,7 @@ def test_find_sorting_with_hash
194194
@@coll.insert('a' => 2, 'b' => 1)
195195
@@coll.insert('a' => 3, 'b' => 2)
196196
@@coll.insert('a' => 4, 'b' => 1)
197-
197+
198198
oh = BSON::OrderedHash.new
199199
oh['a'] = -1
200200

@@ -312,7 +312,7 @@ def test_collection_options
312312

313313
begin
314314
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
315-
options = coll.options()
315+
options = coll.options
316316
assert_equal 'foobar', options['create']
317317
assert_equal true, options['capped']
318318
assert_equal 1024, options['size']
@@ -488,7 +488,7 @@ def test_strict_access_collection
488488
fail "expected exception"
489489
rescue => ex
490490
assert_equal Mongo::MongoDBError, ex.class
491-
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
491+
assert_equal "Collection 'does-not-exist' doesn't exist. (strict=true)", ex.to_s
492492
ensure
493493
@@db.strict = false
494494
@@db.drop_collection('does-not-exist')
@@ -500,8 +500,7 @@ def test_strict_create_collection
500500
@@db.strict = true
501501

502502
begin
503-
@@db.create_collection('foobar')
504-
assert true
503+
assert @@db.create_collection('foobar')
505504
rescue => ex
506505
fail "did not expect exception \"#{ex}\""
507506
end

test/unit/db_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class DBTest < Test::Unit::TestCase
8181
end
8282

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

0 commit comments

Comments
 (0)