Added Mongo#drop_database.

This commit is contained in:
Jim Menard 2009-01-20 09:21:19 -05:00
parent 62502a3f24
commit bf2e35589f
2 changed files with 40 additions and 13 deletions

View File

@ -61,19 +61,12 @@ module XGen
# Returns a hash containing database names as keys and disk space for
# each as values.
def database_info
admin_db = nil
begin
admin_db = db('admin')
doc = admin_db.db_command(:listDatabases => 1)
raise "error retrieving database info" unless admin_db.ok?(doc)
h = {}
doc['databases'].each { |db|
h[db['name']] = db['sizeOnDisk'].to_i
}
h
ensure
admin_db.close
end
doc = single_db_command('admin', :listDatabases => 1)
h = {}
doc['databases'].each { |db|
h[db['name']] = db['sizeOnDisk'].to_i
}
h
end
# Returns an array of database names.
@ -91,6 +84,28 @@ module XGen
raise "not implemented"
end
# Drops the database +name+.
def drop_database(name)
single_db_command(name, :dropDatabase => 1)
end
protected
# Send cmd (a hash, possibly ordered) to the admin database and return
# the answer. Raises an error unless the return is "ok" (DB#ok?
# returns +true+).
def single_db_command(db_name, cmd)
db = nil
begin
db = db(db_name)
doc = db.db_command(cmd)
raise "error retrieving database info" unless db.ok?(doc)
doc
ensure
db.close
end
end
end
end
end

View File

@ -29,4 +29,16 @@ class MongoTest < Test::Unit::TestCase
assert names.include?('admin')
end
def test_drop_database
db = @mongo.db('will-be-deleted')
coll = db.collection('temp')
coll.clear
coll.insert(:name => 'temp')
assert_equal 1, coll.count()
assert @mongo.database_names.include?('will-be-deleted')
@mongo.drop_database('will-be-deleted')
assert !@mongo.database_names.include?('will-be-deleted')
end
end