| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Console;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
|
|
6 |
use Doctrine\DBAL\Schema\Table;
|
|
|
7 |
use Doctrine\DBAL\Schema\View;
|
|
|
8 |
use Illuminate\Database\ConnectionInterface;
|
|
|
9 |
use Illuminate\Database\ConnectionResolverInterface;
|
|
|
10 |
use Illuminate\Support\Arr;
|
|
|
11 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
12 |
|
|
|
13 |
#[AsCommand(name: 'db:show')]
|
|
|
14 |
class ShowCommand extends DatabaseInspectionCommand
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* The name and signature of the console command.
|
|
|
18 |
*
|
|
|
19 |
* @var string
|
|
|
20 |
*/
|
|
|
21 |
protected $signature = 'db:show {--database= : The database connection}
|
|
|
22 |
{--json : Output the database information as JSON}
|
|
|
23 |
{--counts : Show the table row count <bg=red;options=bold> Note: This can be slow on large databases </>};
|
|
|
24 |
{--views : Show the database views <bg=red;options=bold> Note: This can be slow on large databases </>}';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* The console command description.
|
|
|
28 |
*
|
|
|
29 |
* @var string
|
|
|
30 |
*/
|
|
|
31 |
protected $description = 'Display information about the given database';
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Execute the console command.
|
|
|
35 |
*
|
|
|
36 |
* @param \Illuminate\Database\ConnectionResolverInterface $connections
|
|
|
37 |
* @return int
|
|
|
38 |
*/
|
|
|
39 |
public function handle(ConnectionResolverInterface $connections)
|
|
|
40 |
{
|
|
|
41 |
if (! $this->ensureDependenciesExist()) {
|
|
|
42 |
return 1;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$connection = $connections->connection($database = $this->input->getOption('database'));
|
|
|
46 |
|
|
|
47 |
$schema = $connection->getDoctrineSchemaManager();
|
|
|
48 |
|
|
|
49 |
$this->registerTypeMappings($schema->getDatabasePlatform());
|
|
|
50 |
|
|
|
51 |
$data = [
|
|
|
52 |
'platform' => [
|
|
|
53 |
'config' => $this->getConfigFromDatabase($database),
|
|
|
54 |
'name' => $this->getPlatformName($schema->getDatabasePlatform(), $database),
|
|
|
55 |
'open_connections' => $this->getConnectionCount($connection),
|
|
|
56 |
],
|
|
|
57 |
'tables' => $this->tables($connection, $schema),
|
|
|
58 |
];
|
|
|
59 |
|
|
|
60 |
if ($this->option('views')) {
|
|
|
61 |
$data['views'] = $this->collectViews($connection, $schema);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$this->display($data);
|
|
|
65 |
|
|
|
66 |
return 0;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Get information regarding the tables within the database.
|
|
|
71 |
*
|
|
|
72 |
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
|
73 |
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
|
|
|
74 |
* @return \Illuminate\Support\Collection
|
|
|
75 |
*/
|
|
|
76 |
protected function tables(ConnectionInterface $connection, AbstractSchemaManager $schema)
|
|
|
77 |
{
|
|
|
78 |
return collect($schema->listTables())->map(fn (Table $table, $index) => [
|
|
|
79 |
'table' => $table->getName(),
|
|
|
80 |
'size' => $this->getTableSize($connection, $table->getName()),
|
|
|
81 |
'rows' => $this->option('counts') ? $connection->table($table->getName())->count() : null,
|
|
|
82 |
'engine' => rescue(fn () => $table->getOption('engine'), null, false),
|
|
|
83 |
'comment' => $table->getComment(),
|
|
|
84 |
]);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Get information regarding the views within the database.
|
|
|
89 |
*
|
|
|
90 |
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
|
91 |
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
|
|
|
92 |
* @return \Illuminate\Support\Collection
|
|
|
93 |
*/
|
|
|
94 |
protected function collectViews(ConnectionInterface $connection, AbstractSchemaManager $schema)
|
|
|
95 |
{
|
|
|
96 |
return collect($schema->listViews())
|
|
|
97 |
->reject(fn (View $view) => str($view->getName())
|
|
|
98 |
->startsWith(['pg_catalog', 'information_schema', 'spt_']))
|
|
|
99 |
->map(fn (View $view) => [
|
|
|
100 |
'view' => $view->getName(),
|
|
|
101 |
'rows' => $connection->table($view->getName())->count(),
|
|
|
102 |
]);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Render the database information.
|
|
|
107 |
*
|
|
|
108 |
* @param array $data
|
|
|
109 |
* @return void
|
|
|
110 |
*/
|
|
|
111 |
protected function display(array $data)
|
|
|
112 |
{
|
|
|
113 |
$this->option('json') ? $this->displayJson($data) : $this->displayForCli($data);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Render the database information as JSON.
|
|
|
118 |
*
|
|
|
119 |
* @param array $data
|
|
|
120 |
* @return void
|
|
|
121 |
*/
|
|
|
122 |
protected function displayJson(array $data)
|
|
|
123 |
{
|
|
|
124 |
$this->output->writeln(json_encode($data));
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Render the database information formatted for the CLI.
|
|
|
129 |
*
|
|
|
130 |
* @param array $data
|
|
|
131 |
* @return void
|
|
|
132 |
*/
|
|
|
133 |
protected function displayForCli(array $data)
|
|
|
134 |
{
|
|
|
135 |
$platform = $data['platform'];
|
|
|
136 |
$tables = $data['tables'];
|
|
|
137 |
$views = $data['views'] ?? null;
|
|
|
138 |
|
|
|
139 |
$this->newLine();
|
|
|
140 |
|
|
|
141 |
$this->components->twoColumnDetail('<fg=green;options=bold>'.$platform['name'].'</>');
|
|
|
142 |
$this->components->twoColumnDetail('Database', Arr::get($platform['config'], 'database'));
|
|
|
143 |
$this->components->twoColumnDetail('Host', Arr::get($platform['config'], 'host'));
|
|
|
144 |
$this->components->twoColumnDetail('Port', Arr::get($platform['config'], 'port'));
|
|
|
145 |
$this->components->twoColumnDetail('Username', Arr::get($platform['config'], 'username'));
|
|
|
146 |
$this->components->twoColumnDetail('URL', Arr::get($platform['config'], 'url'));
|
|
|
147 |
$this->components->twoColumnDetail('Open Connections', $platform['open_connections']);
|
|
|
148 |
$this->components->twoColumnDetail('Tables', $tables->count());
|
|
|
149 |
|
|
|
150 |
if ($tableSizeSum = $tables->sum('size')) {
|
|
|
151 |
$this->components->twoColumnDetail('Total Size', number_format($tableSizeSum / 1024 / 1024, 2).'MiB');
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
$this->newLine();
|
|
|
155 |
|
|
|
156 |
if ($tables->isNotEmpty()) {
|
|
|
157 |
$this->components->twoColumnDetail('<fg=green;options=bold>Table</>', 'Size (MiB)'.($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>Rows</>' : ''));
|
|
|
158 |
|
|
|
159 |
$tables->each(function ($table) {
|
|
|
160 |
if ($tableSize = $table['size']) {
|
|
|
161 |
$tableSize = number_format($tableSize / 1024 / 1024, 2);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
$this->components->twoColumnDetail(
|
|
|
165 |
$table['table'].($this->output->isVerbose() ? ' <fg=gray>'.$table['engine'].'</>' : null),
|
|
|
166 |
($tableSize ? $tableSize : '—').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.number_format($table['rows']).'</>' : '')
|
|
|
167 |
);
|
|
|
168 |
|
|
|
169 |
if ($this->output->isVerbose()) {
|
|
|
170 |
if ($table['comment']) {
|
|
|
171 |
$this->components->bulletList([
|
|
|
172 |
$table['comment'],
|
|
|
173 |
]);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
});
|
|
|
177 |
|
|
|
178 |
$this->newLine();
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
if ($views && $views->isNotEmpty()) {
|
|
|
182 |
$this->components->twoColumnDetail('<fg=green;options=bold>View</>', '<fg=green;options=bold>Rows</>');
|
|
|
183 |
|
|
|
184 |
$views->each(fn ($view) => $this->components->twoColumnDetail($view['view'], number_format($view['rows'])));
|
|
|
185 |
|
|
|
186 |
$this->newLine();
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
}
|