Skip to content

Commit 60871a3

Browse files
committed
Merge pull request #632 from estolfo/RUBY-945-database-names
RUBY-945 Add #database_names and #list_databases methods to Client and #list_collections method to Database
2 parents 68d1726 + c4e0c46 commit 60871a3

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

lib/mongo/client.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,32 @@ 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+
list_databases.collect{ |info| info['name'] }
236+
end
237+
238+
# Get info for each database.
239+
#
240+
# @example Get the info for each database.
241+
# client.list_databases
242+
#
243+
# @return [ Array<Hash> ] The info for each database.
244+
#
245+
# @since 2.0.5
246+
def list_databases
247+
use(Database::ADMIN).command(
248+
listDatabases: 1
249+
).first['databases']
250+
end
251+
226252
private
227253

228254
def create_from_addresses(addresses, opts = {})

lib/mongo/database.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ def collection_names(options = {})
102102
View.new(self).collection_names(options)
103103
end
104104

105+
# Get info on all the collections in the database.
106+
#
107+
# @example Get info on each collection.
108+
# database.list_collections
109+
#
110+
# @return [ Array<Hash> ] Info for each collection in the database.
111+
#
112+
# @since 2.0.5
113+
def list_collections
114+
View.new(self).list_collections
115+
end
116+
105117
# Get all the collections that belong to this database.
106118
#
107119
# @example Get all the collections.

lib/mongo/database/view.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def collection_names(options = {})
5757
end
5858
end
5959

60+
# Get info on all the collections in the database.
61+
#
62+
# @example Get info on each collection.
63+
# database.list_collections
64+
#
65+
# @return [ Array<Hash> ] Info for each collection in the database.
66+
#
67+
# @since 2.0.5
68+
def list_collections
69+
collections_info(next_primary)
70+
end
71+
6072
# Create the new database view.
6173
#
6274
# @example Create the new database view.

spec/mongo/client_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,23 @@
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(
577+
'admin', 'local', TEST_DB
578+
)
579+
end
580+
end
581+
582+
describe '#list_databases' do
583+
584+
it 'returns a list of database info documents' do
585+
expect(
586+
root_authorized_client.list_databases.collect do |i|
587+
i['name']
588+
end).to include('admin', 'local', TEST_DB)
589+
end
590+
end
572591
end

spec/mongo/database_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@
9595
end
9696
end
9797

98+
describe '#list_collections' do
99+
100+
let(:database) do
101+
described_class.new(authorized_client, TEST_DB)
102+
end
103+
104+
let(:result) do
105+
database.list_collections.map do |info|
106+
info['name']
107+
end
108+
end
109+
110+
before do
111+
database[:users].create
112+
end
113+
114+
after do
115+
database[:users].drop
116+
end
117+
118+
it 'returns a list of the collections info', if: write_command_enabled? do
119+
expect(result).to include('users')
120+
end
121+
122+
it 'returns a list of the collections info', unless: write_command_enabled? do
123+
expect(result).to include("#{TEST_DB}.users")
124+
end
125+
end
126+
98127
describe '#collections' do
99128

100129
context 'when the database exists' do

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)