From b21cbefd339346a8b4d1a7ea587551f19a546d40 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 18:56:08 +0330 Subject: [PATCH 1/6] fix db:show with counts option --- src/Illuminate/Database/Console/ShowCommand.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowCommand.php b/src/Illuminate/Database/Console/ShowCommand.php index 4431c363fe6c..be2f456da80a 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,8 @@ 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(), + ]); } From afe3d691c3cdd991f45dff5196ee9b3b6c046a4b Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 19:42:44 +0330 Subject: [PATCH 2/6] fix db:table --- src/Illuminate/Database/Console/TableCommand.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Console/TableCommand.php b/src/Illuminate/Database/Console/TableCommand.php index 2a485ab39c9e..3d8513486458 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,9 @@ public function handle(ConnectionResolverInterface $connections) return 1; } - $tableName = $this->withoutTablePrefix($connection, $table['name']); + $tableName = $table['schema'] + ? $table['schema'].'.'.$this->withoutTablePrefix($connection, $table['name']) + : $this->withoutTablePrefix($connection, $table['name']); $columns = $this->columns($schema, $tableName); $indexes = $this->indexes($schema, $tableName); @@ -62,6 +66,7 @@ public function handle(ConnectionResolverInterface $connections) $data = [ 'table' => [ + 'schema' => $table['schema'], 'name' => $table['name'], 'columns' => count($columns), 'size' => $table['size'], @@ -197,7 +202,7 @@ protected function displayForCli(array $data) $this->newLine(); - $this->components->twoColumnDetail(''.$table['name'].''); + $this->components->twoColumnDetail(''.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).''); $this->components->twoColumnDetail('Columns', $table['columns']); if ($size = $table['size']) { From cd5ff025513a539065c65a40737fb19f25f6b848 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 20:29:23 +0330 Subject: [PATCH 3/6] add more info --- .../Database/Console/ShowCommand.php | 1 - .../Database/Console/TableCommand.php | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowCommand.php b/src/Illuminate/Database/Console/ShowCommand.php index be2f456da80a..0637dd37f5e5 100644 --- a/src/Illuminate/Database/Console/ShowCommand.php +++ b/src/Illuminate/Database/Console/ShowCommand.php @@ -103,7 +103,6 @@ protected function views(ConnectionInterface $connection, Builder $schema) 'view' => $view['name'], 'schema' => $view['schema'], 'rows' => $connection->table($view['schema'] ? $view['schema'].'.'.$view['name'] : $view['name'])->count(), - ]); } diff --git a/src/Illuminate/Database/Console/TableCommand.php b/src/Illuminate/Database/Console/TableCommand.php index 3d8513486458..5c3240d624fc 100644 --- a/src/Illuminate/Database/Console/TableCommand.php +++ b/src/Illuminate/Database/Console/TableCommand.php @@ -56,9 +56,7 @@ public function handle(ConnectionResolverInterface $connections) return 1; } - $tableName = $table['schema'] - ? $table['schema'].'.'.$this->withoutTablePrefix($connection, $table['name']) - : $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); @@ -70,6 +68,9 @@ public function handle(ConnectionResolverInterface $connections) 'name' => $table['name'], 'columns' => count($columns), 'size' => $table['size'], + 'comment' => $table['comment'], + 'collation' => $table['collation'], + 'engine' => $table['engine'], ], 'columns' => $columns, 'indexes' => $indexes, @@ -108,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'], @@ -202,13 +204,21 @@ protected function displayForCli(array $data) $this->newLine(); - $this->components->twoColumnDetail(''.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).''); + $this->components->twoColumnDetail(''.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).'', $table['comment']); $this->components->twoColumnDetail('Columns', $table['columns']); if ($size = $table['size']) { $this->components->twoColumnDetail('Size', Number::fileSize($size, 2)); } + if ($table['engine']) { + $this->components->twoColumnDetail('Engine', $table['engine']); + } + + if ($table['collation']) { + $this->components->twoColumnDetail('Collation', $table['collation']); + } + $this->newLine(); if ($columns->isNotEmpty()) { From 837e0185217453ea8ee4d76b9c9c368f2241665b Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 21:15:47 +0330 Subject: [PATCH 4/6] formatting --- src/Illuminate/Database/Console/TableCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Console/TableCommand.php b/src/Illuminate/Database/Console/TableCommand.php index 5c3240d624fc..4362ca82d29c 100644 --- a/src/Illuminate/Database/Console/TableCommand.php +++ b/src/Illuminate/Database/Console/TableCommand.php @@ -204,11 +204,11 @@ protected function displayForCli(array $data) $this->newLine(); - $this->components->twoColumnDetail(''.($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name']).'', $table['comment']); + $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']) { From 15513c4d719f71f7137252b4abab1cbf00e5baca Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 15 Aug 2024 22:01:12 +0330 Subject: [PATCH 5/6] fix connection name and table size --- src/Illuminate/Database/Console/ShowCommand.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowCommand.php b/src/Illuminate/Database/Console/ShowCommand.php index 0637dd37f5e5..0741beb48dc2 100644 --- a/src/Illuminate/Database/Console/ShowCommand.php +++ b/src/Illuminate/Database/Console/ShowCommand.php @@ -162,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')); @@ -186,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()) { From 0524d027e59314d001535b7c1ce12e4987788b43 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 15 Aug 2024 13:39:11 -0500 Subject: [PATCH 6/6] Update ShowCommand.php --- src/Illuminate/Database/Console/ShowCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Console/ShowCommand.php b/src/Illuminate/Database/Console/ShowCommand.php index 0741beb48dc2..acb7470e6742 100644 --- a/src/Illuminate/Database/Console/ShowCommand.php +++ b/src/Illuminate/Database/Console/ShowCommand.php @@ -80,7 +80,7 @@ protected function tables(ConnectionInterface $connection, Builder $schema) 'schema' => $table['schema'], 'size' => $table['size'], 'rows' => $this->option('counts') - ? $connection->table($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])->count() + ? ($connection->table($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])->count()) : null, 'engine' => $table['engine'], 'collation' => $table['collation'],