|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OC\Core\Command\Db; |
| 10 | + |
| 11 | +use OC\DB\Connection; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Helper\Table; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Doctrine\DBAL\Platforms\MySQLPlatform; |
| 18 | +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; |
| 19 | +use Doctrine\DBAL\Platforms\SqlitePlatform; |
| 20 | + |
| 21 | +class DbInfo extends Command { |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private readonly Connection $connection, |
| 25 | + ) { |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure(): void { |
| 30 | + $this |
| 31 | + ->setName('db:info') |
| 32 | + ->setDescription('Show database server information and configuration health check') |
| 33 | + ->addOption('json', null, InputOption::VALUE_NONE, 'Output in JSON format'); |
| 34 | + } |
| 35 | + |
| 36 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 37 | + $platform = $this->connection->getDatabasePlatform(); |
| 38 | + $asJson = $input->getOption('json'); |
| 39 | + |
| 40 | + if ($platform instanceof MySQLPlatform) { |
| 41 | + $rows = $this->getMySQLInfo(); |
| 42 | + } elseif ($platform instanceof PostgreSQLPlatform) { |
| 43 | + $rows = $this->getPostgreSQLInfo(); |
| 44 | + } elseif ($platform instanceof SqlitePlatform) { |
| 45 | + $rows = $this->getSQLiteInfo(); |
| 46 | + } else { |
| 47 | + $output->writeln('<error>Unsupported database platform.</error>'); |
| 48 | + return Command::FAILURE; |
| 49 | + } |
| 50 | + |
| 51 | + if ($asJson) { |
| 52 | + $output->writeln(json_encode($rows, JSON_PRETTY_PRINT)); |
| 53 | + return Command::SUCCESS; |
| 54 | + } |
| 55 | + |
| 56 | + $table = new Table($output); |
| 57 | + $table->setHeaders(['Setting', 'Value', 'Recommended', 'Status']); |
| 58 | + |
| 59 | + foreach ($rows as $row) { |
| 60 | + $status = isset($row['recommended']) |
| 61 | + ? ($row['ok'] ? '<info>OK</info>' : '<comment>CHECK</comment>') |
| 62 | + : ''; |
| 63 | + $table->addRow([ |
| 64 | + $row['setting'], |
| 65 | + $row['value'], |
| 66 | + $row['recommended'] ?? '—', |
| 67 | + $status, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + $table->render(); |
| 72 | + return Command::SUCCESS; |
| 73 | + } |
| 74 | + |
| 75 | + private function getMySQLInfo(): array { |
| 76 | + $result = $this->connection->executeQuery( |
| 77 | + "SELECT VERSION() AS version, @@innodb_buffer_pool_size AS buffer_pool, |
| 78 | + @@max_connections AS max_conn, @@character_set_database AS charset, |
| 79 | + @@transaction_isolation AS tx_isolation" |
| 80 | + ); |
| 81 | + $info = $result->fetchAssociative(); |
| 82 | + |
| 83 | + $bufferPoolGB = round(($info['buffer_pool'] / 1024 / 1024 / 1024), 2); |
| 84 | + |
| 85 | + return [ |
| 86 | + ['setting' => 'Engine', 'value' => 'MySQL/MariaDB'], |
| 87 | + ['setting' => 'Version', 'value' => $info['version']], |
| 88 | + ['setting' => 'Character Set', 'value' => $info['charset'], 'recommended' => 'utf8mb4', 'ok' => str_contains($info['charset'], 'utf8mb4')], |
| 89 | + ['setting' => 'Max Connections', 'value' => $info['max_conn'], 'recommended' => '≥ 150', 'ok' => (int)$info['max_conn'] >= 150], |
| 90 | + ['setting' => 'InnoDB Buffer Pool (GB)','value' => $bufferPoolGB, 'recommended' => '≥ 1 GB', 'ok' => $bufferPoolGB >= 1], |
| 91 | + ['setting' => 'Transaction Isolation', 'value' => $info['tx_isolation'], 'recommended' => 'READ-COMMITTED', 'ok' => $info['tx_isolation'] === 'READ-COMMITTED'], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + private function getPostgreSQLInfo(): array { |
| 96 | + $result = $this->connection->executeQuery( |
| 97 | + "SELECT version(), |
| 98 | + current_setting('max_connections') AS max_conn, |
| 99 | + current_setting('shared_buffers') AS shared_buffers, |
| 100 | + current_setting('work_mem') AS work_mem" |
| 101 | + ); |
| 102 | + $info = $result->fetchAssociative(); |
| 103 | + |
| 104 | + return [ |
| 105 | + ['setting' => 'Engine', 'value' => 'PostgreSQL'], |
| 106 | + ['setting' => 'Version', 'value' => $info['version']], |
| 107 | + ['setting' => 'Max Connections', 'value' => $info['max_conn'], 'recommended' => '≥ 100', 'ok' => (int)$info['max_conn'] >= 100], |
| 108 | + ['setting' => 'Shared Buffers', 'value' => $info['shared_buffers'],'recommended' => '128MB+', 'ok' => true], |
| 109 | + ['setting' => 'Work Mem', 'value' => $info['work_mem'], 'recommended' => '4MB+', 'ok' => true], |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + private function getSQLiteInfo(): array { |
| 114 | + $result = $this->connection->executeQuery('SELECT sqlite_version() AS version'); |
| 115 | + $info = $result->fetchAssociative(); |
| 116 | + return [ |
| 117 | + ['setting' => 'Engine', 'value' => 'SQLite'], |
| 118 | + ['setting' => 'Version', 'value' => $info['version']], |
| 119 | + ]; |
| 120 | + } |
| 121 | +} |
0 commit comments