Abstract Class Yiisoft\Db\Migration\Command\DatabaseCommand
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| createContext() | Creates the command context for a named database. | Yiisoft\ |
| createDefaultContext() | Creates the command context for the default database from the command dependencies. | Yiisoft\ |
| execute() | Yiisoft\ |
|
| executeForDatabase() | Executes the command for a single database. | Yiisoft\ |
| getAvailableDatabaseNames() | Yiisoft\ |
|
| isMultipleDatabases() | Yiisoft\ |
|
| resolveDatabaseNamesWithMissingOption() | Resolves the database names or returns a message indicating that an option is missing if multiple databases are configured. | Yiisoft\ |
Method Details
| public mixed __construct ( ?\ | ||
| $additionalDatabases | ?\ |
|
public function __construct(private readonly ?DatabaseRegistry $additionalDatabases = null)
{
parent::__construct();
$this->addOption('db', null, InputOption::VALUE_REQUIRED, 'Database configuration to use for migrations.');
}
Creates the command context for a named database.
| protected abstract object createContext ( Yiisoft\ | ||
| $setup | Yiisoft\ |
|
abstract protected function createContext(DatabaseSetup $setup): object;
Creates the command context for the default database from the command dependencies.
| protected abstract object createDefaultContext ( ) |
abstract protected function createDefaultContext(): object;
| 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;
}
Executes the command for a single database.
| protected abstract integer|null executeForDatabase ( \ | ||
| $input | \ |
|
| $output | \ |
|
| $context | object | |
| return | integer|null |
The command exit status, or |
|---|---|---|
abstract protected function executeForDatabase(
InputInterface $input,
OutputInterface $output,
object $context,
): ?int;
| 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() !== [];
}
Resolves the database names or returns a message indicating that an option is missing if multiple databases are configured.
| protected string[]|string resolveDatabaseNamesWithMissingOption ( \ | ||
| $input | \ |
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.';
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.