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

Standalone DSN parser #5814

Closed
derrabus opened this issue Nov 7, 2022 · 6 comments
Closed

Standalone DSN parser #5814

derrabus opened this issue Nov 7, 2022 · 6 comments

Comments

@derrabus
Copy link
Member

derrabus commented Nov 7, 2022

Feature Request

Q A
New Feature yes
RFC no

Summary

In #5697, we have deprecated driver aliases for database URLs. One use-case of those aliases was to be able to parse a credentials URL like mysql://my_user:[email protected] that are usually provided by a PaaS services. With the changes on 4.0, those database URLs would become specific to Doctrine because we would have to use mysqli:// or pdo-mysql:// as scheme.

My idea would be to deprecate the url option completely and replace it with a standalone DSN parser that translates a given DSN into an array of options that can be passed to DriverManager. This parser can be configured with a scheme-to-driver mapping (e.g. ['mysql' => 'pdo_mysql']).

I see several advantages of this approach:

  • The DBAL does not decide which driver to pick for a database engine, if multiple drivers exist.
  • The array can be modified before it's passed to DriverManager.
    • Adding settings that the DSN does not cover is simple and discoverable.
    • It is easier to see how settings from the DSN can be overridden.
  • The array can be cached by the application which means we don't need to parse the DSN on each request.
  • DriverManager's internal logic gets a little bit simpler.
@derrabus
Copy link
Member Author

derrabus commented Nov 7, 2022

@stof, I think your input can help on this issue.

@stof
Copy link
Member

stof commented Nov 7, 2022

  • The array can be cached by the application which means we don't need to parse the DSN on each request.

this is exactly the opposite of the needs of Symfony though. The whole point is to avoid caching the parsed credentials so that you don't need to clear the cache when credentials change (on most PaaS, clearing cache requires a new deployment, while credentials are injected as env variable in the container without rebuilding the image when they change).

However, having it as a separate class that parses the URL and merges that with an array of options (with the explicit options winning over the URL, at least when non-null) might solve it

@derrabus
Copy link
Member Author

derrabus commented Nov 7, 2022

I wrote "can be cached". Of course, you shouldn't do it if the DSN can change at any time without cache invalidation. Caching those values would be the responsibility of the application that configures the DBAL connection.

The idea is that the application contains bootstrapping logic like this:

$parameters = $dsnParser->parse($dsn);

// Post-process the parameters as you see fit!

$connection = DriverManager::getConnection($parameters);

In case of Symfony, that logic would reside in DoctrineBundle.

@derrabus
Copy link
Member Author

I've started working on this feature in #5843.

@derrabus derrabus removed this from the 3.6.0 milestone Dec 31, 2022
@derrabus
Copy link
Member Author

Fixed via #5843

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants