Skip to content

Commit 7dc20c4

Browse files
committed
Sanity check Manager association in BulkWriteCommandBuilder
1 parent 066c072 commit 7dc20c4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/BulkWriteCommandBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MongoDB\Codec\DocumentCodec;
66
use MongoDB\Codec\Encoder;
77
use MongoDB\Driver\BulkWriteCommand;
8+
use MongoDB\Driver\Manager;
89
use MongoDB\Exception\InvalidArgumentException;
910

1011
use function is_array;
@@ -15,6 +16,7 @@
1516
{
1617
private function __construct(
1718
public BulkWriteCommand $bulkWriteCommand,
19+
private Manager $manager,
1820
private string $namespace,
1921
private Encoder $builderEncoder,
2022
private ?DocumentCodec $codec,
@@ -43,6 +45,7 @@ public static function createWithCollection(Collection $collection, array $optio
4345

4446
return new self(
4547
new BulkWriteCommand($options),
48+
$collection->getManager(),
4649
$collection->getNamespace(),
4750
$collection->getBuilderEncoder(),
4851
$collection->getCodec(),
@@ -51,8 +54,18 @@ public static function createWithCollection(Collection $collection, array $optio
5154

5255
public function withCollection(Collection $collection): self
5356
{
57+
/* Prohibit mixing Collections associated with different Manager
58+
* objects. This is not technically necessary, since the Collection is
59+
* only used to derive a namespace and encoding options; however, it
60+
* may prevent a user from inadvertently mixing writes destined for
61+
* different deployments. */
62+
if ($this->manager !== $collection->getManager()) {
63+
throw new InvalidArgumentException('$collection is associated with a different MongoDB\Driver\Manager');
64+
}
65+
5466
return new self(
5567
$this->bulkWriteCommand,
68+
$this->manager,
5669
$collection->getNamespace(),
5770
$collection->getBuilderEncoder(),
5871
$collection->getCodec(),

0 commit comments

Comments
 (0)