Skip to content

Commit f1f0c87

Browse files
committed
minor: fixes for db commands with new DB#command checking response
1 parent e9de3e5 commit f1f0c87

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/mongo/db.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def last_status
326326
#
327327
# @return [Boolean]
328328
def error?
329-
error != nil
329+
get_last_error['err'] != nil
330330
end
331331

332332
# Get the most recent error to have occured on this database.
@@ -379,10 +379,9 @@ def eval(code, *args)
379379

380380
oh = BSON::OrderedHash.new
381381
oh[:$eval] = code
382-
oh[:args] = args
382+
oh[:args] = args
383383
doc = command(oh)
384-
return doc['retval'] if ok?(doc)
385-
raise OperationFailure, "eval failed: #{doc['errmsg']}"
384+
doc['retval']
386385
end
387386

388387
# Rename a collection.
@@ -397,7 +396,7 @@ def rename_collection(from, to)
397396
oh = BSON::OrderedHash.new
398397
oh[:renameCollection] = "#{@name}.#{from}"
399398
oh[:to] = "#{@name}.#{to}"
400-
doc = DB.new('admin', @connection).command(oh)
399+
doc = DB.new('admin', @connection).command(oh, :check_response => false)
401400
ok?(doc) || raise(MongoDBError, "Error renaming collection: #{doc.inspect}")
402401
end
403402

@@ -434,7 +433,6 @@ def index_information(collection_name)
434433
info
435434
end
436435

437-
438436
# Return stats on this database. Uses MongoDB's dbstats command.
439437
#
440438
# @return [Hash]
@@ -530,7 +528,7 @@ def pk_factory=(pk_factory)
530528
def profiling_level
531529
oh = BSON::OrderedHash.new
532530
oh[:profile] = -1
533-
doc = command(oh)
531+
doc = command(oh, :check_response => false)
534532
raise "Error with profile command: #{doc.inspect}" unless ok?(doc) && doc['was'].kind_of?(Numeric)
535533
case doc['was'].to_i
536534
when 0
@@ -560,7 +558,7 @@ def profiling_level=(level)
560558
else
561559
raise "Error: illegal profiling level value #{level}"
562560
end
563-
doc = command(oh)
561+
doc = command(oh, :check_response => false)
564562
ok?(doc) || raise(MongoDBError, "Error with profile command: #{doc.inspect}")
565563
end
566564

@@ -580,7 +578,7 @@ def profiling_info
580578
# @raise [MongoDBError] if the command fails or there's a problem with the validation
581579
# data, or if the collection is invalid.
582580
def validate_collection(name)
583-
doc = command(:validate => name)
581+
doc = command({:validate => name}, :check_response => false)
584582
raise MongoDBError, "Error with validate command: #{doc.inspect}" unless ok?(doc)
585583
result = doc['result']
586584
raise MongoDBError, "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)

test/db_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
$:.unshift '.'
12
require 'test/test_helper'
23
require 'digest/md5'
34
require 'stringio'
@@ -238,6 +239,11 @@ def test_stored_function_management
238239
end
239240
end
240241

242+
def test_eval
243+
@@db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
244+
assert_equal 'hello', @@db['system'].find_one['_id']
245+
end
246+
241247
if @@version >= "1.3.5"
242248
def test_db_stats
243249
stats = @@db.stats

0 commit comments

Comments
 (0)