Skip to content

Commit

Permalink
RUBY-3205 Deprecate collStats (#2738)
Browse files Browse the repository at this point in the history
* RUBY-3205 Deprecate collStats

* Consider createOptions in unified spec runner

* Update lib/mongo/collection.rb

Co-authored-by: Jamis Buck <[email protected]>

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
comandeo-mongo and jamis authored Jul 7, 2023
1 parent 5a6a8c0 commit eef4b20
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/reference/crud-operations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ part of the command:

.. code-block:: ruby

client.database.command(collstats: 'test', readConcern: {level: :majority})
client.database.command(dbStats: 1, readConcern: {level: :majority})


.. _read-preference:
Expand Down Expand Up @@ -504,7 +504,7 @@ option when a command is run on a database:
} )

# Set read preference for a given command
client.database.command( { collstats: 'test' }, read: { mode: secondary,
client.database.command( { dbStats: 1 }, read: { mode: secondary,
tag_sets: [ { 'dc' => 'nyc' } ] } )

Read preference can also be set for specific operations on a collection
Expand Down
4 changes: 3 additions & 1 deletion lib/mongo/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ def with(new_options)
#
# @since 2.0.0
def capped?
database.read_command(:collstats => name).documents[0][CAPPED]
database.list_collections(filter: { name: name })
.first
&.dig('options', CAPPED) || false
end

# Force the collection to be created in the database.
Expand Down
1 change: 0 additions & 1 deletion lib/mongo/operation/shared/sessions_supported.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module SessionsSupported

READ_COMMANDS = [
:aggregate,
:collStats,
:count,
:dbStats,
:distinct,
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/docs_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

# Start runCommand Example 2

client.database.command(collStats: 'restaurants')
client.database.command(dbStats: 1)

# End runCommand Example 2
end
Expand Down
12 changes: 8 additions & 4 deletions spec/mongo/collection_ddl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@
end

let(:collstats) do
database.read_command(:collstats => :specs).documents.first
collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first
end

let(:storage_stats) do
collstats.fetch('storageStats', {})
end

let(:options) do
Expand All @@ -157,9 +161,9 @@
end

it "applies the options" do
expect(collstats["capped"]).to be true
expect(collstats["max"]).to eq(512)
expect(collstats["maxSize"]).to eq(4096)
expect(storage_stats["capped"]).to be true
expect(storage_stats["max"]).to eq(512)
expect(storage_stats["maxSize"]).to eq(4096)
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions spec/mongo/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,11 @@
end

let(:collstats) do
database.read_command(:collstats => :specs).documents.first
collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first
end

let(:storage_stats) do
collstats.fetch('storageStats', {})
end

before do
Expand All @@ -664,9 +668,9 @@
end

it "applies the options" do
expect(collstats["capped"]).to be true
expect(collstats["max"]).to eq(512)
expect(collstats["maxSize"]).to eq(4096)
expect(storage_stats["capped"]).to be true
expect(storage_stats["max"]).to eq(512)
expect(storage_stats["maxSize"]).to eq(4096)
end
end

Expand Down
24 changes: 12 additions & 12 deletions spec/runners/unified/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,22 @@ def set_initial_data
collection = root_authorized_client.with(write_concern: {w: :majority}).
use(spec.use!('databaseName'))[spec.use!('collectionName')]
collection.drop
create_options = spec.use('createOptions') || {}
docs = spec.use!('documents')
if docs.any?
collection.insert_many(docs)
else
begin
collection.create
rescue Mongo::Error => e
if Mongo::Error::OperationFailure === e && (
begin
collection.create(create_options)
rescue Mongo::Error => e
if Mongo::Error::OperationFailure === e && (
e.code == 48 || e.message =~ /collection already exists/
)
# Already exists
else
raise
end
)
# Already exists
else
raise
end
end
if docs.any?
collection.insert_many(docs)
end
unless spec.empty?
raise NotImplementedError, "Unhandled spec keys: #{spec}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ tests:
command:
ping: 1
command_name: ping
- description: "current op is not bypassed"
- description: "kill op is not bypassed"
clientOptions:
autoEncryptOpts:
kmsProviders:
aws: {} # Credentials filled in from environment.
operations:
- name: runCommand
object: database
command_name: currentOp
command_name: killOp
arguments:
command:
currentOp: 1
killOp: 1
op: 1234
result:
errorContains: "command not supported for auto encryption: currentOp"
errorContains: "command not supported for auto encryption: killOp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
description: collectionData-createOptions
schemaVersion: "1.9"
runOnRequirements:
- minServerVersion: "3.6"
# Capped collections cannot be created on serverless instances.
serverless: forbid
createEntities:
- client:
id: &client0 client0
- database:
id: &database0 database0
client: *client0
databaseName: &database0Name database0
- collection:
id: &collection0 collection0
database: *database0
collectionName: &collection0Name coll0
initialData:
- collectionName: *collection0Name
databaseName: *database0Name
createOptions:
capped: true
# With MMAPv1, the size field cannot be less than 4096.
size: &cappedSize 4096
documents:
- { _id: 1, x: 11 }
tests:
- description: collection is created with the correct options
operations:
- object: *collection0
name: aggregate
arguments:
pipeline:
- $collStats: { storageStats: {} }
- $project: { capped: '$storageStats.capped', maxSize: '$storageStats.maxSize'}
expectResult:
- { capped: true, maxSize: *cappedSize }

0 comments on commit eef4b20

Please sign in to comment.