Skip to content

Commit

Permalink
feat: migrate code of DatabaseConnectionInformation into own checker
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Jan 29, 2025
1 parent 9418f33 commit 2fb2b87
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/Components/Health/Checker/HealthChecker/SystemInfoChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Frosh\Tools\Components\Health\Checker\CheckerInterface;
use Frosh\Tools\Components\Health\HealthCollection;
use Frosh\Tools\Components\Health\SettingsResult;
use Shopware\Core\Maintenance\System\Struct\DatabaseConnectionInformation;
use Shopware\Core\DevOps\Environment\EnvironmentHelper;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class SystemInfoChecker implements HealthCheckerInterface, CheckerInterface
Expand Down Expand Up @@ -36,20 +36,44 @@ private function checkPath(HealthCollection $collection): void

private function getDatabaseInfo(HealthCollection $collection): void
{
$databaseConnectionInfo = (new DatabaseConnectionInformation())->fromEnv();
$result = new SettingsResult();
$result->assign([
'id' => 'database-info',
'snippet' => 'Database',
'current' => 'unknown',
]);

$collection->add(
SettingsResult::ok(
'database-info',
'Database',
\sprintf(
'%s@%s:%d/%s',
$databaseConnectionInfo->getUsername(),
$databaseConnectionInfo->getHostname(),
$databaseConnectionInfo->getPort(),
$databaseConnectionInfo->getDatabaseName(),
),
),
);
try {
$dsn = trim((string) EnvironmentHelper::getVariable('DATABASE_URL', getenv('DATABASE_URL')));
if ($dsn === '') {
return;
}

$params = parse_url($dsn);
if ($params === false) {
return;
}

foreach ($params as $param => $value) {
if (!\is_string($value)) {
continue;
}

$params[$param] = rawurldecode($value);
}

$path = (string) ($params['path'] ?? '/');
$dbName = trim(substr($path, 1));

$result->current =\sprintf(
'%s@%s:%d/%s',
$params['user'] ?? null,
$params['host'] ?? null,
(int) ($params['port'] ?? '3306'),
$dbName,
);
} finally {
$collection->add($result);
}
}
}

0 comments on commit 2fb2b87

Please sign in to comment.