Files
dockerfluxodecaixapro/app/Console/Commands/DbListCommand.php
T
2026-09-01 10:46:29 -03:00

46 lines
1.4 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Database\Capsule\Manager as Capsule;
use Symfony\Component\Console\Helper\Table;
class DbListCommand extends Command
{
protected static $defaultName = 'db:list';
protected static $defaultDescription = 'Lista todas as tabelas do banco de dados';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$connection = Capsule::connection();
$dbName = $connection->getDatabaseName();
$output->writeln("<info>Tabelas no banco de dados: {$dbName}</info>");
$tables = $connection->select('SHOW TABLES');
$key = "Tables_in_{$dbName}";
if (empty($tables)) {
$output->writeln('<info>Nenhuma tabela encontrada no banco de dados.</info>');
return Command::SUCCESS;
}
$table = new Table($output);
$table->setHeaders(['Tabela', 'Total de Registros']);
foreach ($tables as $t) {
$tableName = $t->$key;
$count = $connection->table($tableName)->count();
$table->addRow([$tableName, $count]);
}
$table->render();
$output->writeln("<info>Total de tabelas: " . count($tables) . "</info>");
return Command::SUCCESS;
}
}