Skip to content

Commit f0de069

Browse files
[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 <taylor@laravel.com>
1 parent 0b3d2aa commit f0de069

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/Illuminate/Database/Console/ShowCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ protected function tables(ConnectionInterface $connection, Builder $schema)
7979
'table' => $table['name'],
8080
'schema' => $table['schema'],
8181
'size' => $table['size'],
82-
'rows' => $this->option('counts') ? $connection->table($table['name'])->count() : null,
82+
'rows' => $this->option('counts')
83+
? ($connection->table($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])->count())
84+
: null,
8385
'engine' => $table['engine'],
8486
'collation' => $table['collation'],
8587
'comment' => $table['comment'],
@@ -100,7 +102,7 @@ protected function views(ConnectionInterface $connection, Builder $schema)
100102
->map(fn ($view) => [
101103
'view' => $view['name'],
102104
'schema' => $view['schema'],
103-
'rows' => $connection->table($view->getName())->count(),
105+
'rows' => $connection->table($view['schema'] ? $view['schema'].'.'.$view['name'] : $view['name'])->count(),
104106
]);
105107
}
106108

@@ -160,7 +162,7 @@ protected function displayForCli(array $data)
160162
$this->newLine();
161163

162164
$this->components->twoColumnDetail('<fg=green;options=bold>'.$platform['name'].'</>', $platform['version']);
163-
$this->components->twoColumnDetail('Connection', Arr::get($platform['config'], 'connection'));
165+
$this->components->twoColumnDetail('Connection', $platform['connection']);
164166
$this->components->twoColumnDetail('Database', Arr::get($platform['config'], 'database'));
165167
$this->components->twoColumnDetail('Host', Arr::get($platform['config'], 'host'));
166168
$this->components->twoColumnDetail('Port', Arr::get($platform['config'], 'port'));
@@ -184,13 +186,11 @@ protected function displayForCli(array $data)
184186
);
185187

186188
$tables->each(function ($table) {
187-
if ($tableSize = $table['size']) {
188-
$tableSize = Number::fileSize($tableSize, 2);
189-
}
189+
$tableSize = is_null($table['size']) ? null : Number::fileSize($table['size'], 2);
190190

191191
$this->components->twoColumnDetail(
192192
($table['schema'] ? $table['schema'].' <fg=gray;options=bold>/</> ' : '').$table['table'].($this->output->isVerbose() ? ' <fg=gray>'.$table['engine'].'</>' : null),
193-
($tableSize ?: '').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.Number::format($table['rows']).'</>' : '')
193+
($tableSize ?? '').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.Number::format($table['rows']).'</>' : '')
194194
);
195195

196196
if ($this->output->isVerbose()) {

src/Illuminate/Database/Console/TableCommand.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,38 @@ public function handle(ConnectionResolverInterface $connections)
3939
{
4040
$connection = $connections->connection($this->input->getOption('database'));
4141
$schema = $connection->getSchemaBuilder();
42-
$tables = $schema->getTables();
42+
$tables = collect($schema->getTables())
43+
->keyBy(fn ($table) => $table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])
44+
->all();
4345

4446
$tableName = $this->argument('table') ?: select(
4547
'Which table would you like to inspect?',
46-
array_column($tables, 'name')
48+
array_keys($tables)
4749
);
4850

49-
$table = Arr::first($tables, fn ($table) => $table['name'] === $tableName);
51+
$table = $tables[$tableName] ?? Arr::first($tables, fn ($table) => $table['name'] === $tableName);
5052

5153
if (! $table) {
5254
$this->components->warn("Table [{$tableName}] doesn't exist.");
5355

5456
return 1;
5557
}
5658

57-
$tableName = $this->withoutTablePrefix($connection, $table['name']);
59+
$tableName = ($table['schema'] ? $table['schema'].'.' : '').$this->withoutTablePrefix($connection, $table['name']);
5860

5961
$columns = $this->columns($schema, $tableName);
6062
$indexes = $this->indexes($schema, $tableName);
6163
$foreignKeys = $this->foreignKeys($schema, $tableName);
6264

6365
$data = [
6466
'table' => [
67+
'schema' => $table['schema'],
6568
'name' => $table['name'],
6669
'columns' => count($columns),
6770
'size' => $table['size'],
71+
'comment' => $table['comment'],
72+
'collation' => $table['collation'],
73+
'engine' => $table['engine'],
6874
],
6975
'columns' => $columns,
7076
'indexes' => $indexes,
@@ -103,6 +109,7 @@ protected function getAttributesForColumn($column)
103109
{
104110
return collect([
105111
$column['type_name'],
112+
$column['generation'] ? $column['generation']['type'] : null,
106113
$column['auto_increment'] ? 'autoincrement' : null,
107114
$column['nullable'] ? 'nullable' : null,
108115
$column['collation'],
@@ -197,11 +204,19 @@ protected function displayForCli(array $data)
197204

198205
$this->newLine();
199206

200-
$this->components->twoColumnDetail('<fg=green;options=bold>'.$table['name'].'</>');
207+
$this->components->twoColumnDetail('<fg=green;options=bold>'.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).'</>', $table['comment'] ? '<fg=gray>'.$table['comment'].'</>' : null);
201208
$this->components->twoColumnDetail('Columns', $table['columns']);
202209

203-
if ($size = $table['size']) {
204-
$this->components->twoColumnDetail('Size', Number::fileSize($size, 2));
210+
if (! is_null($table['size'])) {
211+
$this->components->twoColumnDetail('Size', Number::fileSize($table['size'], 2));
212+
}
213+
214+
if ($table['engine']) {
215+
$this->components->twoColumnDetail('Engine', $table['engine']);
216+
}
217+
218+
if ($table['collation']) {
219+
$this->components->twoColumnDetail('Collation', $table['collation']);
205220
}
206221

207222
$this->newLine();

0 commit comments

Comments
 (0)