Skip to content

Commit f808922

Browse files
committed
RUBY-945 Add #database_names and #database_info methods to client
1 parent 68d1726 commit f808922

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

lib/mongo/client.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,34 @@ def write_concern
223223
@write_concern ||= WriteConcern.get(options[:write])
224224
end
225225

226+
# Get the names of all databases.
227+
#
228+
# @example Get the database names.
229+
# client.database_names
230+
#
231+
# @return [ Array<String> ] The names of the databases.
232+
#
233+
# @since 2.0.5
234+
def database_names
235+
use(Database::ADMIN).command(
236+
listDatabases: 1
237+
).first['databases'].collect{ |info| info['name'] }
238+
end
239+
240+
# Get info for each database.
241+
#
242+
# @example Get the database info.
243+
# client.database_info
244+
#
245+
# @return [ Array<Hash ] The info for each database.
246+
#
247+
# @since 2.0.5
248+
def database_info
249+
Database.new(self, Database::ADMIN, options).command(
250+
listDatabases: 1
251+
).first['databases']
252+
end
253+
226254
private
227255

228256
def create_from_addresses(addresses, opts = {})

spec/mongo/client_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,21 @@
569569
end
570570
end
571571
end
572+
573+
describe '#database_names' do
574+
575+
it 'returns a list of database names' do
576+
expect(root_authorized_client.database_names).to include('admin', 'local', TEST_DB)
577+
end
578+
end
579+
580+
describe '#database_info' do
581+
582+
it 'returns a list of database info documents' do
583+
expect(
584+
root_authorized_client.database_info.collect do |i|
585+
i['name']
586+
end).to include('admin', 'local', TEST_DB)
587+
end
588+
end
572589
end

spec/support/authorization.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
Mongo::Auth::Roles::USER_ADMIN_ANY_DATABASE,
7878
Mongo::Auth::Roles::DATABASE_ADMIN_ANY_DATABASE,
7979
Mongo::Auth::Roles::READ_WRITE_ANY_DATABASE,
80-
Mongo::Auth::Roles::HOST_MANAGER
80+
Mongo::Auth::Roles::HOST_MANAGER,
81+
Mongo::Auth::Roles::CLUSTER_ADMIN
8182
]
8283
)
8384

@@ -105,8 +106,8 @@
105106
# @since 2.0.
106107
TEST_READ_WRITE_USER = Mongo::Auth::User.new(
107108
database: TEST_DB,
108-
user: 'test-user',
109-
password: 'password',
109+
user: TEST_USER.name,
110+
password: TEST_USER.password,
110111
roles: [ Mongo::Auth::Roles::READ_WRITE, Mongo::Auth::Roles::DATABASE_ADMIN ]
111112
)
112113

0 commit comments

Comments
 (0)