|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kiwilan\Steward\Commands\Db; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | +use Kiwilan\Steward\Commands\Commandable; |
| 8 | + |
| 9 | +class DbTestCommand extends Commandable |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'db:test |
| 17 | + {--c|credentials : Show database credentials}'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Test database connection'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + * |
| 29 | + * @return int |
| 30 | + */ |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + $this->title(); |
| 34 | + |
| 35 | + $credentials = $this->optionBool('credentials'); |
| 36 | + $success = false; |
| 37 | + |
| 38 | + $this->info('Testing database connection...'); |
| 39 | + $this->newLine(); |
| 40 | + |
| 41 | + $connection = Schema::getConnection(); |
| 42 | + |
| 43 | + $driver = $connection->getDriverName(); |
| 44 | + $database = $connection->getDatabaseName(); |
| 45 | + $host = $connection->getConfig('host'); |
| 46 | + $port = $connection->getConfig('port'); |
| 47 | + $username = $connection->getConfig('username'); |
| 48 | + $password = $connection->getConfig('password'); |
| 49 | + |
| 50 | + $this->comment("Driver: {$driver}"); |
| 51 | + $this->comment("Database: {$database}"); |
| 52 | + $this->comment("Host: {$host}"); |
| 53 | + $this->comment("Port: {$port}"); |
| 54 | + |
| 55 | + if ($credentials) { |
| 56 | + $this->comment("Username: {$username}"); |
| 57 | + $this->comment("Password: {$password}"); |
| 58 | + } |
| 59 | + |
| 60 | + $this->newLine(); |
| 61 | + |
| 62 | + $this->comment("Try to ping database at {$host}:{$port}..."); |
| 63 | + $available = $this->pingDatabase($host, $port); |
| 64 | + $this->info($available ? 'Database is available.' : 'Database is not available.'); |
| 65 | + |
| 66 | + $this->newLine(); |
| 67 | + |
| 68 | + if ($available) { |
| 69 | + $success = $this->testConnection($connection); |
| 70 | + } else { |
| 71 | + $this->comment('Try to get error information...'); |
| 72 | + $this->getError($connection); |
| 73 | + |
| 74 | + $success = false; |
| 75 | + } |
| 76 | + |
| 77 | + if (! $success) { |
| 78 | + return Command::FAILURE; |
| 79 | + } |
| 80 | + |
| 81 | + return Command::SUCCESS; |
| 82 | + } |
| 83 | + |
| 84 | + private function pingDatabase(string $url, int|string $port): bool |
| 85 | + { |
| 86 | + $connection = @fsockopen($url, $port, $errno, $errstr, 1); |
| 87 | + if (is_resource($connection)) { |
| 88 | + fclose($connection); |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private function getError(\Illuminate\Database\Connection $connection): void |
| 97 | + { |
| 98 | + try { |
| 99 | + $connection->getPdo(); |
| 100 | + } catch (\Throwable $th) { |
| 101 | + $this->error($th->getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private function testConnection(\Illuminate\Database\Connection $connection): bool |
| 106 | + { |
| 107 | + $driverName = null; |
| 108 | + $serverInfo = null; |
| 109 | + $clientVersion = null; |
| 110 | + $serverVersion = null; |
| 111 | + $connectionStatus = null; |
| 112 | + |
| 113 | + try { |
| 114 | + $pdo = $connection->getPdo(); |
| 115 | + |
| 116 | + $driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
| 117 | + $serverInfo = $pdo->getAttribute(\PDO::ATTR_SERVER_INFO); |
| 118 | + $clientVersion = $pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION); |
| 119 | + $serverVersion = $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION); |
| 120 | + $connectionStatus = $pdo->getAttribute(\PDO::ATTR_CONNECTION_STATUS); |
| 121 | + } catch (\Exception $e) { |
| 122 | + $this->error('Connection failed.'); |
| 123 | + |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + $this->alert('Connection successful.'); |
| 128 | + $this->comment("Driver name: {$driverName}"); |
| 129 | + $this->comment("Server info: {$serverInfo}"); |
| 130 | + $this->comment("Client version: {$clientVersion}"); |
| 131 | + $this->comment("Server version: {$serverVersion}"); |
| 132 | + $this->comment("Connection status: {$connectionStatus}"); |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | +} |
0 commit comments