0 follower

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

InheritanceYiisoft\Yii\Cycle\Command\Migration\DownCommand » 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, \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher )
$promise Yiisoft\Yii\Cycle\Command\CycleDependencyProxy
$eventDispatcher \Psr\EventDispatcher\EventDispatcherInterface

                public function __construct(CycleDependencyProxy $promise, private readonly EventDispatcherInterface $eventDispatcher)
{
    parent::__construct($promise);
}

            
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
{
    $migrations = $this->findMigrations($output);
    // check any executed migration
    foreach (array_reverse($migrations) as $migration) {
        if ($migration->getState()->getStatus() === State::STATUS_EXECUTED) {
            break;
        }
    }
    if (!isset($migration)) {
        $output->writeln('<fg=red>No migration found for rollback</>');
        return self::SUCCESS;
    }
    $migrator = $this->promise->getMigrator();
    // Confirm
    if (!$migrator->getConfig()->isSafe()) {
        $output->writeln('<fg=yellow>Migration to be reverted:</>');
        $output->writeln('— <fg=cyan>' . $migration->getState()->getName() . '</>');
        if ($input->isInteractive()) {
            /** @var QuestionHelper $qaHelper */
            $qaHelper = $this->getHelper('question');
            $question = new ConfirmationQuestion('Revert the above migration? (yes|no) ', false);
            if (!$qaHelper->ask($input, $output, $question)) {
                return self::SUCCESS;
            }
        }
    }
    $this->eventDispatcher->dispatch(new BeforeMigrate());
    try {
        $migration = $migrator->rollback();
        if (!$migration instanceof MigrationInterface) {
            throw new \Exception('Migration not found');
        }
        $state = $migration->getState();
        $status = $state->getStatus();
        $output->writeln(
            sprintf('<fg=cyan>%s</>: %s', $state->getName(), self::MIGRATION_STATUS[$status] ?? $status)
        );
    } finally {
        $this->eventDispatcher->dispatch(new AfterMigrate());
    }
    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;
}