Skip to content
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

PHPLIB-1518 WriteResult::get*Count() always return an int on acknowledged result #1454

Merged
merged 3 commits into from
Sep 26, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 25, 2024

Fix PHPLIB-1518

I chose to rely on the driver exception thrown by mongodb/mongo-php-driver#1687

@GromNaN GromNaN requested a review from a team as a code owner September 25, 2024 09:16
@GromNaN GromNaN changed the title PHPLIB-1518 WriteResult::get*Count() always return an int on acknowledged result PHPLIB-1518 WriteResult::get*Count() always return an int on acknowledged result Sep 25, 2024
src/UpdateResult.php Dismissed Show dismissed Hide dismissed
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

LGTM, but we may want to consider unacknowledged writes in rename.

Comment on lines +586 to +588
// If the update resulted in no modification, it's possible that the
// file did not exist, in which case we must raise an error.
if ($updateResult->getMatchedCount() !== 1) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should consider using unacknowledged writes a valid use case, but in case we do there's a functional change here. If we want to preserve the option to use unacknowledged writes with GridFS, we should check isAcknowledged instead of relying on the null value from getMatchedCount:

Suggested change
// If the update resulted in no modification, it's possible that the
// file did not exist, in which case we must raise an error.
if ($updateResult->getMatchedCount() !== 1) {
/* If the update resulted in no modification, it's possible that the
* file did not exist, in which case we must raise an error. Checking
* the write result's matched count will be most efficient, but fall
* back to a findOne operation if necessary (i.e. legacy writes).
*/
$found = $updateResult->isAcknowledged()
? $updateResult->getMatchedCount() === 1
: $this->collectionWrapper->findFileById($id) !== null;
if (! $found) {

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case, that's a fix to backport to v1.x, as it already throws an exception when there is an unacknowledged operation.

Copy link
Member

Choose a reason for hiding this comment

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

In that case, let's defer to a separate PR, as this does PR not change existing behaviour.

Copy link
Member

Choose a reason for hiding this comment

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

let's defer to a separate PR

I assume a ticket needs to be created for this. Please cross-reference it here after doing so.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just realized that this second if ($updateResult->getMatchedCount() !== 1) is always true after if ($updateResult->getModifiedCount() === 1) return;.

Do you know what are the "legacy writes" that are mentioned in the comment?

Copy link
Member

Choose a reason for hiding this comment

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

Legacy opcodes (e.g. OP_UPDATE), which were removed in MongoDB 6.0. At this point, drivers may still use them for unacknowledged writes but all acknowledged writes are guaranteed to use OP_MSG and the corresponding write commands (e.g. update).

@GromNaN
Copy link
Member Author

GromNaN commented Sep 26, 2024

I did not update the UPGRADE file, there is nothing to upgrade.

@GromNaN GromNaN merged commit ea818c8 into mongodb:v2.x Sep 26, 2024
31 checks passed
alcaeus added a commit that referenced this pull request Sep 30, 2024
* v2.x:
  PHPLIB-1518 `WriteResult::get*Count()` always return an int on acknowledged result (#1454)
  PHPLIB-954: Add return types to all methods (#1391)
  PHPLIB-797: Remove unused methods in UnsupportedException (#1436)
  Revert "Add final annotations to non-internal Operation classes (#1410)"
  PHPLIB-953 Make internal classes and Operation classes final (#1392)
  PHPLIB-1218 Remove deprecated fields from GridFS files (#1398)
@GromNaN GromNaN deleted the PHPLIB-1518 branch September 30, 2024 13:48
alcaeus added a commit that referenced this pull request Oct 16, 2024
* v2.x:
  PHPLIB-1546 and PHPLIB-1159: Remove CreateCollection flags and autoIndexId options (#1478)
  PHPLIB-1227 Use void return types for operations without meaningful result document (#1468)
  Remove deprecated functionality (#1439)
  PHPLIB-1518 `WriteResult::get*Count()` always return an int on acknowledged result (#1454)
  PHPLIB-954: Add return types to all methods (#1391)
  PHPLIB-797: Remove unused methods in UnsupportedException (#1436)
  Revert "Add final annotations to non-internal Operation classes (#1410)"
  PHPLIB-953 Make internal classes and Operation classes final (#1392)
  PHPLIB-1218 Remove deprecated fields from GridFS files (#1398)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants