Skip to content

RUBY-945 Add #database_names and #list_databases methods to Client and #list_collections method to Database #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/mongo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ def write_concern
@write_concern ||= WriteConcern.get(options[:write])
end

# Get the names of all databases.
#
# @example Get the database names.
# client.database_names
#
# @return [ Array<String> ] The names of the databases.
#
# @since 2.0.5
def database_names
list_databases.collect{ |info| info['name'] }
end

# Get info for each database.
#
# @example Get the info for each database.
# client.list_databases
#
# @return [ Array<Hash> ] The info for each database.
#
# @since 2.0.5
def list_databases
use(Database::ADMIN).command(
listDatabases: 1
).first['databases']
end

private

def create_from_addresses(addresses, opts = {})
Expand Down
12 changes: 12 additions & 0 deletions lib/mongo/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def collection_names(options = {})
View.new(self).collection_names(options)
end

# Get info on all the collections in the database.
#
# @example Get info on each collection.
# database.list_collections
#
# @return [ Array<Hash> ] Info for each collection in the database.
#
# @since 2.0.5
def list_collections
View.new(self).list_collections
end

# Get all the collections that belong to this database.
#
# @example Get all the collections.
Expand Down
12 changes: 12 additions & 0 deletions lib/mongo/database/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def collection_names(options = {})
end
end

# Get info on all the collections in the database.
#
# @example Get info on each collection.
# database.list_collections
#
# @return [ Array<Hash> ] Info for each collection in the database.
#
# @since 2.0.5
def list_collections
collections_info(next_primary)
end

# Create the new database view.
#
# @example Create the new database view.
Expand Down
19 changes: 19 additions & 0 deletions spec/mongo/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,23 @@
end
end
end

describe '#database_names' do

it 'returns a list of database names' do
expect(root_authorized_client.database_names).to include(
'admin', 'local', TEST_DB
)
end
end

describe '#list_databases' do

it 'returns a list of database info documents' do
expect(
root_authorized_client.list_databases.collect do |i|
i['name']
end).to include('admin', 'local', TEST_DB)
end
end
end
29 changes: 29 additions & 0 deletions spec/mongo/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@
end
end

describe '#list_collections' do

let(:database) do
described_class.new(authorized_client, TEST_DB)
end

let(:result) do
database.list_collections.map do |info|
info['name']
end
end

before do
database[:users].create
end

after do
database[:users].drop
end

it 'returns a list of the collections info', if: write_command_enabled? do
expect(result).to include('users')
end

it 'returns a list of the collections info', unless: write_command_enabled? do
expect(result).to include("#{TEST_DB}.users")
end
end

describe '#collections' do

context 'when the database exists' do
Expand Down
7 changes: 4 additions & 3 deletions spec/support/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
Mongo::Auth::Roles::USER_ADMIN_ANY_DATABASE,
Mongo::Auth::Roles::DATABASE_ADMIN_ANY_DATABASE,
Mongo::Auth::Roles::READ_WRITE_ANY_DATABASE,
Mongo::Auth::Roles::HOST_MANAGER
Mongo::Auth::Roles::HOST_MANAGER,
Mongo::Auth::Roles::CLUSTER_ADMIN
]
)

Expand Down Expand Up @@ -105,8 +106,8 @@
# @since 2.0.
TEST_READ_WRITE_USER = Mongo::Auth::User.new(
database: TEST_DB,
user: 'test-user',
password: 'password',
user: TEST_USER.name,
password: TEST_USER.password,
roles: [ Mongo::Auth::Roles::READ_WRITE, Mongo::Auth::Roles::DATABASE_ADMIN ]
)

Expand Down