Skip to content

Commit 34bc0cd

Browse files
authored
Merge pull request #31 from utopia-php/exclude-read-concern-commands
Exclude read concern commands
2 parents ecfad6a + 5206ea9 commit 34bc0cd

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Client.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Client
6666
public const COMMAND_END_SESSIONS = "endSessions";
6767
public const COMMAND_LIST_INDEXES = "listIndexes";
6868
public const COMMAND_COLLMOD = "collMod";
69-
69+
public const COMMAND_KILL_CURSORS = "killCursors";
7070
// Connection and performance settings
7171
private int $defaultMaxTimeMS = 30000; // 30 seconds default
7272

@@ -97,6 +97,14 @@ class Client
9797
public const READ_PREFERENCE_SECONDARY_PREFERRED = 'secondaryPreferred';
9898
public const READ_PREFERENCE_NEAREST = 'nearest';
9999

100+
/**
101+
* Commands that do not support readConcern options
102+
*/
103+
private array $readConcernNotSupportedCommands = [
104+
self::COMMAND_GET_MORE,
105+
self::COMMAND_KILL_CURSORS
106+
];
107+
100108

101109
/**
102110
* Authentication for connection
@@ -320,7 +328,15 @@ public function query(array $command, ?string $db = null): stdClass|array|int
320328

321329
// CRITICAL: Remove readConcern from any non-first operation in a transaction
322330
// MongoDB will reject commands with readConcern that have txnNumber but not startTransaction
323-
if (isset($command['txnNumber']) && !isset($command['startTransaction']) && isset($command['readConcern'])) {
331+
// Or if the command is in the readConcernNotSupportedCommands array
332+
if (
333+
(
334+
isset($command['txnNumber'])
335+
&& !isset($command['startTransaction'])
336+
&& isset($command['readConcern'])
337+
)
338+
|| \in_array(array_key_first($command) ?? '', $this->readConcernNotSupportedCommands)
339+
) {
324340
unset($command['readConcern']);
325341
}
326342

tests/TransactionTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ public function testTransactionStateManagement()
162162
// Verify final state
163163
$finalState = $client->getSessionState($session);
164164
$this->assertEquals('committed', $finalState['state']);
165-
166165
} finally {
167166
$client->endSessions([$session]);
168167
}
@@ -202,7 +201,6 @@ public function testTransactionAbort()
202201
// Verify document was not inserted (transaction rolled back)
203202
$found = $client->find('test_collection', ['name' => 'abort_test']);
204203
$this->assertEmpty($found->cursor->firstBatch);
205-
206204
} finally {
207205
$client->endSessions([$session]);
208206
}
@@ -251,7 +249,6 @@ public function testWithTransactionHelper()
251249

252250
$this->assertNotEmpty($found1->cursor->firstBatch);
253251
$this->assertNotEmpty($found2->cursor->firstBatch);
254-
255252
} finally {
256253
$client->endSessions([$session]);
257254
}
@@ -343,7 +340,6 @@ public function testCRUDWithSessionAndConcerns()
343340
$this->assertEquals(1, $count);
344341

345342
$client->commitTransaction($session);
346-
347343
} finally {
348344
$client->endSessions([$session]);
349345
}

0 commit comments

Comments
 (0)