0 follower

Final Class Yiisoft\Db\Migration\Command\CreateCommand

InheritanceYiisoft\Db\Migration\Command\CreateCommand » Symfony\Component\Console\Command\Command

Creates a new migration.

This command creates a new migration using the available migration template.

To use it, configure migrations paths (newMigrationPath and sourcePaths) in params.php file, in your application.

'yiisoft/db-migration' => [
    'newMigrationNamespace' => '',
    'newMigrationPath' => '',
    'sourceNamespaces' => [],
    'sourcePaths' => [],
],

After using this command, developers should modify the created migration skeleton by filling up the actual migration logic.

./yii migrate:create table --command=table

To generate a namespaced migration, you should specify a namespace before the migration's name.

Note that backslash (\) is usually considered a special character in the shell, so you need to escape it properly to avoid shell errors or incorrect behavior.

For example:

./yii migrate:create post --command=table --namespace=Yiisoft\\Db\\Migration\\Migration
./yii migrate:create post --command=table --path=@root/migrations/blog

In case {@see \Yiisoft\Db\Migration\Service\MigrationService::$newMigrationPath} is not set, and no namespace is provided, {@see \Yiisoft\Db\Migration\Service\MigrationService::$newMigrationNamespace} will be used.

Constants

Hide inherited constants

Constant Value Description Defined By
AVAILABLE_COMMANDS [ 'create', 'table', 'dropTable', 'addColumn', 'dropColumn', 'junction', ] Yiisoft\Db\Migration\Command\CreateCommand

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\Migration\Service\Generate\CreateService $createService, Yiisoft\Db\Migration\Service\MigrationService $migrationService, Yiisoft\Db\Migration\Migrator $migrator )
$createService Yiisoft\Db\Migration\Service\Generate\CreateService
$migrationService Yiisoft\Db\Migration\Service\MigrationService
$migrator Yiisoft\Db\Migration\Migrator

                public function __construct(
    private readonly CreateService $createService,
    private readonly MigrationService $migrationService,
    private readonly Migrator $migrator,
) {
    parent::__construct();
}

            
configure() protected method

protected void configure ( )

                protected function configure(): void
{
    $this
        ->addArgument('name', InputArgument::REQUIRED, 'Table name to generate migration for.')
        ->addOption('command', 'c', InputOption::VALUE_REQUIRED, 'Command to execute.', 'create')
        ->addOption('fields', 'f', InputOption::VALUE_REQUIRED, 'Table fields to generate.')
        ->addOption('table-comment', null, InputOption::VALUE_REQUIRED, 'Table comment.')
        ->addOption('and', null, InputOption::VALUE_REQUIRED, 'And junction.')
        ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path to migration directory.')
        ->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED, 'Migration file namespace.')
        ->addOption('force-yes', 'y', InputOption::VALUE_NONE, 'Force yes to all questions.')
        ->setHelp('This command generates new migration file.');
}

            
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

                protected function execute(InputInterface $input, OutputInterface $output): int
{
    $io = new SymfonyStyle($input, $output);
    $this->migrator->setIo($io);
    $this->migrationService->setIo($io);
    $this->createService->setIo($io);
    /** @var string|null $path */
    $path = $input->getOption('path');
    /** @var string|null $namespace */
    $namespace = $input->getOption('namespace');
    if ($path !== null || $namespace !== null) {
        $this->migrationService->setNewMigrationPath((string) $path);
        $this->migrationService->setNewMigrationNamespace((string) $namespace);
    } else {
        $namespace = $this->migrationService->getNewMigrationNamespace();
    }
    if ($this->migrationService->before($this->getName() ?? '') === Command::INVALID) {
        return Command::INVALID;
    }
    /** @var string $table */
    $table = $input->getArgument('name');
    if (!preg_match('/^[\w\\\\]+$/', $table)) {
        $io->error(
            'The migration name should contain letters, digits, underscore and/or backslash characters only.',
        );
        return Command::INVALID;
    }
    /** @var string $command */
    $command = $input->getOption('command');
    if (!in_array($command, self::AVAILABLE_COMMANDS, true)) {
        $io->error(
            "Command not found \"$command\". Available commands: " . implode(', ', self::AVAILABLE_COMMANDS) . '.',
        );
        return Command::INVALID;
    }
    /** @var string|null $and */
    $and = $input->getOption('and');
    $name = $this->generateName($command, $table, $and);
    $className = $this->migrationService->generateClassName($name);
    $nameLimit = $this->migrator->getMigrationNameLimit();
    if ($nameLimit !== 0 && strlen($className) > $nameLimit) {
        $io->error('The migration name is too long.');
        return Command::INVALID;
    }
    $migrationPath = $this->migrationService->findMigrationPath();
    if (!is_dir($migrationPath)) {
        $io->error("Invalid path directory $migrationPath");
        return Command::INVALID;
    }
    if ($this->confirm($input, $output)) {
        /** @var string|null $fields */
        $fields = $input->getOption('fields');
        /** @var string|null $tableComment */
        $tableComment = $input->getOption('table-comment');
        $content = $this->createService->run(
            $command,
            $table,
            $className,
            $namespace,
            $fields,
            $and,
            $tableComment,
        );
        $file = $migrationPath . DIRECTORY_SEPARATOR . $className . '.php';
        file_put_contents($file, $content, LOCK_EX);
        $output->writeln("\n\t<info>$className</info>");
        $output->writeln("\n");
        $io->success('New migration created successfully.');
    }
    $this->migrationService->databaseConnection();
    return Command::SUCCESS;
}