Skip to content

Commit ceaf580

Browse files
committed
Add 'comment' to search index commands
1 parent 1627399 commit ceaf580

File tree

6 files changed

+84
-28
lines changed

6 files changed

+84
-28
lines changed

psalm-baseline.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<code>$mergedDriver['platform']</code>
2323
</MixedAssignment>
2424
</file>
25+
<file src="src/Collection.php">
26+
<ArgumentTypeCoercion occurrences="1">
27+
<code>$options</code>
28+
</ArgumentTypeCoercion>
29+
</file>
2530
<file src="src/Command/ListCollections.php">
2631
<MixedAssignment occurrences="2">
2732
<code>$cmd[$option]</code>
@@ -446,6 +451,9 @@
446451
<MixedArgumentTypeCoercion occurrences="1">
447452
<code>(array) $index</code>
448453
</MixedArgumentTypeCoercion>
454+
<MixedAssignment occurrences="1">
455+
<code>$cmd[$option]</code>
456+
</MixedAssignment>
449457
</file>
450458
<file src="src/Operation/DatabaseCommand.php">
451459
<MixedArgument occurrences="1">
@@ -527,6 +535,11 @@
527535
<code>isInTransaction</code>
528536
</MixedMethodCall>
529537
</file>
538+
<file src="src/Operation/DropSearchIndex.php">
539+
<MixedAssignment occurrences="1">
540+
<code>$cmd[$option]</code>
541+
</MixedAssignment>
542+
</file>
530543
<file src="src/Operation/Explain.php">
531544
<MixedArgument occurrences="1">
532545
<code>$this-&gt;options['typeMap']</code>
@@ -636,6 +649,11 @@
636649
<code>$options['session']</code>
637650
</MixedAssignment>
638651
</file>
652+
<file src="src/Operation/ListSearchIndexes.php">
653+
<DocblockTypeContradiction occurrences="1">
654+
<code>isset($options['name']) &amp;&amp; ! is_string($options['name'])</code>
655+
</DocblockTypeContradiction>
656+
</file>
639657
<file src="src/Operation/MapReduce.php">
640658
<DocblockTypeContradiction occurrences="1">
641659
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>
@@ -711,6 +729,11 @@
711729
<code>! is_array($update) &amp;&amp; ! is_object($update)</code>
712730
</DocblockTypeContradiction>
713731
</file>
732+
<file src="src/Operation/UpdateSearchIndex.php">
733+
<MixedAssignment occurrences="1">
734+
<code>$cmd[$option]</code>
735+
</MixedAssignment>
736+
</file>
714737
<file src="src/Operation/Watch.php">
715738
<DocblockTypeContradiction occurrences="1">
716739
<code>isset($resumeToken) &amp;&amp; ! is_array($resumeToken) &amp;&amp; ! is_object($resumeToken)</code>

src/Collection.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ public function createIndexes(array $indexes, array $options = [])
402402
*
403403
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
404404
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
405-
* @param array|object $definition Atlas Search index mapping definition
406-
* @param array{name?: string} $options Command options
405+
* @param array|object $definition Atlas Search index mapping definition
406+
* @param array{name?: string, comment?: mixed} $options Command options
407407
* @return string The name of the created search index
408408
* @throws UnsupportedException if options are not supported by the selected server
409409
* @throws InvalidArgumentException for parameter/option parsing errors
@@ -440,15 +440,15 @@ public function createSearchIndex($definition, array $options = []): string
440440
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
441441
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
442442
* @param list<array{name?: string, definition: array|object}> $indexes List of search index specifications
443-
* @param array $options Command options
443+
* @param array{comment?: string} $options Command options
444444
* @return string[] The names of the created search indexes
445445
* @throws UnsupportedException if options are not supported by the selected server
446446
* @throws InvalidArgumentException for parameter/option parsing errors
447447
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
448448
*/
449449
public function createSearchIndexes(array $indexes, array $options = []): array
450450
{
451-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
451+
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
452452
$server = select_server($this->manager, $options);
453453

454454
return $operation->execute($server);
@@ -635,8 +635,8 @@ public function dropIndexes(array $options = [])
635635
* Drop a single Atlas Search index in the collection.
636636
* Only available when used against a 7.0+ Atlas cluster.
637637
*
638-
* @param string $name Search index name
639-
* @param array $options Additional options
638+
* @param string $name Search index name
639+
* @param array{comment?: mixed} $options Additional options
640640
* @throws UnsupportedException if options are not supported by the selected server
641641
* @throws InvalidArgumentException for parameter/option parsing errors
642642
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
@@ -1205,16 +1205,16 @@ public function updateOne($filter, $update, array $options = [])
12051205
* Update a single Atlas Search index in the collection.
12061206
* Only available when used against a 7.0+ Atlas cluster.
12071207
*
1208-
* @param string $name Search index name
1209-
* @param array|object $definition Atlas Search index definition
1210-
* @param array $options Command options
1208+
* @param string $name Search index name
1209+
* @param array|object $definition Atlas Search index definition
1210+
* @param array{comment?: mixed} $options Command options
12111211
* @throws UnsupportedException if options are not supported by the selected server
12121212
* @throws InvalidArgumentException for parameter parsing errors
12131213
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
12141214
*/
12151215
public function updateSearchIndex(string $name, $definition, array $options = []): void
12161216
{
1217-
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition);
1217+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
12181218
$server = select_server($this->manager, $options);
12191219

12201220
$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)