Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ListSchemas
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 run
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Dev Commands Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\CLI\Commands;
11
12use Framework\CLI\CLI;
13use Framework\Debug\Debugger;
14
15/**
16 * Class ListSchemas.
17 *
18 * @package dev-commands
19 */
20class ListSchemas extends DatabaseCommand
21{
22    protected string $description = 'Lists database schemas.';
23
24    public function run() : void
25    {
26        $this->setDatabase();
27        $sql = 'SELECT `SCHEMA_NAME` AS `schema`,
28`DEFAULT_COLLATION_NAME` AS `collation`
29FROM `information_schema`.`SCHEMATA`
30ORDER BY `SCHEMA_NAME`';
31        $schemas = $this->getDatabase()->query($sql)->fetchArrayAll();
32        $sql = 'SELECT `TABLE_SCHEMA` AS `schema`,
33SUM(`DATA_LENGTH` + `INDEX_LENGTH`) AS `size`,
34COUNT(DISTINCT CONCAT(`TABLE_SCHEMA`, ".", `TABLE_NAME`)) AS `tables`
35FROM `information_schema`.`TABLES`
36GROUP BY `TABLE_SCHEMA`';
37        $infos = $this->getDatabase()->query($sql)->fetchArrayAll();
38        foreach ($schemas as &$schema) {
39            $schema['size'] = $schema['tables'] = 0;
40            foreach ($infos as $info) {
41                if ($info['schema'] === $schema['schema']) {
42                    $schema['tables'] = $info['tables'];
43                    $schema['size'] = Debugger::convertSize((int) $info['size']);
44                    break;
45                }
46            }
47        }
48        unset($schema);
49        CLI::table($schemas, [
50            'Schema',
51            'Collation',
52            'Tables',
53            'Size',
54        ]);
55        CLI::write('Total: ' . \count($schemas));
56    }
57}