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

58 lines
2.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 Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\ArrayInput;
class MigrateRefreshCommand extends Command
{
protected static $defaultName = 'migrate:refresh';
protected static $defaultDescription = 'Reset and re-run all migrations';
protected function configure(): void
{
$this
->addOption('step', 's', InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted', 0)
->addOption('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed command should be re-run');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$step = (int) $input->getOption('step');
if ($step > 0) {
$output->writeln("<info>Revertendo {$step} passos de migração...</info>");
$rollbackCommand = $this->getApplication()->find('migrate:rollback');
$rollbackInput = new ArrayInput(['--step' => $step]);
$rollbackCommand->run($rollbackInput, $output);
} else {
$output->writeln('<info>Revertendo todas as migrações (reset)...</info>');
// Reset all by calling rollback in loop or a specific reset command
// Assuming we'll use rollback in loop for simplicity or adding a reset later
// Here we use step=999 as a convention for "all" in our rollback implementation
$rollbackCommand = $this->getApplication()->find('migrate:rollback');
$rollbackInput = new ArrayInput(['--step' => 999]);
$rollbackCommand->run($rollbackInput, $output);
}
$output->writeln('<info>Executando migrações novamente...</info>');
$migrateCommand = $this->getApplication()->find('migrate');
$migrateInput = new ArrayInput([]);
$migrateCommand->run($migrateInput, $output);
if ($input->getOption('seed')) {
$output->writeln('<info>Executando seeders...</info>');
$seedCommand = $this->getApplication()->find('db:seed');
$seedInput = new ArrayInput([]);
$seedCommand->run($seedInput, $output);
}
$output->writeln('<info>Operação migrate:refresh concluída com sucesso!</info>');
return Command::SUCCESS;
}
}