From f0de069d2c40e77cae9d669bb712a21f9bdee1cb Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 22:12:07 +0330 Subject: [PATCH] [11.x] Enhance DB inspection commands (#52501) * fix db:show with counts option * fix db:table * add more info * formatting * fix connection name and table size * Update ShowCommand.php --------- Co-authored-by: Taylor Otwell --- .../Database/Console/ShowCommand.php | 14 ++++----- .../Database/Console/TableCommand.php | 29 ++++++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowCommand.php b/src/Illuminate/Database/Console/ShowCommand.php index 4431c363fe6c..acb7470e6742 100644 --- a/src/Illuminate/Database/Console/ShowCommand.php +++ b/src/Illuminate/Database/Console/ShowCommand.php @@ -79,7 +79,9 @@ protected function tables(ConnectionInterface $connection, Builder $schema) 'table' => $table['name'], 'schema' => $table['schema'], 'size' => $table['size'], - 'rows' => $this->option('counts') ? $connection->table($table['name'])->count() : null, + 'rows' => $this->option('counts') + ? ($connection->table($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])->count()) + : null, 'engine' => $table['engine'], 'collation' => $table['collation'], 'comment' => $table['comment'], @@ -100,7 +102,7 @@ protected function views(ConnectionInterface $connection, Builder $schema) ->map(fn ($view) => [ 'view' => $view['name'], 'schema' => $view['schema'], - 'rows' => $connection->table($view->getName())->count(), + 'rows' => $connection->table($view['schema'] ? $view['schema'].'.'.$view['name'] : $view['name'])->count(), ]); } @@ -160,7 +162,7 @@ protected function displayForCli(array $data) $this->newLine(); $this->components->twoColumnDetail(''.$platform['name'].'', $platform['version']); - $this->components->twoColumnDetail('Connection', Arr::get($platform['config'], 'connection')); + $this->components->twoColumnDetail('Connection', $platform['connection']); $this->components->twoColumnDetail('Database', Arr::get($platform['config'], 'database')); $this->components->twoColumnDetail('Host', Arr::get($platform['config'], 'host')); $this->components->twoColumnDetail('Port', Arr::get($platform['config'], 'port')); @@ -184,13 +186,11 @@ protected function displayForCli(array $data) ); $tables->each(function ($table) { - if ($tableSize = $table['size']) { - $tableSize = Number::fileSize($tableSize, 2); - } + $tableSize = is_null($table['size']) ? null : Number::fileSize($table['size'], 2); $this->components->twoColumnDetail( ($table['schema'] ? $table['schema'].' / ' : '').$table['table'].($this->output->isVerbose() ? ' '.$table['engine'].'' : null), - ($tableSize ?: '—').($this->option('counts') ? ' / '.Number::format($table['rows']).'' : '') + ($tableSize ?? '—').($this->option('counts') ? ' / '.Number::format($table['rows']).'' : '') ); if ($this->output->isVerbose()) { diff --git a/src/Illuminate/Database/Console/TableCommand.php b/src/Illuminate/Database/Console/TableCommand.php index 2a485ab39c9e..4362ca82d29c 100644 --- a/src/Illuminate/Database/Console/TableCommand.php +++ b/src/Illuminate/Database/Console/TableCommand.php @@ -39,14 +39,16 @@ public function handle(ConnectionResolverInterface $connections) { $connection = $connections->connection($this->input->getOption('database')); $schema = $connection->getSchemaBuilder(); - $tables = $schema->getTables(); + $tables = collect($schema->getTables()) + ->keyBy(fn ($table) => $table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']) + ->all(); $tableName = $this->argument('table') ?: select( 'Which table would you like to inspect?', - array_column($tables, 'name') + array_keys($tables) ); - $table = Arr::first($tables, fn ($table) => $table['name'] === $tableName); + $table = $tables[$tableName] ?? Arr::first($tables, fn ($table) => $table['name'] === $tableName); if (! $table) { $this->components->warn("Table [{$tableName}] doesn't exist."); @@ -54,7 +56,7 @@ public function handle(ConnectionResolverInterface $connections) return 1; } - $tableName = $this->withoutTablePrefix($connection, $table['name']); + $tableName = ($table['schema'] ? $table['schema'].'.' : '').$this->withoutTablePrefix($connection, $table['name']); $columns = $this->columns($schema, $tableName); $indexes = $this->indexes($schema, $tableName); @@ -62,9 +64,13 @@ public function handle(ConnectionResolverInterface $connections) $data = [ 'table' => [ + 'schema' => $table['schema'], 'name' => $table['name'], 'columns' => count($columns), 'size' => $table['size'], + 'comment' => $table['comment'], + 'collation' => $table['collation'], + 'engine' => $table['engine'], ], 'columns' => $columns, 'indexes' => $indexes, @@ -103,6 +109,7 @@ protected function getAttributesForColumn($column) { return collect([ $column['type_name'], + $column['generation'] ? $column['generation']['type'] : null, $column['auto_increment'] ? 'autoincrement' : null, $column['nullable'] ? 'nullable' : null, $column['collation'], @@ -197,11 +204,19 @@ protected function displayForCli(array $data) $this->newLine(); - $this->components->twoColumnDetail(''.$table['name'].''); + $this->components->twoColumnDetail(''.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).'', $table['comment'] ? ''.$table['comment'].'' : null); $this->components->twoColumnDetail('Columns', $table['columns']); - if ($size = $table['size']) { - $this->components->twoColumnDetail('Size', Number::fileSize($size, 2)); + if (! is_null($table['size'])) { + $this->components->twoColumnDetail('Size', Number::fileSize($table['size'], 2)); + } + + if ($table['engine']) { + $this->components->twoColumnDetail('Engine', $table['engine']); + } + + if ($table['collation']) { + $this->components->twoColumnDetail('Collation', $table['collation']); } $this->newLine();