41 lines
1.3 KiB
PHP
Executable File
41 lines
1.3 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;
|
|
|
|
class DbOptimizeCommand extends Command
|
|
{
|
|
protected static $defaultName = 'db:optimize';
|
|
protected static $defaultDescription = 'Otimiza as tabelas do banco de dados';
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$connection = Capsule::connection();
|
|
$dbName = $connection->getDatabaseName();
|
|
|
|
$output->writeln("<info>Otimizando banco de dados: {$dbName}</info>");
|
|
|
|
$tables = $connection->select('SHOW TABLES');
|
|
$key = "Tables_in_{$dbName}";
|
|
|
|
foreach ($tables as $table) {
|
|
$tableName = $table->$key;
|
|
$output->write("Otimizando tabela: <comment>{$tableName}</comment>... ");
|
|
|
|
try {
|
|
$connection->statement("OPTIMIZE TABLE {$tableName}");
|
|
$output->writeln('<info>OK</info>');
|
|
} catch (\Exception $e) {
|
|
$output->writeln('<error>Erro ao otimizar</error>');
|
|
}
|
|
}
|
|
|
|
$output->writeln('<info>Otimização concluída!</info>');
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|