Final Class Yiisoft\Db\Migration\Command\HistoryCommand
| Inheritance | Yiisoft\ |
|---|
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\ |
Protected Methods
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $migrationService | Yiisoft\ |
|
| $migrator | Yiisoft\ |
|
| $additionalDatabases | ?\ |
|
public function __construct(
private readonly MigrationService $migrationService,
private readonly Migrator $migrator,
?DatabaseRegistry $additionalDatabases = null,
) {
parent::__construct($additionalDatabases);
}
| 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 Yiisoft\ | ||
| $setup | Yiisoft\ |
|
protected function createContext(DatabaseSetup $setup): MigrationContext
{
return new MigrationContext($setup->migrator, $setup->migrationService);
}
| protected Yiisoft\ |
protected function createDefaultContext(): MigrationContext
{
return new MigrationContext($this->migrator, $this->migrationService);
}
| protected integer execute ( \ | ||
| $input | \ |
|
| $output | \ |
|
final protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$names = $this->resolveDatabaseNames($input);
if (is_string($names)) {
$io->error($names);
return Command::INVALID;
}
$multipleDatabases = $this->isMultipleDatabases();
$hasResults = false;
foreach ($names as $name) {
if (!$multipleDatabases || $name === DatabaseRegistry::DEFAULT_DATABASE) {
$context = $this->createDefaultContext();
} else {
/** @psalm-var DatabaseRegistry $this->additionalDatabases */
$context = $this->createContext($this->additionalDatabases->createSetup($name));
}
if ($multipleDatabases) {
$io->section('Database: ' . OutputFormatter::escape($name));
}
$status = $this->executeForDatabase($input, $output, $context);
if ($status === null) {
continue;
}
if ($status !== Command::SUCCESS) {
return $status;
}
$hasResults = true;
}
return $hasResults ? Command::SUCCESS : Command::FAILURE;
}
| protected ?int executeForDatabase ( \ | ||
| $input | \ |
|
| $output | \ |
|
| $context | object | |
protected function executeForDatabase(
InputInterface $input,
OutputInterface $output,
object $context,
): ?int {
$io = new SymfonyStyle($input, $output);
$context->migrator->setIo($io);
$context->migrationService->setIo($io);
$context->migrationService->databaseConnection();
$context->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.');
return Command::INVALID;
}
$migrations = $context->migrator->getHistory($limit);
if (empty($migrations)) {
$io->warning('No migration has been done before.');
return null;
}
$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.');
return Command::SUCCESS;
}
| protected string[] getAvailableDatabaseNames ( ) |
protected function getAvailableDatabaseNames(): array
{
return [DatabaseRegistry::DEFAULT_DATABASE, ...($this->additionalDatabases?->getNames() ?? [])];
}
| protected boolean isMultipleDatabases ( ) |
protected function isMultipleDatabases(): bool
{
return $this->additionalDatabases !== null && $this->additionalDatabases->getNames() !== [];
}
| protected array resolveDatabaseNamesWithMissingOption ( \ | ||
| $input | \ |
|
protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array
{
return $this->getAvailableDatabaseNames();
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.