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

[DoctrineBridge] Allow to use a middleware instead of DbalLogger #45491

Merged
merged 1 commit into from
Mar 25, 2022

Conversation

l-vo
Copy link
Contributor

@l-vo l-vo commented Feb 20, 2022

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Step toward the fix of doctrine/DoctrineBundle#1431 (mentioned too in #44313 and #44495)
License MIT
Doc PR

The SqlLogger that is used in doctrine bridge and doctrine bundle has been deprecated and replaced by a system of Middleware.

A work has started on Doctrine bundle with doctrine/DoctrineBundle#1456 and doctrine/DoctrineBundle#1472

This PR suggest to add a middleware thats covers what was previously done by DbalLogger and DebugStack.

Another PR will follow in DoctrineBundle for the integration.

@l-vo
Copy link
Contributor Author

l-vo commented Feb 20, 2022

status: needs work

@l-vo l-vo force-pushed the add_middleware_datacollector branch 4 times, most recently from 122ec76 to 0281685 Compare February 20, 2022 20:57
@l-vo
Copy link
Contributor Author

l-vo commented Feb 20, 2022

status: needs review

@carsonbot
Copy link

Hey!

I think @lucasaba has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

{
$query = null;
if (1 === ++$this->nestingLevel) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"'));
Copy link
Member

Choose a reason for hiding this comment

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

why the double quotes ? They are not part of the SQL query starting a transaction.

Copy link
Member

Choose a reason for hiding this comment

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

same for COMMIT and ROLLBACK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the format that is currently used in the profiler: https://github.com/doctrine/dbal/blob/2afc6f00e8ff145ab386e9d25af293db98418bf5/src/Connection.php#L1297; should I change it anyway ?

src/Symfony/Bridge/Doctrine/Middleware/Debug/Query.php Outdated Show resolved Hide resolved
@stof
Copy link
Member

stof commented Feb 24, 2022

@nicolas-grekas @fabpot given that this PR is about moving away from a deprecated DBAL API for the collector of the Symfony webprofiler, I suggest merging that into 5.4 rather than 6.1, so that the Symfony LTS does not use the deprecated API for years for the debug features.

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

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

All classes in the Symfony\Bridge\Doctrine\Middleware\Debug namespace except for the middleware itself and the DebugDataHolder should be tagged @internal. The various decorators are not meant to be reused by other code than our middleware (the DebugDataHolder needs to be wired by DoctrineBundle, which is why it cannot be internal)

@l-vo l-vo force-pushed the add_middleware_datacollector branch from 2eca244 to f660a87 Compare February 28, 2022 20:27
@l-vo
Copy link
Contributor Author

l-vo commented Feb 28, 2022

status: needs review

src/Symfony/Bridge/Doctrine/Middleware/Debug/Query.php Outdated Show resolved Hide resolved

public function start(): void
{
$this->start = microtime(true);
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this duplicate the logic from the stopwatch? How about using the duration from the stopwatch instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Stopwatch is an optional dependency of Doctrine bridge. Not using the stopwatch here allows to have durations even when stopwatch is not installed.

Copy link
Member

Choose a reason for hiding this comment

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

I find it kind of acceptable that timings are available only if the stopwatch is installed.

Copy link
Contributor Author

@l-vo l-vo Mar 13, 2022

Choose a reason for hiding this comment

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

The point is it's currently done in this way in DebugStack. So using stopwatch instead will lead to a loss of query durations occurring on a patch version (for an application without stopwatch). WDYT ?

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Wrong button… I'm getting old. 🙈

@l-vo l-vo force-pushed the add_middleware_datacollector branch 3 times, most recently from 8aeaacc to 8de3964 Compare March 13, 2022 16:12
@l-vo
Copy link
Contributor Author

l-vo commented Mar 13, 2022

@derrabus @stof target branch changed to 5.4. Should I do another PR to 6.1 after merge to restore modern additions brought by php >=8 ?

@l-vo
Copy link
Contributor Author

l-vo commented Mar 13, 2022

status: needs review

@derrabus
Copy link
Member

Should I do another PR to 6.1 after merge to restore modern additions brought by php >=8 ?

If we don't do this already while merging up, please create that PR.

@l-vo l-vo force-pushed the add_middleware_datacollector branch from 2d36823 to 20d0806 Compare March 22, 2022 20:09
@fabpot
Copy link
Member

fabpot commented Mar 25, 2022

Thank you @l-vo.

@fabpot
Copy link
Member

fabpot commented Apr 2, 2022

That's a breaking change as setMiddlewares() was introduced in DBAL 3, so this code is not compatible with Doctrine DBAL 2.

@fabpot
Copy link
Member

fabpot commented Apr 2, 2022

Nervermind, the BC break is in DoctrineBundle 2.6.

@l-vo l-vo deleted the add_middleware_datacollector branch April 2, 2022 13:57
@l-vo
Copy link
Contributor Author

l-vo commented Apr 2, 2022

@fabpot there is this kind of bc layer on Doctrine bundle: https://github.com/doctrine/DoctrineBundle/blob/0620e230c6529011b305e07efa0df0678bf71a6d/DoctrineBundle.php#L67. Did I miss another thing ?

@fabpot
Copy link
Member

fabpot commented Apr 2, 2022

Here is the error I get:

!!  Symfony\Component\ErrorHandler\Error\UndefinedMethodError {#5710
!!    #message: "Attempted to call an undefined method named "setMiddlewares" of class "Doctrine\DBAL\Configuration"."
!!    #code: 0
!!    #file: "./var/cache/dev/ContainerCojB6IU/App_KernelDevDebugContainer.php"
!!    #line: 1233
!!    trace: {
!!      ./var/cache/dev/ContainerCojB6IU/App_KernelDevDebugContainer.php:1233 {
!!        ContainerCojB6IU\App_KernelDevDebugContainer->getDoctrine_Dbal_DefaultConnectionService()
!!        › $a->setSQLLogger(new \Doctrine\DBAL\Logging\LoggerChain([0 => new \Symfony\Bridge\Doctrine\Logger\DbalLogger($b, ($this->privates['debug.stopwatch'] ?? ($this->privates['debug.stopwatch'] = new \Symfony\Component\Stopwatch\Stopwatch(true)))), 1 => ($this->privates['doctrine.dbal.logger.profiling.default'] ?? ($this->privates['doctrine.dbal.logger.profiling.default'] = new \Doctrine\DBAL\Logging\DebugStack()))]));
!!        › $a->setMiddlewares([]);
!!        › 
!!      }

@l-vo
Copy link
Contributor Author

l-vo commented Apr 2, 2022

By chance, is it a project with sentry-symfony ?

@fabpot
Copy link
Member

fabpot commented Apr 2, 2022

Yes, it is!

@l-vo
Copy link
Contributor Author

l-vo commented Apr 2, 2022

it has been spotted on doctrine/DoctrineBundle#1485, sentry-symfony aliases an internal interface as Doctrine\DBAL\Middleware that fools the detection. Accorded to the issue in DoctrineBundle, it won't be fixed on DoctrineBundle side. From @dmaicher:

Ok so I see 2 options:

  • report this issue on sentry-symfony as they really should not alias/define a Doctrine DBAL interface...
  • we look into changing the "middleware avaiable detection" on our side here so it works with Sentry

I personally would vote for the first option as this alias magic can really cause a lot of confusion and what the f**k moments as we see here.

@l-vo
Copy link
Contributor Author

l-vo commented Apr 2, 2022

Fixed on sentry-symfony side by getsentry/sentry-symfony#608

nicolas-grekas added a commit that referenced this pull request Apr 19, 2022
…g middlewares (l-vo)

This PR was submitted for the 6.0 branch but it was merged into the 6.1 branch instead.

Discussion
----------

[DoctrineBridge] Restore PHP8 features for Doctrine debug middlewares

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

 To merge #45491 in 5.4, the code has been changed to be 7.2 compliant. This PR restores the original code for Symfony versions that support php >= 8.0 (see #45491 (comment)).

Commits
-------

f4bdd82 Restore PHP8 features for Doctrine debug middlewares
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants