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 # Returns a hash containing database names as keys and disk space for
# each as values. # each as values.
def database_info def database_info
admin_db = nil doc = single_db_command('admin', :listDatabases => 1)
begin h = {}
admin_db = db('admin') doc['databases'].each { |db|
doc = admin_db.db_command(:listDatabases => 1) h[db['name']] = db['sizeOnDisk'].to_i
raise "error retrieving database info" unless admin_db.ok?(doc) }
h = {} h
doc['databases'].each { |db|
h[db['name']] = db['sizeOnDisk'].to_i
}
h
ensure
admin_db.close
end
end end
# Returns an array of database names. # Returns an array of database names.
@ -91,6 +84,28 @@ module XGen
raise "not implemented" raise "not implemented"
end 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 end
end end

View File

@ -29,4 +29,16 @@ class MongoTest < Test::Unit::TestCase
assert names.include?('admin') assert names.include?('admin')
end 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 end