Final Class Yiisoft\Db\Migration\Command\NewCommand
| Inheritance | Yiisoft\ |
|---|
Displays not yet applied migrations.
This command will show the new migrations that have not been applied yet.
For example:
./yii migrate:new # first 10 new migrations
./yii migrate:new --limit=5 # first 5 new migrations
./yii migrate:new --all # all new migrations
./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations # new migrations from the directory
./yii migrate:new --namespace=Yiisoft\\Rbac\\Db\\Migrations # new migrations from the namespace
# new migrations from multiple directories and namespaces
./yii migrate:new --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
./yii migrate:new --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\ | ||
| $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 new migrations.')
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to display.')
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to display.');
}
| 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();
/** @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);
}
$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->migrationService->getNewMigrations();
if (empty($migrations)) {
$io->warning('No new migrations found. Your system is up-to-date.');
return null;
}
$countMigrations = count($migrations);
$migrationWord = $countMigrations === 1 ? 'migration' : 'migrations';
if ($limit !== null && $countMigrations > $limit) {
$migrations = array_slice($migrations, 0, $limit);
$io->warning("Showing $limit out of $countMigrations new $migrationWord:\n" );
} else {
$io->section("Found $countMigrations new $migrationWord:");
}
foreach ($migrations as $i => $migration) {
$output->writeln("<info>\t" . ($i + 1) . ". $migration</info>");
}
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.