Skip to content

faster sql query, fixes #15 #17

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

tacman
Copy link
Contributor

@tacman tacman commented Apr 3, 2024

No description provided.

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 42.28%. Comparing base (3b28751) to head (16df034).
Report is 3 commits behind head on main.

Files Patch % Lines
src/HealthCheck/DoctrineConnectionHealthCheck.php 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main      #17      +/-   ##
============================================
+ Coverage     41.17%   42.28%   +1.10%     
+ Complexity       52       51       -1     
============================================
  Files            12       12              
  Lines           153      149       -4     
============================================
  Hits             63       63              
+ Misses           90       86       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thijskh
Copy link
Member

thijskh commented Apr 4, 2024

Thanks, indeed it fixes the issue but we want to ensure just a little bit more; this query will return success even if the database in question does not exist or is compleltely empty, while we want to do the basic sanity check that things "work". So I think we'll only merge this if indeed the query in question is in fact configurable.

@tacman
Copy link
Contributor Author

tacman commented Apr 4, 2024 via email

@tacman
Copy link
Contributor Author

tacman commented Apr 4, 2024 via email

@MKodde
Copy link
Member

MKodde commented Apr 11, 2024

@tacman if the current query is not working correctly on other database platforms then by all means, feel free to suggest a better query that runs on more platforms. If the backticks are the only thing messing things up, feel free to remove them here.

I do like your suggestion, to inject the query from the config. That way a user can configure an appropriate query for the application the monitor bundle is running on.

Some thoughts:

  1. I like the current setup as it looks for the first schema in the database and runs a query on that. That's zero config for me. And I have many projects that run the monitor bundle. I'm not looking forward to configuring a test qurey for all of them. So maybe use the injected query as an override to the default behavior?
  2. You could look into adding a service override for Postgres users. That would require adding some magic in the compile pass I assume. But it could be a solution?

But is there even a way to disable services from the application now?

If you mean: is it possible to remove a service from being added to the health checker service? My answer is yes and no. Any service that is tagged with the openconext.monitor.health_check is included in the health checker service. That task is performed here:

foreach ($taggedServices as $id => $tags) {
$definition->addMethodCall('addHealthCheck', array(new Reference($id)));
}
.

No: that config is now hard coded in the services.yaml file. So it would need some tweaking to make it configurable.
Yes: you could add some additional logic to the compiler pass. It could check your database platform, and select the appropriate DB health checker that way.

Hope this helps you somewhat. If you need some more help, feel free to reach out to me on the OpenConext Slack channel or on the Symfony Devs Slack. You should be able to find me there

@tacman
Copy link
Contributor Author

tacman commented Apr 11, 2024

How about instead of hand-coding the query:

$query = 'SELECT * FROM `%s` LIMIT 1';
$this->entityManager->getConnection()->executeQuery(sprintf($query, $table->getName()));

We use a querybuilder, which automatically escapes the table name.

        $this->entityManager->createQueryBuilder()
            ->from($table->getName())
            ->setMaxResults(1)
            ->getQuery()
            ->getResult(); // or a count query, getOneOrNull(), or whatever.

There are faster ways to do this, but I think this will at least allow postgres. Maybe use the queryCache, since we're not actually concerned with the data? Or even a limit of 0, which would make the query but not hydrate an entity, though maybe you want to test that.

@tacman
Copy link
Contributor Author

tacman commented Apr 11, 2024

I think the backticks are only relevant when the table name is a reserved work. Unfortunately, Symfony's 'make:user' command create a user table if the database connection is postgres, which I always rename to 'users' so I can easily switch between postgres and sqlite.

@tacman
Copy link
Contributor Author

tacman commented Apr 19, 2024

I'd love to use the bundle, but because I use postgres it consistently fails.

I know the select 1 didn't accomplish what you want, but I'm not sure how to fix this.

@MKodde
Copy link
Member

MKodde commented Apr 24, 2024

How about instead of hand-coding the query:

$query = 'SELECT * FROM `%s` LIMIT 1';
$this->entityManager->getConnection()->executeQuery(sprintf($query, $table->getName()));

We use a querybuilder, which automatically escapes the table name.

        $this->entityManager->createQueryBuilder()
            ->from($table->getName())
            ->setMaxResults(1)
            ->getQuery()
            ->getResult(); // or a count query, getOneOrNull(), or whatever.

There are faster ways to do this, but I think this will at least allow postgres. Maybe use the queryCache, since we're not actually concerned with the data? Or even a limit of 0, which would make the query but not hydrate an entity, though maybe you want to test that.

We think that should work for us as well. The performance penalty should be minimal. So feel free to go ahead with this suggestion.

@thijskh
Copy link
Member

thijskh commented Aug 21, 2024

Hi @tacman. Can you move forward with the suggested change? An alternative that we would find acceptable to make the full SQL query a configurable string in the bundle config. That also seems like a flexible and generic solution.

@tacman
Copy link
Contributor Author

tacman commented Aug 21, 2024

ok, to confirm, that's

    $this->entityManager->createQueryBuilder()
        ->from($table->getName())
        ->setMaxResults(1)
        ->getQuery()
        ->getResult(); // or a count query, getOneOrNull(), or whatever.

I think getOneOrNull is good, because it has the benefit of hydrating the entity, so it will throw an error if a migration (on that table) is missing.

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