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