Skip to content

Commit 37bbdd5

Browse files
jmikolaGromNaNalcaeusCopilot
authored
PHPLIB-847, PHPLIB-1627: BulkWriteCommand API (#1630)
* BulkWriteCommandBuilder * Test against mongodb/mongo-php-driver#1790 * Psalm stubs for PHPC BulkWriteCommand classes * BulkWriteCommandBuilder::withCollection returns a new instance * Sanity check Manager association in BulkWriteCommandBuilder * Client::bulkWrite() and ClientBulkWrite operation * Spec tests for Client::bulkWrite() * Revise error messages for readConcern and writeConcern in transactions The transaction spec requires certain language, and this is now expected in spec test for clientBulkWrite. * BulkWriteCommandBuilder is final * Re-order BulkWriteCommandBuilder methods to satisfy PedantryTest * Ignore order of non-public constructors in PedantryTest * Fix preparation of unacknowledged BulkWriteCommandResults * Skip CSFLE namedKMS tests that require schema 1.18 * Test Collection::getBuilderEncoder() and getCodec() Also fixes the return type for getBuilderEncoder() * Default BulkWriteCommandBuilder options to empty arrays This is actually required for the union assignment in createWithCollection(). The nullable arrays were copied from the extension, but are inconsistent with other PHPLIB APIs. * CRUD prose tests 3 and 4 * Update Psalm stubs for PHPC BulkWriteCommand API * CRUD prose tests 5-9 * Rename BulkWriteCommandBuilder to ClientBulkWrite Also renames the operation class to ClientBulkWriteCommand to avoid aliasing in Client. * Server::executeBulkWriteCommand() always returns a BulkWriteCommandResult * Use dropCollection() helper to ensure collections are cleaned up * Prose test 11 * Prose test 12 * Prose test 13 * Prose test 15 * Validate assigned $options property instead of ctor arg * Fix Psalm errors and update baseline * phpcs fixes * Test against v2.x * Fix CS * Remove readonly qualifier on the class, PHP 8.2 would be required * Add NoDiscard attribute to 'withCollection' methods * Switch all EXTENSION_BRANCH versions * Always build against v2.x * Append CRYPT_SHARED_LIB_PATH to autoEncryptionOpts in tests * PHPLIB-1677: Assert unset BulkWriteException.partialResult in CRUD prose tests * Use explicit null check Co-authored-by: Copilot <[email protected]> * Require ext-mongodb 2.1 --------- Co-authored-by: Jérôme Tamarelle <[email protected]> Co-authored-by: Andreas Braun <[email protected]> Co-authored-by: Andreas Braun <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent cca7233 commit 37bbdd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1794
-68
lines changed

.evergreen/config/generated/build/build-extension.yml

Lines changed: 36 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.evergreen/config/templates/build/build-extension.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
vars:
66
PHP_VERSION: "%phpVersion%"
77
- func: "compile extension"
8+
# TODO: Remove vars to switch to latest stable version when 2.1.0 is releeased
9+
vars:
10+
EXTENSION_BRANCH: "v2.x"
811
- func: "upload extension"
912
- name: "build-php-%phpVersion%-lowest"
1013
tags: ["build", "php%phpVersion%", "lowest", "pr", "tag"]
@@ -14,7 +17,9 @@
1417
PHP_VERSION: "%phpVersion%"
1518
- func: "compile extension"
1619
vars:
17-
EXTENSION_VERSION: "2.0.0"
20+
# TODO: Switch to 2.1.0 when it is released
21+
# EXTENSION_VERSION: "2.0.0"
22+
EXTENSION_BRANCH: "v2.x"
1823
- func: "upload extension"
1924
- name: "build-php-%phpVersion%-next-stable"
2025
tags: ["build", "php%phpVersion%", "next-stable", "pr", "tag"]
@@ -24,7 +29,9 @@
2429
PHP_VERSION: "%phpVersion%"
2530
- func: "compile extension"
2631
vars:
27-
EXTENSION_BRANCH: "v2.0"
32+
# TODO: Switch to v2.1 when 2.1.0 is released
33+
# EXTENSION_VERSION: "v2.1"
34+
EXTENSION_BRANCH: "v2.x"
2835
- func: "upload extension"
2936
- name: "build-php-%phpVersion%-next-minor"
3037
tags: ["build", "php%phpVersion%", "next-minor"]

.github/workflows/coding-standards.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ on:
1313

1414
env:
1515
PHP_VERSION: "8.2"
16-
DRIVER_VERSION: "stable"
16+
# TODO: change to "stable" once 2.0.0 is released
17+
# DRIVER_VERSION: "stable"
18+
DRIVER_VERSION: "mongodb/[email protected]"
1719

1820
jobs:
1921
phpcs:

.github/workflows/generator.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ on:
1313

1414
env:
1515
PHP_VERSION: "8.2"
16-
DRIVER_VERSION: "stable"
16+
# TODO: change to "stable" once 2.0.0 is released
17+
# DRIVER_VERSION: "stable"
18+
DRIVER_VERSION: "mongodb/[email protected]"
1719

1820
jobs:
1921
diff:

.github/workflows/static-analysis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ on:
1919

2020
env:
2121
PHP_VERSION: "8.2"
22-
DRIVER_VERSION: "stable"
22+
# TODO: change to "stable" once 2.0.0 is released
23+
# DRIVER_VERSION: "stable"
24+
DRIVER_VERSION: "mongodb/[email protected]"
2325

2426
jobs:
2527
psalm:

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ on:
1212
- "feature/*"
1313

1414
env:
15-
DRIVER_VERSION: "stable"
15+
# TODO: change to "stable" once 2.0.0 is released
16+
# DRIVER_VERSION: "stable"
17+
DRIVER_VERSION: "mongodb/[email protected]"
1618

1719
jobs:
1820
phpunit:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
],
1212
"require": {
1313
"php": "^8.1",
14-
"ext-mongodb": "^2.0",
14+
"ext-mongodb": "^2.1",
1515
"composer-runtime-api": "^2.0",
16-
"psr/log": "^1.1.4|^2|^3"
16+
"psr/log": "^1.1.4|^2|^3",
17+
"symfony/polyfill-php85": "^1.32"
1718
},
1819
"require-dev": {
1920
"doctrine/coding-standard": "^12.0",

psalm-baseline.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@
235235
<code><![CDATA[$pipeline]]></code>
236236
</PossiblyInvalidArgument>
237237
</file>
238+
<file src="src/ClientBulkWrite.php">
239+
<PossiblyInvalidArgument>
240+
<code><![CDATA[$document]]></code>
241+
<code><![CDATA[$replacement]]></code>
242+
</PossiblyInvalidArgument>
243+
</file>
238244
<file src="src/Codec/EncodeIfSupported.php">
239245
<ArgumentTypeCoercion>
240246
<code><![CDATA[$value]]></code>
@@ -545,6 +551,14 @@
545551
<code><![CDATA[$args[2]]]></code>
546552
</MixedOperand>
547553
</file>
554+
<file src="src/Operation/ClientBulkWriteCommand.php">
555+
<MixedMethodCall>
556+
<code><![CDATA[isInTransaction]]></code>
557+
</MixedMethodCall>
558+
<MixedReturnStatement>
559+
<code><![CDATA[$server->executeBulkWriteCommand($this->bulkWriteCommand, $options)]]></code>
560+
</MixedReturnStatement>
561+
</file>
548562
<file src="src/Operation/Count.php">
549563
<MixedAssignment>
550564
<code><![CDATA[$cmd[$option]]]></code>

psalm.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
</projectFiles>
2121

2222
<stubs>
23+
<file name="stubs/Driver/BulkWriteCommand.stub.php"/>
24+
<file name="stubs/Driver/BulkWriteCommandException.stub.php"/>
25+
<file name="stubs/Driver/BulkWriteCommandResult.stub.php"/>
2326
<file name="stubs/Driver/Cursor.stub.php"/>
2427
<file name="stubs/Driver/CursorInterface.stub.php"/>
2528
<file name="stubs/Driver/WriteResult.stub.php"/>

src/Client.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MongoDB\Builder\BuilderEncoder;
2525
use MongoDB\Builder\Pipeline;
2626
use MongoDB\Codec\Encoder;
27+
use MongoDB\Driver\BulkWriteCommand;
28+
use MongoDB\Driver\BulkWriteCommandResult;
2729
use MongoDB\Driver\ClientEncryption;
2830
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2931
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
@@ -39,6 +41,7 @@
3941
use MongoDB\Model\BSONArray;
4042
use MongoDB\Model\BSONDocument;
4143
use MongoDB\Model\DatabaseInfo;
44+
use MongoDB\Operation\ClientBulkWriteCommand;
4245
use MongoDB\Operation\DropDatabase;
4346
use MongoDB\Operation\ListDatabaseNames;
4447
use MongoDB\Operation\ListDatabases;
@@ -189,6 +192,32 @@ final public function addSubscriber(Subscriber $subscriber): void
189192
$this->manager->addSubscriber($subscriber);
190193
}
191194

195+
/**
196+
* Executes multiple write operations across multiple namespaces.
197+
*
198+
* @param BulkWriteCommand|ClientBulkWrite $bulk Assembled bulk write command or builder
199+
* @param array $options Additional options
200+
* @throws UnsupportedException if options are unsupported on the selected server
201+
* @throws InvalidArgumentException for parameter/option parsing errors
202+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
203+
* @see ClientBulkWriteCommand::__construct() for supported options
204+
*/
205+
public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options = []): BulkWriteCommandResult
206+
{
207+
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
208+
$options['writeConcern'] = $this->writeConcern;
209+
}
210+
211+
if ($bulk instanceof ClientBulkWrite) {
212+
$bulk = $bulk->bulkWriteCommand;
213+
}
214+
215+
$operation = new ClientBulkWriteCommand($bulk, $options);
216+
$server = select_server_for_write($this->manager, $options);
217+
218+
return $operation->execute($server);
219+
}
220+
192221
/**
193222
* Returns a ClientEncryption instance for explicit encryption and decryption
194223
*

0 commit comments

Comments
 (0)