0

Final Class Yiisoft\Db\Migration\Command\NewCommand

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

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

Method Details

Hide inherited methods

__construct() public method

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

                public function __construct(
    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 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.');
}

            
createContext() protected method

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

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

            
createDefaultContext() protected method

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

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

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

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