diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index e3a31ed..91d499c 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -333,9 +333,6 @@ module Mongo # Also note that it is permissible to create compound indexes that include a geospatial index as # long as the geospatial index comes first. # - # @param [Boolean] unique if true, this index will enforce a uniqueness constraint. DEPRECATED. Future - # versions of this driver will specify the uniqueness constraint using a hash param. - # # @option opts [Boolean] :unique (false) if true, this index will enforce a uniqueness constraint. # @option opts [Boolean] :background (false) indicate that the index should be built in the background. This # feature is only available in MongoDB >= 1.3.2. @@ -362,7 +359,7 @@ module Mongo # # @core indexes create_index-instance_method def create_index(spec, opts={}) - opts.assert_valid_keys(:min, :max, :background, :unique, :dropDups) if opts.is_a?(Hash) + opts.assert_valid_keys(:min, :max, :background, :unique, :dropDups) field_spec = OrderedHash.new if spec.is_a?(String) || spec.is_a?(Symbol) field_spec[spec.to_s] = 1 @@ -381,20 +378,17 @@ module Mongo end name = generate_index_name(field_spec) - if opts == true || opts == false - warn "For Collection#create_index, the method for specifying a unique index has changed." + - "Please pass :unique => true to the method instead." - end - sel = { + + selector = { :name => name, :ns => "#{@db.name}.#{@name}", - :key => field_spec, - :unique => (opts == true ? true : false) } - sel.merge!(opts) if opts.is_a?(Hash) + :key => field_spec + } + selector.merge!(opts) begin - response = insert_documents([sel], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true) + response = insert_documents([selector], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true) rescue Mongo::OperationFailure - raise Mongo::OperationFailure, "Failed to create index #{sel.inspect} with the following errors: #{response}" + raise Mongo::OperationFailure, "Failed to create index #{selector.inspect} with the following errors: #{response}" end name end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index e9a37f7..bec2b5c 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -404,8 +404,11 @@ module Mongo # @param [Boolean] unique if +true+, the created index will enforce a uniqueness constraint. # # @return [String] the name of the index created. + # + # @deprecated def create_index(collection_name, field_or_spec, unique=false) - self.collection(collection_name).create_index(field_or_spec, unique) + warn "DB#create_index is now deprecated. Please use Collection#create_index instead." + self.collection(collection_name).create_index(field_or_spec, :unique => unique) end # Return +true+ if the supplied +doc+ contains an 'ok' field with the value 1. diff --git a/test/collection_test.rb b/test/collection_test.rb index b18e0b8..f198ebd 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -165,7 +165,7 @@ class TestCollection < Test::Unit::TestCase end else def test_safe_update - @@test.create_index("x", true) + @@test.create_index("x", :unique => true) @@test.insert("x" => 5) @@test.insert("x" => 10) @@ -182,7 +182,7 @@ class TestCollection < Test::Unit::TestCase end def test_safe_save - @@test.create_index("hello", true) + @@test.create_index("hello", :unique => true) @@test.save("hello" => "world") @@test.save("hello" => "world") @@ -558,7 +558,7 @@ class TestCollection < Test::Unit::TestCase end should "create a unique index" do - @collection.create_index([['a', Mongo::ASCENDING]], true) + @collection.create_index([['a', Mongo::ASCENDING]], :unique => true) assert @collection.index_information['a_1']['unique'] == true end diff --git a/test/db_api_test.rb b/test/db_api_test.rb index b47be37..ef720a4 100644 --- a/test/db_api_test.rb +++ b/test/db_api_test.rb @@ -338,7 +338,7 @@ class DBAPITest < Test::Unit::TestCase @@db.drop_collection("blah") test = @@db.collection("blah") - test.create_index("hello", unique=true) + test.create_index("hello", :unique => true) test.insert("hello" => "world") test.insert("hello" => "mike") @@ -357,7 +357,7 @@ class DBAPITest < Test::Unit::TestCase @@db.drop_collection("blah") test = @@db.collection("blah") - test.create_index("hello.a", unique=true) + test.create_index("hello.a", :unique => true) test.insert("hello" => {"a" => 4, "b" => 5}) test.insert("hello" => {"a" => 7, "b" => 2}) diff --git a/test/threading/test_threading_large_pool.rb b/test/threading/test_threading_large_pool.rb index 3140a57..616a11a 100644 --- a/test/threading/test_threading_large_pool.rb +++ b/test/threading/test_threading_large_pool.rb @@ -19,7 +19,7 @@ class TestThreadingLargePool < Test::Unit::TestCase @duplicate.insert("test" => "update") @unique.insert("test" => "insert") @unique.insert("test" => "update") - @unique.create_index("test", true) + @unique.create_index("test", :unique => true) end def test_safe_update diff --git a/test/threading_test.rb b/test/threading_test.rb index 21a4cdf..42477cc 100644 --- a/test/threading_test.rb +++ b/test/threading_test.rb @@ -17,7 +17,7 @@ class TestThreading < Test::Unit::TestCase @duplicate.insert("test" => "update") @unique.insert("test" => "insert") @unique.insert("test" => "update") - @unique.create_index("test", true) + @unique.create_index("test", :unique => true) end def test_safe_update