-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-2518: Support sort option for updateOne and replaceOne #1794
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--TEST-- | ||
MongoDB\Driver\BulkWrite::update() with sort option | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
<?php skip_if_not_live(); ?> | ||
<?php skip_if_server_version('<', '8.0'); ?> | ||
<?php skip_if_not_clean(); ?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber | ||
{ | ||
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void | ||
{ | ||
if ($event->getCommandName() !== 'update') { | ||
return; | ||
} | ||
|
||
printf("update included sort: %s\n", json_encode($event->getCommand()->updates[0]->sort)); | ||
} | ||
|
||
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void | ||
{ | ||
} | ||
|
||
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void | ||
{ | ||
} | ||
} | ||
|
||
$manager = create_test_manager(); | ||
|
||
$bulk = new MongoDB\Driver\BulkWrite(); | ||
$bulk->insert(['_id' => 1]); | ||
$bulk->insert(['_id' => 2]); | ||
$bulk->insert(['_id' => 3]); | ||
$manager->executeBulkWrite(NS, $bulk); | ||
|
||
MongoDB\Driver\Monitoring\addSubscriber(new CommandLogger); | ||
|
||
$bulk = new MongoDB\Driver\BulkWrite; | ||
$bulk->update(['_id' => ['$gt' => 1]], ['$set' => ['x' => 11]], ['sort' => ['_id' => 1]]); | ||
$manager->executeBulkWrite(NS, $bulk); | ||
|
||
$bulk = new MongoDB\Driver\BulkWrite; | ||
$bulk->update(['_id' => ['$gt' => 1]], ['x' => 22], ['sort' => ['_id' => -1]]); | ||
$manager->executeBulkWrite(NS, $bulk); | ||
|
||
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([])); | ||
|
||
var_dump($cursor->toArray()); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
update included sort: {"_id":1} | ||
update included sort: {"_id":-1} | ||
array(3) { | ||
[0]=> | ||
object(stdClass)#%d (%d) { | ||
["_id"]=> | ||
int(1) | ||
} | ||
[1]=> | ||
object(stdClass)#%d (%d) { | ||
["_id"]=> | ||
int(2) | ||
["x"]=> | ||
int(11) | ||
} | ||
[2]=> | ||
object(stdClass)#%d (%d) { | ||
["_id"]=> | ||
int(3) | ||
["x"]=> | ||
int(22) | ||
} | ||
} | ||
===DONE=== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
MongoDB\Driver\BulkWrite::update() with multi:true prohibits sort option | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$manager = create_test_manager(); | ||
$bulk = new MongoDB\Driver\BulkWrite; | ||
|
||
echo throws(function() use ($manager, $bulk) { | ||
$bulk->update(['x' => ['$gt' => 1]], ['$set' => ['y' => 11]], ['multi' => true, 'sort' => ['x' => 1]]); | ||
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
OK: Got MongoDB\Driver\Exception\InvalidArgumentException | ||
Invalid option 'sort' | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
===DONE=== |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to test
replaceOne
here instead? This other method is in the spec commit. Otherwise I don't get the benefit of this 2nd update.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually testing the code path for
mongoc_bulk_operation_replace_one_with_opts
. PHPC's original bulk write API only provides a singleBulkWrite::update()
method, and we branch according to the structure of the second argument andmulti
option.See: https://github.com/mongodb/mongo-php-driver/blob/1.20.1/src/MongoDB/BulkWrite.c#L452
This isn't relevant to the upcoming API for the bulkWrite command, which will have specific methods in PHPC.