0

Final Class Yiisoft\Db\Migration\Command\UpdateCommand

InheritanceYiisoft\Db\Migration\Command\UpdateCommand » Yiisoft\Db\Migration\Command\DatabaseCommand » Symfony\Component\Console\Command\Command

Applies new migrations.

For example,

./yii migrate:up                                           # apply all new migrations
./yii migrate:up --limit=3                                 # apply the first 3 new migrations
./yii migrate:up --path=@vendor/yiisoft/rbac-db/migrations # apply new migrations from the directory
./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations # apply new migrations from the namespace

# apply new migrations from multiple directories and namespaces
./yii migrate:up --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
./yii migrate:up --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\Migration\Runner\UpdateRunner $updateRunner, Yiisoft\Db\Migration\Service\MigrationService $migrationService, Yiisoft\Db\Migration\Migrator $migrator, ?\Yiisoft\Db\Migration\DatabaseRegistry $additionalDatabases = null )
$updateRunner Yiisoft\Db\Migration\Runner\UpdateRunner
$migrationService Yiisoft\Db\Migration\Service\MigrationService
$migrator Yiisoft\Db\Migration\Migrator
$additionalDatabases ?\Yiisoft\Db\Migration\DatabaseRegistry

                public function __construct(
    private readonly UpdateRunner $updateRunner,
    private readonly MigrationService $migrationService,
    private readonly Migrator $migrator,
    ?DatabaseRegistry $additionalDatabases = null,
) {
    parent::__construct($additionalDatabases);
}

            
configure() protected method

protected void configure ( )

                protected function configure(): void
{
    $this
        ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to apply.')
        ->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to apply.')
        ->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to apply.')
        ->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.');
}

            
createContext() protected method

protected Yiisoft\Db\Migration\Command\Context\UpdateContext createContext ( Yiisoft\Db\Migration\DatabaseSetup $setup )
$setup Yiisoft\Db\Migration\DatabaseSetup

                protected function createContext(DatabaseSetup $setup): UpdateContext
{
    return new UpdateContext(
        $setup->migrator,
        $setup->migrationService,
        new UpdateRunner($setup->migrator),
    );
}

            
createDefaultContext() protected method

protected Yiisoft\Db\Migration\Command\Context\UpdateContext createDefaultContext ( )

                protected function createDefaultContext(): UpdateContext
{
    return new UpdateContext($this->migrator, $this->migrationService, $this->updateRunner);
}

            
execute() protected method
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

                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;
}

            
executeForDatabase() protected method

protected integer executeForDatabase ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output, object $context )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface
$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->updateRunner->setIo($io);
    $context->migrationService->databaseConnection();
    /** @var string[] $paths */
    $paths = $input->getOption('path');
    /** @var string[] $namespaces */
    $namespaces = $input->getOption('namespace');
    if (!empty($paths) || !empty($namespaces)) {
        $context->migrationService->setSourcePaths($paths);
        $context->migrationService->setSourceNamespaces($namespaces);
    }
    if ($context->migrationService->before($this->getName() ?? '') === Command::INVALID) {
        return Command::INVALID;
    }
    $limit = $input->getOption('limit');
    if ($limit !== null) {
        $limit = (int) $limit;
        if ($limit <= 0) {
            $io->error('The limit option must be greater than 0.');
            return Command::INVALID;
        }
    }
    $migrations = $context->migrationService->getNewMigrations();
    if (empty($migrations)) {
        $output->writeln("<fg=green>No new migrations found.</>\n");
        $io->success('Your system is up-to-date.');
        return Command::SUCCESS;
    }
    $migrationsCount = count($migrations);
    $migrationWord = $migrationsCount === 1 ? 'migration' : 'migrations';
    if ($limit !== null && $migrationsCount > $limit) {
        $migrations = array_slice($migrations, 0, $limit);
        $output->writeln("<fg=yellow>Total $limit out of $migrationsCount new $migrationWord to be applied:</>\n");
    } else {
        $output->writeln("<fg=yellow>Total $migrationsCount new $migrationWord to be applied:</>\n");
    }
    foreach ($migrations as $i => $migration) {
        $nameLimit = $context->migrator->getMigrationNameLimit();
        if (strlen($migration) > $nameLimit) {
            $output->writeln(
                "\n<fg=red>The migration name '$migration' is too long. Its not possible to apply "
                . 'this migration.</>',
            );
            return Command::INVALID;
        }
        $output->writeln("\t<fg=yellow>" . ($i + 1) . ". $migration</>");
    }
    if ($input->getOption('force-yes') || $io->confirm("Apply the above $migrationWord?")) {
        $instances = $context->migrationService->makeMigrations($migrations);
        $migrationWas = ($migrationsCount === 1 ? 'migration was' : 'migrations were');
        foreach ($instances as $i => $instance) {
            try {
                $context->updateRunner->run($instance, $i + 1);
            } catch (Throwable $e) {
                $output->writeln("\n<fg=yellow>Total $i out of $migrationsCount new $migrationWas applied.</>\n");
                $io->error($i > 0 ? 'Partially updated.' : 'Not updated.');
                throw $e;
            }
        }
        $output->writeln("\n<fg=green>Total $migrationsCount new $migrationWas applied.</>\n");
        $io->success('Updated successfully.');
    }
    return Command::SUCCESS;
}

            
getAvailableDatabaseNames() protected method
protected string[] getAvailableDatabaseNames ( )

                protected function getAvailableDatabaseNames(): array
{
    return [DatabaseRegistry::DEFAULT_DATABASE, ...($this->additionalDatabases?->getNames() ?? [])];
}

            
isMultipleDatabases() protected method
protected boolean isMultipleDatabases ( )

                protected function isMultipleDatabases(): bool
{
    return $this->additionalDatabases !== null && $this->additionalDatabases->getNames() !== [];
}

            
resolveDatabaseNamesWithMissingOption() protected method

protected array|string resolveDatabaseNamesWithMissingOption ( \Symfony\Component\Console\Input\InputInterface $input )
$input \Symfony\Component\Console\Input\InputInterface

                protected function resolveDatabaseNamesWithMissingOption(InputInterface $input): array|string
{
    if (!$this->isMultipleDatabases()) {
        return [DatabaseRegistry::DEFAULT_DATABASE];
    }
    return $input->getOption('path') !== [] || $input->getOption('namespace') !== []
        ? 'The --db option is required with --path or --namespace when multiple databases are configured.'
        : $this->getAvailableDatabaseNames();
}