|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Frosh\Tools\Components\Health\Checker\PerformanceChecker; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Frosh\Tools\Components\Health\Checker\CheckerInterface; |
| 9 | +use Frosh\Tools\Components\Health\HealthCollection; |
| 10 | +use Frosh\Tools\Components\Health\SettingsResult; |
| 11 | +use Shopware\Core\DevOps\Environment\EnvironmentHelper; |
| 12 | + |
| 13 | +class MysqlSettingsChecker implements PerformanceCheckerInterface, CheckerInterface |
| 14 | +{ |
| 15 | + public const DOCUMENTATION_URL = 'https://developer.shopware.com/docs/guides/hosting/performance/performance-tweaks.html#mysql-configuration'; |
| 16 | + |
| 17 | + public const MYSQL_GROUP_CONCAT_MAX_LEN = 320000; |
| 18 | + |
| 19 | + public const MYSQL_SQL_MODE_PART = 'ONLY_FULL_GROUP_BY'; |
| 20 | + |
| 21 | + public function __construct(private readonly Connection $connection) {} |
| 22 | + |
| 23 | + public function collect(HealthCollection $collection): void |
| 24 | + { |
| 25 | + $this->checkGroupConcatMaxLen($collection); |
| 26 | + $this->checkSqlMode($collection); |
| 27 | + $this->checkCheckDefaultEnvironmentSessionVariables($collection); |
| 28 | + } |
| 29 | + |
| 30 | + private function checkGroupConcatMaxLen(HealthCollection $collection): void |
| 31 | + { |
| 32 | + /** @var string|false $groupConcatMaxLen */ |
| 33 | + $groupConcatMaxLen = $this->connection->fetchOne('SELECT @@group_concat_max_len'); |
| 34 | + if (!$groupConcatMaxLen || (int) $groupConcatMaxLen < self::MYSQL_GROUP_CONCAT_MAX_LEN) { |
| 35 | + $collection->add( |
| 36 | + SettingsResult::error( |
| 37 | + 'sql_group_concat_max_len', |
| 38 | + 'MySQL value group_concat_max_len', |
| 39 | + (string) $groupConcatMaxLen, |
| 40 | + 'Atleast ' . self::MYSQL_GROUP_CONCAT_MAX_LEN, |
| 41 | + self::DOCUMENTATION_URL, |
| 42 | + ), |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private function checkSqlMode(HealthCollection $collection): void |
| 48 | + { |
| 49 | + /** @var string|false $sqlMode */ |
| 50 | + $sqlMode = $this->connection->fetchOne('SELECT @@sql_mode'); |
| 51 | + if (!$sqlMode || !str_contains($sqlMode, self::MYSQL_SQL_MODE_PART)) { |
| 52 | + $collection->add( |
| 53 | + SettingsResult::error( |
| 54 | + 'sql_mode', |
| 55 | + 'MySQL value sql_mode', |
| 56 | + (string) $sqlMode, |
| 57 | + 'Contains ' . self::MYSQL_SQL_MODE_PART, |
| 58 | + self::DOCUMENTATION_URL, |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private function checkCheckDefaultEnvironmentSessionVariables(HealthCollection $collection): void |
| 65 | + { |
| 66 | + $setSessionVariables = (bool) EnvironmentHelper::getVariable('SQL_SET_DEFAULT_SESSION_VARIABLES', true); |
| 67 | + if ($setSessionVariables) { |
| 68 | + $collection->add( |
| 69 | + SettingsResult::warning( |
| 70 | + 'sql_set_default_session_variables', |
| 71 | + 'MySQL session vars are set on each connect', |
| 72 | + 'enabled', |
| 73 | + 'disabled', |
| 74 | + self::DOCUMENTATION_URL, |
| 75 | + ), |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments