0

Abstract Class Yiisoft\Db\Migration\Command\DatabaseCommand

InheritanceYiisoft\Db\Migration\Command\DatabaseCommand » Symfony\Component\Console\Command\Command
SubclassesYiisoft\Db\Migration\Command\CreateCommand, Yiisoft\Db\Migration\Command\DownCommand, Yiisoft\Db\Migration\Command\HistoryCommand, Yiisoft\Db\Migration\Command\MarkCommand, Yiisoft\Db\Migration\Command\NewCommand, Yiisoft\Db\Migration\Command\RedoCommand, Yiisoft\Db\Migration\Command\UpdateCommand

Protected Methods

Hide inherited methods

Method Description Defined By
createContext() Creates the command context for a named database. Yiisoft\Db\Migration\Command\DatabaseCommand
createDefaultContext() Creates the command context for the default database from the command dependencies. Yiisoft\Db\Migration\Command\DatabaseCommand
execute() Yiisoft\Db\Migration\Command\DatabaseCommand
executeForDatabase() Executes the command for a single database. Yiisoft\Db\Migration\Command\DatabaseCommand
getAvailableDatabaseNames() Yiisoft\Db\Migration\Command\DatabaseCommand
isMultipleDatabases() Yiisoft\Db\Migration\Command\DatabaseCommand
resolveDatabaseNamesWithMissingOption() Resolves the database names or returns a message indicating that an option is missing if multiple databases are configured. Yiisoft\Db\Migration\Command\DatabaseCommand

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( ?\Yiisoft\Db\Migration\DatabaseRegistry $additionalDatabases = null )
$additionalDatabases ?\Yiisoft\Db\Migration\DatabaseRegistry

                public function __construct(private readonly ?DatabaseRegistry $additionalDatabases = null)
{
    parent::__construct();
    $this->addOption('db', null, InputOption::VALUE_REQUIRED, 'Database configuration to use for migrations.');
}

            
createContext() protected abstract method

Creates the command context for a named database.

protected abstract object createContext ( Yiisoft\Db\Migration\DatabaseSetup $setup )
$setup Yiisoft\Db\Migration\DatabaseSetup

                abstract protected function createContext(DatabaseSetup $setup): object;

            
createDefaultContext() protected abstract method

Creates the command context for the default database from the command dependencies.

protected abstract object createDefaultContext ( )

                abstract protected function createDefaultContext(): object;

            
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 abstract method

Executes the command for a single database.

protected abstract integer|null 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
return integer|null

The command exit status, or null if the database has an empty result. The command fails if all processed databases have empty results.

                abstract protected function executeForDatabase(
    InputInterface $input,
    OutputInterface $output,
    object $context,
): ?int;

            
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

Resolves the database names or returns a message indicating that an option is missing if multiple databases are configured.

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

The input interface containing the command options and arguments.

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