Skip to content

Commit eef4b20

Browse files
RUBY-3205 Deprecate collStats (#2738)
* RUBY-3205 Deprecate collStats * Consider createOptions in unified spec runner * Update lib/mongo/collection.rb Co-authored-by: Jamis Buck <jamisbuck@gmail.com> --------- Co-authored-by: Jamis Buck <jamisbuck@gmail.com>
1 parent 5a6a8c0 commit eef4b20

File tree

9 files changed

+76
-29
lines changed

9 files changed

+76
-29
lines changed

docs/reference/crud-operations.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ part of the command:
458458

459459
.. code-block:: ruby
460460

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

463463

464464
.. _read-preference:
@@ -504,7 +504,7 @@ option when a command is run on a database:
504504
} )
505505

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

510510
Read preference can also be set for specific operations on a collection

lib/mongo/collection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ def with(new_options)
314314
#
315315
# @since 2.0.0
316316
def capped?
317-
database.read_command(:collstats => name).documents[0][CAPPED]
317+
database.list_collections(filter: { name: name })
318+
.first
319+
&.dig('options', CAPPED) || false
318320
end
319321

320322
# Force the collection to be created in the database.

lib/mongo/operation/shared/sessions_supported.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module SessionsSupported
3030

3131
READ_COMMANDS = [
3232
:aggregate,
33-
:collStats,
3433
:count,
3534
:dbStats,
3635
:distinct,

spec/integration/docs_examples_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162

163163
# Start runCommand Example 2
164164

165-
client.database.command(collStats: 'restaurants')
165+
client.database.command(dbStats: 1)
166166

167167
# End runCommand Example 2
168168
end

spec/mongo/collection_ddl_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@
137137
end
138138

139139
let(:collstats) do
140-
database.read_command(:collstats => :specs).documents.first
140+
collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first
141+
end
142+
143+
let(:storage_stats) do
144+
collstats.fetch('storageStats', {})
141145
end
142146

143147
let(:options) do
@@ -157,9 +161,9 @@
157161
end
158162

159163
it "applies the options" do
160-
expect(collstats["capped"]).to be true
161-
expect(collstats["max"]).to eq(512)
162-
expect(collstats["maxSize"]).to eq(4096)
164+
expect(storage_stats["capped"]).to be true
165+
expect(storage_stats["max"]).to eq(512)
166+
expect(storage_stats["maxSize"]).to eq(4096)
163167
end
164168
end
165169
end

spec/mongo/collection_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,11 @@
651651
end
652652

653653
let(:collstats) do
654-
database.read_command(:collstats => :specs).documents.first
654+
collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first
655+
end
656+
657+
let(:storage_stats) do
658+
collstats.fetch('storageStats', {})
655659
end
656660

657661
before do
@@ -664,9 +668,9 @@
664668
end
665669

666670
it "applies the options" do
667-
expect(collstats["capped"]).to be true
668-
expect(collstats["max"]).to eq(512)
669-
expect(collstats["maxSize"]).to eq(4096)
671+
expect(storage_stats["capped"]).to be true
672+
expect(storage_stats["max"]).to eq(512)
673+
expect(storage_stats["maxSize"]).to eq(4096)
670674
end
671675
end
672676

spec/runners/unified/test.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,22 +320,22 @@ def set_initial_data
320320
collection = root_authorized_client.with(write_concern: {w: :majority}).
321321
use(spec.use!('databaseName'))[spec.use!('collectionName')]
322322
collection.drop
323+
create_options = spec.use('createOptions') || {}
323324
docs = spec.use!('documents')
324-
if docs.any?
325-
collection.insert_many(docs)
326-
else
327-
begin
328-
collection.create
329-
rescue Mongo::Error => e
330-
if Mongo::Error::OperationFailure === e && (
325+
begin
326+
collection.create(create_options)
327+
rescue Mongo::Error => e
328+
if Mongo::Error::OperationFailure === e && (
331329
e.code == 48 || e.message =~ /collection already exists/
332-
)
333-
# Already exists
334-
else
335-
raise
336-
end
330+
)
331+
# Already exists
332+
else
333+
raise
337334
end
338335
end
336+
if docs.any?
337+
collection.insert_many(docs)
338+
end
339339
unless spec.empty?
340340
raise NotImplementedError, "Unhandled spec keys: #{spec}"
341341
end

spec/spec_tests/data/client_side_encryption/bypassedCommand.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ tests:
2626
command:
2727
ping: 1
2828
command_name: ping
29-
- description: "current op is not bypassed"
29+
- description: "kill op is not bypassed"
3030
clientOptions:
3131
autoEncryptOpts:
3232
kmsProviders:
3333
aws: {} # Credentials filled in from environment.
3434
operations:
3535
- name: runCommand
3636
object: database
37-
command_name: currentOp
37+
command_name: killOp
3838
arguments:
3939
command:
40-
currentOp: 1
40+
killOp: 1
41+
op: 1234
4142
result:
42-
errorContains: "command not supported for auto encryption: currentOp"
43+
errorContains: "command not supported for auto encryption: killOp"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
description: collectionData-createOptions
2+
schemaVersion: "1.9"
3+
runOnRequirements:
4+
- minServerVersion: "3.6"
5+
# Capped collections cannot be created on serverless instances.
6+
serverless: forbid
7+
createEntities:
8+
- client:
9+
id: &client0 client0
10+
- database:
11+
id: &database0 database0
12+
client: *client0
13+
databaseName: &database0Name database0
14+
- collection:
15+
id: &collection0 collection0
16+
database: *database0
17+
collectionName: &collection0Name coll0
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
createOptions:
22+
capped: true
23+
# With MMAPv1, the size field cannot be less than 4096.
24+
size: &cappedSize 4096
25+
documents:
26+
- { _id: 1, x: 11 }
27+
tests:
28+
- description: collection is created with the correct options
29+
operations:
30+
- object: *collection0
31+
name: aggregate
32+
arguments:
33+
pipeline:
34+
- $collStats: { storageStats: {} }
35+
- $project: { capped: '$storageStats.capped', maxSize: '$storageStats.maxSize'}
36+
expectResult:
37+
- { capped: true, maxSize: *cappedSize }

0 commit comments

Comments
 (0)