Final Class Yiisoft\Db\Migration\Command\UpdateCommand
| Inheritance | Yiisoft\ |
|---|
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
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
Protected Methods
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $updateRunner | Yiisoft\ |
|
| $migrationService | Yiisoft\ |
|
| $migrator | Yiisoft\ |
|
| $additionalDatabases | ?\ |
|
public function __construct(
private readonly UpdateRunner $updateRunner,
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 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.');
}
| protected Yiisoft\ | ||
| $setup | Yiisoft\ |
|
protected function createContext(DatabaseSetup $setup): UpdateContext
{
return new UpdateContext(
$setup->migrator,
$setup->migrationService,
new UpdateRunner($setup->migrator),
);
}
| protected Yiisoft\ |
protected function createDefaultContext(): UpdateContext
{
return new UpdateContext($this->migrator, $this->migrationService, $this->updateRunner);
}
| 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 integer 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->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;
}
| 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|string resolveDatabaseNamesWithMissingOption ( \ | ||
| $input | \ |
|
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();
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.