add support for unique index creation
This commit is contained in:
parent
a2898effb4
commit
1312c70677
|
@ -117,8 +117,10 @@ module XGen
|
|||
# should be either a single field name or a Array of [field name,
|
||||
# direction] pairs. Directions should be specified as
|
||||
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING.
|
||||
def create_index(field_or_spec)
|
||||
@db.create_index(@name, field_or_spec)
|
||||
# +unique+ is an optional boolean indicating whether this index
|
||||
# should enforce a uniqueness constraint.
|
||||
def create_index(field_or_spec, unique=false)
|
||||
@db.create_index(@name, field_or_spec, unique)
|
||||
end
|
||||
|
||||
# Drop index +name+.
|
||||
|
|
|
@ -430,8 +430,9 @@ module XGen
|
|||
# should be either a single field name or a Array of [field name,
|
||||
# direction] pairs. Directions should be specified as
|
||||
# XGen::Mongo::ASCENDING or XGen::Mongo::DESCENDING. Normally called
|
||||
# by Collection#create_index.
|
||||
def create_index(collection_name, field_or_spec)
|
||||
# by Collection#create_index. If +unique+ is true the index will
|
||||
# enforce a uniqueness constraint.
|
||||
def create_index(collection_name, field_or_spec, unique=false)
|
||||
field_h = OrderedHash.new
|
||||
if field_or_spec.is_a? String
|
||||
field_h[field_or_spec] = 1
|
||||
|
@ -442,7 +443,8 @@ module XGen
|
|||
sel = {
|
||||
:name => name,
|
||||
:ns => full_coll_name(collection_name),
|
||||
:key => field_h
|
||||
:key => field_h,
|
||||
:unique => unique
|
||||
}
|
||||
@semaphore.synchronize {
|
||||
send_to_db(InsertMessage.new(@name, SYSTEM_INDEX_COLLECTION, sel))
|
||||
|
|
|
@ -309,6 +309,28 @@ class DBAPITest < Test::Unit::TestCase
|
|||
@@db.drop_index(@@coll.name, name)
|
||||
end
|
||||
|
||||
def test_unique_index
|
||||
@@db.drop_collection("blah")
|
||||
test = @@db.collection("blah")
|
||||
test.create_index("hello")
|
||||
|
||||
test.insert("hello" => "world")
|
||||
test.insert("hello" => "mike")
|
||||
test.insert("hello" => "world")
|
||||
assert !@@db.error?
|
||||
|
||||
@@db.drop_collection("blah")
|
||||
test = @@db.collection("blah")
|
||||
test.create_index("hello", unique=true)
|
||||
|
||||
test.insert("hello" => "world")
|
||||
test.insert("hello" => "mike")
|
||||
test.insert("hello" => "world")
|
||||
assert @@db.error?
|
||||
|
||||
@@db.drop_collection("blah")
|
||||
end
|
||||
|
||||
def test_array
|
||||
@@coll << {'b' => [1, 2, 3]}
|
||||
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
||||
|
|
Loading…
Reference in New Issue