Skip to content

PHPLIB-1320: Support "comment" command option in Collection::createIndex() #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ public function countDocuments($filter = [], array $options = [])
*/
public function createIndex($key, array $options = [])
{
$commandOptionKeys = ['commitQuorum' => 1, 'maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
$indexOptions = array_diff_key($options, $commandOptionKeys);
$commandOptions = array_intersect_key($options, $commandOptionKeys);
$operationOptionKeys = ['comment' => 1, 'commitQuorum' => 1, 'maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
$indexOptions = array_diff_key($options, $operationOptionKeys);
$operationOptions = array_intersect_key($options, $operationOptionKeys);

return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
return current($this->createIndexes([['key' => $key] + $indexOptions], $operationOptions));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@ public function testAggregateWithinTransaction(): void

public function testCreateIndexSplitsCommandOptions(): void
{
$this->skipIfServerVersion('<', '4.4', 'commitQuorum and comment options are not supported');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that this removes test coverage for older versions for splitting, but I don't think it makes any difference. Fine to leave as is.


if ($this->isStandalone()) {
$this->markTestSkipped('commitQuorum is not supported');
}

(new CommandObserver())->observe(
function (): void {
$this->collection->createIndex(
['x' => 1],
[
'comment' => 'foo',
'commitQuorum' => 'majority',
'maxTimeMS' => 10000,
'session' => $this->manager->startSession(),
'sparse' => true,
Expand All @@ -147,6 +155,8 @@ function (): void {
},
function (array $event): void {
$command = $event['started']->getCommand();
$this->assertObjectHasAttribute('comment', $command);
$this->assertObjectHasAttribute('commitQuorum', $command);
$this->assertObjectHasAttribute('lsid', $command);
$this->assertObjectHasAttribute('maxTimeMS', $command);
$this->assertObjectHasAttribute('writeConcern', $command);
Expand Down