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

Support write command operations #180

Closed
wants to merge 2 commits into from
Closed

Support write command operations #180

wants to merge 2 commits into from

Conversation

qRoC
Copy link

@qRoC qRoC commented Jul 4, 2017

@alcaeus
Copy link
Owner

alcaeus commented Jul 6, 2017

I'd hold off on this pull request until the upstream issue has been dealt with. A fix there would mean no changes required here.

Also, this pull request is missing tests.

@qRoC
Copy link
Author

qRoC commented Jul 6, 2017

Steps to reproduce: mongodb/mongo-php-library#384
This fix is used in a real environment and are no errors, but maybe not all cases are covered.

@@ -405,7 +405,12 @@ public function command(array $data, $options = [], &$hash = null)
{
try {
$cursor = new \MongoCommandCursor($this->connection, $this->name, $data);
$cursor->setReadPreference($this->getReadPreference());

if (!empty($data['new']) || !empty($data['upsert'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that there are many more commands that write than findAndModify, which it looks like this is attempting to detect.

Also, command detection would be better implemented by checking the first key in the command document (i.e. $data argument), as that must indicate the command name. That would allow most write commands to be detected, excluding aggregate and mapReduce where you would need to inspect additional options to determine if they are directing output to a collection.

Copy link
Author

Choose a reason for hiding this comment

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

Note that there are many more commands that write than findAndModify, which it looks like this is attempting to detect.

Yes, because of this I look values of keys "new" and "upsert". So it works for all commands.

Copy link
Author

Choose a reason for hiding this comment

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

This is a bad decision, but it works.

Copy link
Contributor

@jmikola jmikola Jul 6, 2017

Choose a reason for hiding this comment

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

My point was that commands should not be detected by the presence of options. As-is, this approach had a few complications:

  • It picks up any command that might include a new or upsert key at the top level. Even if no other command uses these options, it assumes no future command will.
  • The new and upsert options for findAndModity are both boolean options. Using empty() here means that the condition is only satisfied if either option is true; however, they could both be false.
  • Some variations of findAndModify do not even use these options (e.g. find and delete).

I don't meant to discourage you with the above criticism. Rather, I want to stress the importance of using a robust solution in library code (be it Doctrine MongoDB, the adapter, or the new extension's PHP library).

Copy link
Author

Choose a reason for hiding this comment

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

I don't meant to discourage you with the above criticism. Rather, I want to stress the importance of using a robust solution in library code (be it Doctrine MongoDB, the adapter, or the new extension's PHP library).

I now, its fast fix, no more.

So, maybe allow to set manually?

if ($options['readPreference']) {
                $cursor->setReadPreference($options['readPreference']);
            } else {
                $cursor->setReadPreference($this->getReadPreference());
            }

Copy link
Contributor

Choose a reason for hiding this comment

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

#181 and doctrine/mongodb#297 should address the issue. The adapter was missing some behavior that forced a primary server for most commands, which was undocumented behavior in the legacy mongo extension. On a separate note, Doctrine MongoDB needed a patch to be able to apply a read preference to findAndModify and mapReduce commands executed through its query builder.

@alcaeus alcaeus added invalid and removed needs test labels Jul 6, 2017
@alcaeus
Copy link
Owner

alcaeus commented Jul 6, 2017

As per mongodb/mongo-php-library#384 (comment), this is an upstream issue that will be fixed in doctrine/mongodb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants