0 follower

Final Class Yiisoft\Yii\Cycle\Command\Migration\GenerateCommand

InheritanceYiisoft\Yii\Cycle\Command\Migration\GenerateCommand » Yiisoft\Yii\Cycle\Command\Migration\BaseMigrationCommand » Symfony\Component\Console\Command\Command

Constants

Hide inherited constants

Constant Value Description Defined By
MIGRATION_STATUS [ \Cycle\Migrations\State::STATUS_UNDEFINED => 'undefined', \Cycle\Migrations\State::STATUS_PENDING => 'pending', \Cycle\Migrations\State::STATUS_EXECUTED => 'executed', ] Yiisoft\Yii\Cycle\Command\Migration\BaseMigrationCommand

Method Details

Hide inherited methods

__construct() public method
public mixed __construct ( Yiisoft\Yii\Cycle\Command\CycleDependencyProxy $promise )
$promise Yiisoft\Yii\Cycle\Command\CycleDependencyProxy

                public function __construct(protected CycleDependencyProxy $promise)
{
    parent::__construct();
}

            
createEmptyMigration() protected method
protected \Cycle\Schema\Generator\Migrations\MigrationImage|null createEmptyMigration ( \Symfony\Component\Console\Output\OutputInterface $output, string $name, string|null $database null )
$output \Symfony\Component\Console\Output\OutputInterface
$name string
$database string|null

                protected function createEmptyMigration(
    OutputInterface $output,
    string $name,
    ?string $database = null
): ?MigrationImage {
    if ($database === null) {
        // get default database
        $database = $this->promise->getDatabaseProvider()->database()->getName();
    }
    $migrator = $this->promise->getMigrator();
    $migrationSkeleton = new MigrationImage($this->promise->getMigrationConfig(), $database);
    $migrationSkeleton->setName($name);
    $className = $migrationSkeleton->getClass()->getName();
    \assert($className !== null);
    try {
        $migrationFile = $migrator->getRepository()->registerMigration(
            $migrationSkeleton->buildFileName(),
            $className,
            $migrationSkeleton->getFile()->render()
        );
    } catch (RepositoryException $e) {
        $output->writeln('<fg=yellow>Can not create migration</>');
        /**
         * @infection-ignore-all
         * ConcatOperandRemoval
         * Removing closing tag works (probably it's automatically fixed by formatter), but makes no sense.
         */
        $output->writeln('<fg=red>' . $e->getMessage() . '</>');
        return null;
    }
    $output->writeln('<info>New migration file has been created</info>');
    $output->writeln("<fg=cyan>$migrationFile</>");
    return $migrationSkeleton;
}

            
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

                #[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
    $migrator = $this->promise->getMigrator();
    // check existing unapplied migrations
    $listAfter = $migrator->getMigrations();
    foreach ($listAfter as $migration) {
        if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
            $output->writeln('<fg=red>Outstanding migrations found, run `migrate:up` first.</>');
            return self::SUCCESS;
        }
    }
    $conveyor = $this->promise->getSchemaConveyor();
    // migrations generator
    $conveyor->addGenerator(
        SchemaConveyorInterface::STAGE_USERLAND,
        new GenerateMigrations($migrator->getRepository(), $this->promise->getMigrationConfig())
    );
    // show DB changes
    $conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, new PrintChanges($output));
    // compile schema and convert diffs to new migrations
    (new Compiler())->compile(new Registry($this->promise->getDatabaseProvider()), $conveyor->getGenerators());
    // compare migrations list before and after
    $listBefore = $migrator->getMigrations();
    $added = count($listBefore) - count($listAfter);
    $output->writeln("<info>Added {$added} file(s)</info>");
    // print added migrations
    if ($added > 0) {
        foreach ($listBefore as $migration) {
            if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
                $output->writeln($migration->getState()->getName());
            }
        }
    } else {
        $output->writeln(
            '<info>If you want to create new empty migration, use <fg=yellow>migrate:create</></info>'
        );
        if ($input->isInteractive() && $input instanceof StreamableInputInterface) {
            /** @var QuestionHelper $qaHelper */
            $qaHelper = $this->getHelper('question');
            $question = new ConfirmationQuestion('Would you like to create empty migration right now? (Y/n)', true);
            $answer = $qaHelper->ask($input, $output, $question);
            if (!$answer) {
                return self::SUCCESS;
            }
            // get the name for a new migration
            $question = new Question('Please enter an unique name for the new migration: ');
            $name = $qaHelper->ask($input, $output, $question);
            if (empty($name)) {
                $output->writeln('<fg=red>You entered an empty name. Exit</>');
                return self::SUCCESS;
            }
            // create an empty migration
            $this->createEmptyMigration($output, $name);
        }
    }
    return self::SUCCESS;
}

            
findMigrations() protected method
protected \Cycle\Migrations\MigrationInterface[] findMigrations ( \Symfony\Component\Console\Output\OutputInterface $output )
$output \Symfony\Component\Console\Output\OutputInterface

                protected function findMigrations(OutputInterface $output): array
{
    $list = $this->promise->getMigrator()->getMigrations();
    $output->writeln(
        sprintf(
            '<info>Total %d migration(s) found in %s</info>',
            count($list),
            $this->promise->getMigrationConfig()->getDirectory()
        )
    );
    return $list;
}