Final Class Yiisoft\Db\Migration\Command\NewCommand
| Inheritance | Yiisoft\Db\Migration\Command\NewCommand » 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
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Migration\Command\NewCommand |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| configure() | Yiisoft\Db\Migration\Command\NewCommand | |
| execute() | Yiisoft\Db\Migration\Command\NewCommand |
Method Details
| public mixed __construct ( Yiisoft\Db\Migration\Service\MigrationService $migrationService, Yiisoft\Db\Migration\Migrator $migrator ) | ||
| $migrationService | Yiisoft\Db\Migration\Service\MigrationService | |
| $migrator | Yiisoft\Db\Migration\Migrator | |
public function __construct(
private readonly MigrationService $migrationService,
private readonly Migrator $migrator,
) {
parent::__construct();
}
| 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 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 | |
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
/** @var string[] $paths */
$paths = $input->getOption('path');
/** @var string[] $namespaces */
$namespaces = $input->getOption('namespace');
if (!empty($paths) || !empty($namespaces)) {
$this->migrationService->setSourcePaths($paths);
$this->migrationService->setSourceNamespaces($namespaces);
}
$this->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.');
$this->migrationService->databaseConnection();
return Command::INVALID;
}
$migrations = $this->migrationService->getNewMigrations();
if (empty($migrations)) {
$io->warning('No new migrations found. Your system is up-to-date.');
$this->migrationService->databaseConnection();
return Command::FAILURE;
}
$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>");
}
$this->migrationService->databaseConnection();
return Command::SUCCESS;
}
Signup or Login in order to comment.