Skip to content

[HttpFoundation][Lock] Makes MongoDB adapters usable with ext-mongodb directly #52336

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
Nov 2, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Oct 27, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

mongodb/mongodb is complex to handle for libraries with optional support of MongoDB, as it requires ext-mongodb. In order to reduce complexity for maintainers, I reimplemented the session and lock adapters to use only the C driver classes.

Some features of MongoDB\Client are missing (server selection, session, transaction). But they are not necessary to store Sessions and Lock.

Changes:

  • Lock & Session accept a MongoDB\Driver\Manager
  • The lock uses exclusively UTC date. Before, there was a mix of time() and UTCDatetime objects.
  • Session tests require a mongo server.
  • mongodb/mongodb not needed in the CI

And of course also allows developers to use this adapters without installing mongodb/mongodb if they want, with the same features as before.

@nicolas-grekas
Copy link
Member

Some test failures are related in the integration job apparently.

@GromNaN
Copy link
Member Author

GromNaN commented Oct 29, 2023

Some test failures are related in the integration job apparently.

Thanks. I fixed the tests, the CI uses an old version of the ext-mongodb. But that's fine, we don't set version constraint.

@@ -142,6 +149,10 @@ public function __construct(Collection|Client|string $mongo, array $options = []
*/
private function skimUri(string $uri): string
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving this here: note that as per the Connection Strings documentation, the path in the connection string refers to the default authentication database to be used if the authSource does not define one. While most people consider this to be the default database, I wanted to point out the difference. Not necessarily an issue of this PR as it's already the current behaviour, so feel free to resolve this comment after reading if you think this isn't worth changing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that would be better if the option database had precedence over the database in the path of the URI.

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 authSource should never be assumed to be a database name. For some auth mechanisms it might be set to $external (see: authSource docs).

@GromNaN GromNaN force-pushed the ext-mongodb2 branch 7 times, most recently from ff8e9ed to d30e3a9 Compare October 31, 2023 14:18
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM for 6.4, I like that this makes the CI simpler !

@nicolas-grekas nicolas-grekas added the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label Oct 31, 2023
@GromNaN GromNaN force-pushed the ext-mongodb2 branch 2 times, most recently from dbefd97 to a2d9ef1 Compare October 31, 2023 15:05
Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Thanks for the review @stof. I removed clock related code.

if (null === $dbData) {
$this->options['expiry_field'] => ['$gte' => $this->getUTCDateTime()],
], [
'projection' => [
Copy link
Contributor

Choose a reason for hiding this comment

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

Does anything enforce that $this->options['id_field'] is actually _id or even a field with a unique index?

I assume this will only ever return one or no documents, but I noticed you use limit: 1 for the delete operation in doDestroy(), but omit the limit here. I don't disagree with an explicit limit on the delete operation, since the default is to remove all matching documents.

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 guess this options were copied from PdoSessionHandler when this provider was created. This provider has a configureSchema or createTable methods to init the table. We can add a similar method for MongoDB. But since the collection already have an unique index on _id, I think it's fine to let people that would like to change the id field, initialize the collection themselve.

@@ -142,6 +149,10 @@ public function __construct(Collection|Client|string $mongo, array $options = []
*/
private function skimUri(string $uri): string
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 authSource should never be assumed to be a database name. For some auth mechanisms it might be set to $external (see: authSource docs).

);

return $this->collection;
return $this->manager ??= new Manager($this->uri, $this->options['uriOptions'], $this->options['driverOptions']);
Copy link
Contributor

Choose a reason for hiding this comment

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

Offhand, is there anything in these Symfony adapters that sets driver information like we do for Doctrine ODM and Laravel? If so, should we consider doing so in a subsequent PR? /cc @alcaeus

Copy link
Member Author

Choose a reason for hiding this comment

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

The connection will mostly come from Doctrine, but I can add user-agent for standalone usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense if you're constructing the Manager yourself, but no need to hold up this PR to add that if you want to address it down the line.

@GromNaN GromNaN force-pushed the ext-mongodb2 branch 2 times, most recently from 5e567e8 to a3478ca Compare November 2, 2023 20:12
@GromNaN GromNaN force-pushed the ext-mongodb2 branch 2 times, most recently from 7ba9819 to 264d3a2 Compare November 2, 2023 20:17
@fabpot
Copy link
Member

fabpot commented Nov 2, 2023

Thank you @GromNaN.

@fabpot fabpot merged commit 8a69f67 into symfony:6.4 Nov 2, 2023
@GromNaN GromNaN deleted the ext-mongodb2 branch November 2, 2023 20:46
@fabpot fabpot mentioned this pull request Nov 10, 2023
@GromNaN GromNaN changed the title [HttpFoundation][Lock] Makes MongoDB adapters usable with ext-mongodb only [HttpFoundation][Lock] Makes MongoDB adapters usable with ext-mongodb directly Nov 10, 2023
@fabpot fabpot mentioned this pull request Nov 10, 2023
@xabbuh xabbuh mentioned this pull request Nov 25, 2023
xabbuh added a commit that referenced this pull request Nov 25, 2023
This PR was merged into the 6.4 branch.

Discussion
----------

fix GitHub workflows

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

this was forgotten in #52336

Commits
-------

a4e82c9 fix GitHub workflows
nicolas-grekas added a commit that referenced this pull request Oct 25, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

[Lock] Ensure compatibility with ext-mongodb v2

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Same as #58619 for branch 6.4 that was reworked by #52336

Commits
-------

cd3b778 Ensure compatibility with ext-mongodb v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature HttpFoundation Lock ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants