Skip to content

Commit

Permalink
[11.x] Enhance DB inspection commands (#52501)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Aug 15, 2024
1 parent 0b3d2aa commit f0de069
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/Illuminate/Database/Console/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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(),
]);
}

Expand Down Expand Up @@ -160,7 +162,7 @@ protected function displayForCli(array $data)
$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>'.$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'));
Expand All @@ -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'].' <fg=gray;options=bold>/</> ' : '').$table['table'].($this->output->isVerbose() ? ' <fg=gray>'.$table['engine'].'</>' : null),
($tableSize ?: '').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.Number::format($table['rows']).'</>' : '')
($tableSize ?? '').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.Number::format($table['rows']).'</>' : '')
);

if ($this->output->isVerbose()) {
Expand Down
29 changes: 22 additions & 7 deletions src/Illuminate/Database/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,38 @@ 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.");

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);
$foreignKeys = $this->foreignKeys($schema, $tableName);

$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,
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -197,11 +204,19 @@ protected function displayForCli(array $data)

$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>'.$table['name'].'</>');
$this->components->twoColumnDetail('<fg=green;options=bold>'.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).'</>', $table['comment'] ? '<fg=gray>'.$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();
Expand Down

0 comments on commit f0de069

Please sign in to comment.