Final Class Yiisoft\Db\Migration\Command\HistoryCommand
| Inheritance | Yiisoft\Db\Migration\Command\HistoryCommand » Symfony\Component\Console\Command\Command |
|---|
Displays the migration history.
This command will show the list of migrations that have been applied so far.
For example:
./yii migrate:history # last 10 migrations
./yii migrate:history --limit=5 # last 5 migrations
./yii migrate:history --all # whole history
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Migration\Command\HistoryCommand |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| configure() | Yiisoft\Db\Migration\Command\HistoryCommand | |
| execute() | Yiisoft\Db\Migration\Command\HistoryCommand |
Method Details
| public mixed __construct ( Yiisoft\Db\Migration\Service\MigrationService $migrationService, Yiisoft\Db\Migration\Migrator $migrator ) | ||
| $migrationService | Yiisoft\Db\Migration\Service\MigrationService | |
| $migrator | Yiisoft\Db\Migration\Migrator | |
public function __construct(
private readonly MigrationService $migrationService,
private readonly Migrator $migrator,
) {
parent::__construct();
}
| protected void configure ( ) |
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to display.', 10)
->addOption('all', 'a', InputOption::VALUE_NONE, 'All migrations.');
}
| protected integer execute ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output ) | ||
| $input | \Symfony\Component\Console\Input\InputInterface | |
| $output | \Symfony\Component\Console\Output\OutputInterface | |
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
$this->migrationService->before($this->getName() ?? '');
$limit = !$input->getOption('all')
? (int) $input->getOption('limit')
: null;
if ($limit !== null && $limit <= 0) {
$io->error('The limit option must be greater than 0.');
$this->migrationService->databaseConnection();
return Command::INVALID;
}
$migrations = $this->migrator->getHistory($limit);
if (empty($migrations)) {
$io->warning('No migration has been done before.');
return Command::FAILURE;
}
$countMigrations = count($migrations);
if ($limit === $countMigrations) {
$io->section("Last $countMigrations applied " . ($countMigrations === 1 ? 'migration' : 'migrations') . ':');
} else {
$io->section("Total $countMigrations " . ($countMigrations === 1 ? 'migration has' : 'migrations have') . ' been applied before:');
}
foreach ($migrations as $version => $time) {
$output->writeln("\t<info>(" . date('Y-m-d H:i:s', (int) $time) . ') ' . $version . '</info>');
}
$output->writeln("\n");
$io->success('Success.');
$this->migrationService->databaseConnection();
return Command::SUCCESS;
}
Signup or Login in order to comment.