Skip to content

Commit f1ab8b7

Browse files
committed
Add 'comment' to search index commands
1 parent b9c3b78 commit f1ab8b7

File tree

6 files changed

+86
-41
lines changed

6 files changed

+86
-41
lines changed

psalm-baseline.xml

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
<code>($value is NativeType ? BSONType : $value)</code>
8686
</MixedInferredReturnType>
8787
</file>
88+
<file src="src/Collection.php">
89+
<ArgumentTypeCoercion occurrences="1">
90+
<code>$options</code>
91+
</ArgumentTypeCoercion>
92+
</file>
8893
<file src="src/Command/ListCollections.php">
8994
<MixedAssignment occurrences="2">
9095
<code>$cmd[$option]</code>
@@ -359,8 +364,7 @@
359364
</DocblockTypeContradiction>
360365
</file>
361366
<file src="src/Operation/Aggregate.php">
362-
<MixedArgument occurrences="2">
363-
<code>$this-&gt;options['typeMap']</code>
367+
<MixedArgument occurrences="1">
364368
<code>$this-&gt;options['typeMap']</code>
365369
</MixedArgument>
366370
<MixedAssignment occurrences="5">
@@ -511,6 +515,9 @@
511515
<MixedArgumentTypeCoercion occurrences="1">
512516
<code>(array) $index</code>
513517
</MixedArgumentTypeCoercion>
518+
<MixedAssignment occurrences="1">
519+
<code>$cmd[$option]</code>
520+
</MixedAssignment>
514521
</file>
515522
<file src="src/Operation/DatabaseCommand.php">
516523
<MixedArgument occurrences="1">
@@ -592,6 +599,11 @@
592599
<code>isInTransaction</code>
593600
</MixedMethodCall>
594601
</file>
602+
<file src="src/Operation/DropSearchIndex.php">
603+
<MixedAssignment occurrences="1">
604+
<code>$cmd[$option]</code>
605+
</MixedAssignment>
606+
</file>
595607
<file src="src/Operation/Explain.php">
596608
<MixedArgument occurrences="1">
597609
<code>$this-&gt;options['typeMap']</code>
@@ -690,17 +702,17 @@
690702
<code>isInTransaction</code>
691703
</MixedMethodCall>
692704
</file>
693-
<file src="src/Operation/ListCollectionNames.php">
694-
<MissingClosureReturnType occurrences="1">
695-
<code>function (array $collectionInfo) {</code>
696-
</MissingClosureReturnType>
697-
</file>
698705
<file src="src/Operation/ListIndexes.php">
699706
<MixedAssignment occurrences="2">
700707
<code>$cmd[$option]</code>
701708
<code>$options['session']</code>
702709
</MixedAssignment>
703710
</file>
711+
<file src="src/Operation/ListSearchIndexes.php">
712+
<DocblockTypeContradiction occurrences="1">
713+
<code>isset($options['name']) &amp;&amp; ! is_string($options['name'])</code>
714+
</DocblockTypeContradiction>
715+
</file>
704716
<file src="src/Operation/MapReduce.php">
705717
<DocblockTypeContradiction occurrences="1">
706718
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>
@@ -776,6 +788,11 @@
776788
<code>! is_array($update) &amp;&amp; ! is_object($update)</code>
777789
</DocblockTypeContradiction>
778790
</file>
791+
<file src="src/Operation/UpdateSearchIndex.php">
792+
<MixedAssignment occurrences="1">
793+
<code>$cmd[$option]</code>
794+
</MixedAssignment>
795+
</file>
779796
<file src="src/Operation/Watch.php">
780797
<DocblockTypeContradiction occurrences="1">
781798
<code>isset($resumeToken) &amp;&amp; ! is_array($resumeToken) &amp;&amp; ! is_object($resumeToken)</code>
@@ -797,14 +814,9 @@
797814
<code>$this-&gt;changeStreamOptions['resumeAfter']</code>
798815
<code>$this-&gt;changeStreamOptions['startAfter']</code>
799816
</MixedReturnStatement>
800-
<PropertyNotSetInConstructor occurrences="2">
817+
<PropertyNotSetInConstructor occurrences="1">
801818
<code>$firstBatchSize</code>
802-
<code>$operationTime</code>
803819
</PropertyNotSetInConstructor>
804-
<RedundantConditionGivenDocblockType occurrences="2">
805-
<code>$resumeToken === null &amp;&amp; $this-&gt;operationTime !== null</code>
806-
<code>$this-&gt;operationTime !== null</code>
807-
</RedundantConditionGivenDocblockType>
808820
<UnusedFunctionCall occurrences="2">
809821
<code>addSubscriber</code>
810822
<code>removeSubscriber</code>

src/Collection.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ public function createIndexes(array $indexes, array $options = [])
389389
*
390390
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
391391
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
392-
* @param array|object $definition Atlas Search index mapping definition
393-
* @param array{name?: string} $options Command options
392+
* @param array|object $definition Atlas Search index mapping definition
393+
* @param array{name?: string, comment?: mixed} $options Command options
394394
* @return string The name of the created search index
395395
* @throws UnsupportedException if options are not supported by the selected server
396396
* @throws InvalidArgumentException for parameter/option parsing errors
@@ -427,15 +427,15 @@ public function createSearchIndex($definition, array $options = []): string
427427
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
428428
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
429429
* @param list<array{name?: string, definition: array|object}> $indexes List of search index specifications
430-
* @param array $options Command options
430+
* @param array{comment?: string} $options Command options
431431
* @return string[] The names of the created search indexes
432432
* @throws UnsupportedException if options are not supported by the selected server
433433
* @throws InvalidArgumentException for parameter/option parsing errors
434434
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
435435
*/
436436
public function createSearchIndexes(array $indexes, array $options = []): array
437437
{
438-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
438+
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
439439
$server = select_server($this->manager, $options);
440440

441441
return $operation->execute($server);
@@ -622,8 +622,8 @@ public function dropIndexes(array $options = [])
622622
* Drop a single Atlas Search index in the collection.
623623
* Only available when used against a 7.0+ Atlas cluster.
624624
*
625-
* @param string $name Search index name
626-
* @param array $options Additional options
625+
* @param string $name Search index name
626+
* @param array{comment?: mixed} $options Additional options
627627
* @throws UnsupportedException if options are not supported by the selected server
628628
* @throws InvalidArgumentException for parameter/option parsing errors
629629
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
@@ -1192,16 +1192,16 @@ public function updateOne($filter, $update, array $options = [])
11921192
* Update a single Atlas Search index in the collection.
11931193
* Only available when used against a 7.0+ Atlas cluster.
11941194
*
1195-
* @param string $name Search index name
1196-
* @param array|object $definition Atlas Search index definition
1197-
* @param array $options Command options
1195+
* @param string $name Search index name
1196+
* @param array|object $definition Atlas Search index definition
1197+
* @param array{comment?: mixed} $options Command options
11981198
* @throws UnsupportedException if options are not supported by the selected server
11991199
* @throws InvalidArgumentException for parameter parsing errors
12001200
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
12011201
*/
12021202
public function updateSearchIndex(string $name, $definition, array $options = []): void
12031203
{
1204-
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition);
1204+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
12051205
$server = select_server($this->manager, $options);
12061206

12071207
$operation->execute($server);

src/Operation/CreateSearchIndexes.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ class CreateSearchIndexes implements Executable
4848
/** @var array */
4949
private $indexes = [];
5050

51+
/** @var array */
52+
private $options;
53+
5154
/**
5255
* Constructs a createSearchIndexes command.
5356
*
54-
* @param string $databaseName Database name
55-
* @param string $collectionName Collection name
56-
* @param list<array|object> $indexes List of search index specifications
57+
* @param string $databaseName Database name
58+
* @param string $collectionName Collection name
59+
* @param list<array|object> $indexes List of search index specifications
60+
* @param array{comment?: mixed} $options Command options
5761
* @throws InvalidArgumentException for parameter parsing errors
5862
*/
59-
public function __construct(string $databaseName, string $collectionName, array $indexes)
63+
public function __construct(string $databaseName, string $collectionName, array $indexes, array $options)
6064
{
6165
if (! array_is_list($indexes)) {
6266
throw new InvalidArgumentException('$indexes is not a list');
@@ -72,6 +76,7 @@ public function __construct(string $databaseName, string $collectionName, array
7276

7377
$this->databaseName = $databaseName;
7478
$this->collectionName = $collectionName;
79+
$this->options = $options;
7580
}
7681

7782
/**
@@ -89,6 +94,12 @@ public function execute(Server $server): array
8994
'indexes' => $this->indexes,
9095
];
9196

97+
foreach (['comment'] as $option) {
98+
if (isset($this->options[$option])) {
99+
$cmd[$option] = $this->options[$option];
100+
}
101+
}
102+
92103
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
93104

94105
/** @var object{indexesCreated: list<object{name: string}>} $result */

src/Operation/DropSearchIndex.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ class DropSearchIndex implements Executable
4040
/** @var string */
4141
private $name;
4242

43+
/** @var array */
44+
private $options;
45+
4346
/**
4447
* Constructs a dropSearchIndex command.
4548
*
46-
* @param string $databaseName Database name
47-
* @param string $collectionName Collection name
48-
* @param string $name Index name
49+
* @param string $databaseName Database name
50+
* @param string $collectionName Collection name
51+
* @param string $name Index name
52+
* @param array{comment?: mixed} $options Command options
4953
* @throws InvalidArgumentException for parameter parsing errors
5054
*/
51-
public function __construct(string $databaseName, string $collectionName, string $name)
55+
public function __construct(string $databaseName, string $collectionName, string $name, array $options = [])
5256
{
5357
if ($name === '') {
5458
throw new InvalidArgumentException('Index name cannot be empty');
@@ -57,6 +61,7 @@ public function __construct(string $databaseName, string $collectionName, string
5761
$this->databaseName = $databaseName;
5862
$this->collectionName = $collectionName;
5963
$this->name = $name;
64+
$this->options = $options;
6065
}
6166

6267
/**
@@ -73,6 +78,12 @@ public function execute(Server $server): void
7378
'name' => $this->name,
7479
];
7580

81+
foreach (['comment'] as $option) {
82+
if (isset($this->options[$option])) {
83+
$cmd[$option] = $this->options[$option];
84+
}
85+
}
86+
7687
$server->executeCommand($this->databaseName, new Command($cmd));
7788
}
7889
}

src/Operation/UpdateSearchIndex.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ class UpdateSearchIndex implements Executable
4545
/** @var array|object */
4646
private $definition;
4747

48+
/** @var array */
49+
private $options = [];
50+
4851
/**
4952
* Constructs a createSearchIndexes command.
5053
*
51-
* @param string $databaseName Database name
52-
* @param string $collectionName Collection name
53-
* @param string $name Search index name
54-
* @param array|object $definition Atlas Search index definition
54+
* @param string $databaseName Database name
55+
* @param string $collectionName Collection name
56+
* @param string $name Search index name
57+
* @param array|object $definition Atlas Search index definition
58+
* @param array{comment?: mixed} $options Command options
5559
* @throws InvalidArgumentException for parameter parsing errors
5660
*/
57-
public function __construct(string $databaseName, string $collectionName, string $name, $definition)
61+
public function __construct(string $databaseName, string $collectionName, string $name, $definition, array $options = [])
5862
{
5963
if ($name === '') {
6064
throw new InvalidArgumentException('Index name cannot be empty');
@@ -68,6 +72,7 @@ public function __construct(string $databaseName, string $collectionName, string
6872
$this->collectionName = $collectionName;
6973
$this->name = $name;
7074
$this->definition = $definition;
75+
$this->options = $options;
7176
}
7277

7378
/**
@@ -85,6 +90,12 @@ public function execute(Server $server): void
8590
'definition' => $this->definition,
8691
];
8792

93+
foreach (['comment'] as $option) {
94+
if (isset($this->options[$option])) {
95+
$cmd[$option] = $this->options[$option];
96+
}
97+
}
98+
8899
$server->executeCommand($this->databaseName, new Command($cmd));
89100
}
90101
}

tests/Collection/SearchIndexFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public function testIndexLifecycle(): void
5555
$name = 'search_index';
5656

5757
// Create a search index
58-
$createdName = $collection->createSearchIndex(['mappings' => ['dynamic' => true]], ['name' => $name]);
58+
$createdName = $collection->createSearchIndex(['mappings' => ['dynamic' => true]], ['name' => $name, 'comment' => 'Index creation test']);
5959
$this->assertSame($name, $createdName);
6060

6161
// Wait for the index to be ready
6262
$maxWaitingTime = 120;
6363
do {
6464
sleep(1);
65-
$result = $collection->listSearchIndexes($name);
65+
$result = $collection->listSearchIndexes(['name' => $name]);
6666
$this->assertInstanceOf(CachingIterator::class, $result);
6767
$index = $result->current();
6868

@@ -74,16 +74,16 @@ public function testIndexLifecycle(): void
7474
// Update the search index
7575
$collection->updateSearchIndex($name, [
7676
'mappings' => ['dynamic' => false],
77-
]);
77+
], ['comment' => 'Index update test']);
7878

7979
// Delete the search index
80-
$collection->dropSearchIndex($name);
80+
$collection->dropSearchIndex($name, ['comment' => 'Index deletion test']);
8181

8282
// Wait for the index to be deleted
8383
$maxWaitingTime = 90;
8484
do {
8585
sleep(1);
86-
$result = $collection->listSearchIndexes($name);
86+
$result = $collection->listSearchIndexes(['name' => $name]);
8787
$this->assertInstanceOf(CachingIterator::class, $result);
8888

8989
if ($maxWaitingTime-- <= 0) {

0 commit comments

Comments
 (0)