Skip to content

Commit d0c1669

Browse files
committed
RUBY-945 Add #database_names method to client
1 parent 68d1726 commit d0c1669

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/mongo/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ 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+
Database.new(self, Database::ADMIN, options).command(
236+
listDatabases: 1
237+
).first['databases'].collect { |info| info['name'] }
238+
end
239+
226240
private
227241

228242
def create_from_addresses(addresses, opts = {})

spec/mongo/client_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,11 @@
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')
577+
end
578+
end
572579
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)