Completed
Push — master ( c28f13...ab7ebf )
by Asmir
21s queued 11s
created

ExecuteCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 119
ccs 64
cts 66
cp 0.9697
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 68 1
B execute() 0 44 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\Version\Direction;
8
use Doctrine\Migrations\Version\Version;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use function getcwd;
14
use function is_string;
15
use function is_writable;
16
17
/**
18
 * The ExecutCommand class is responsible for executing a single migration version up or down.
19
 */
20
class ExecuteCommand extends DoctrineCommand
21
{
22
    /** @var string */
23
    protected static $defaultName = 'migrations:execute';
24
25 4
    protected function configure() : void
26
    {
27
        $this
28 4
            ->setAliases(['execute'])
29 4
            ->setDescription(
30 4
                'Execute a single migration version up or down manually.'
31
            )
32 4
            ->addArgument(
33 4
                'version',
34 4
                InputArgument::REQUIRED,
35 4
                'The version to execute.',
36 4
                null
37
            )
38 4
            ->addOption(
39 4
                'write-sql',
40 4
                null,
41 4
                InputOption::VALUE_OPTIONAL,
42 4
                'The path to output the migration SQL file instead of executing it. Defaults to current working directory.',
43 4
                false
44
            )
45 4
            ->addOption(
46 4
                'dry-run',
47 4
                null,
48 4
                InputOption::VALUE_NONE,
49 4
                'Execute the migration as a dry run.'
50
            )
51 4
            ->addOption(
52 4
                'up',
53 4
                null,
54 4
                InputOption::VALUE_NONE,
55 4
                'Execute the migration up.'
56
            )
57 4
            ->addOption(
58 4
                'down',
59 4
                null,
60 4
                InputOption::VALUE_NONE,
61 4
                'Execute the migration down.'
62
            )
63 4
            ->addOption(
64 4
                'query-time',
65 4
                null,
66 4
                InputOption::VALUE_NONE,
67 4
                'Time all the queries individually.'
68
            )
69 4
            ->setHelp(<<<EOT
70 4
The <info>%command.name%</info> command executes a single migration version up or down manually:
71
72
    <info>%command.full_name% FQCN</info>
73
74
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
75
76
    <info>%command.full_name% FQCN --down</info>
77
78
You can also execute the migration as a <comment>--dry-run</comment>:
79
80
    <info>%command.full_name% FQCN --dry-run</info>
81
82
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>:
83
84
    <info>%command.full_name% FQCN --write-sql</info>
85
86
Or you can also execute the migration without a warning message which you need to interact with:
87
88
    <info>%command.full_name% FQCN --no-interaction</info>
89
EOT
90
        );
91
92 4
        parent::configure();
93 4
    }
94
95 4
    public function execute(InputInterface $input, OutputInterface $output) : ?int
96
    {
97 4
        $version   = $input->getArgument('version');
98 4
        $path      = $input->getOption('write-sql');
99 4
        $direction = $input->getOption('down') !== false
100 4
            ? Direction::DOWN
101 4
            : Direction::UP;
102
103 4
        $migrator = $this->getDependencyFactory()->getMigrator();
104 4
        $plan     = $this->getDependencyFactory()->getMigrationPlanCalculator()->getPlanForExactVersion(new Version($version), $direction);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type null and string[]; however, parameter $version of Doctrine\Migrations\Version\Version::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        $plan     = $this->getDependencyFactory()->getMigrationPlanCalculator()->getPlanForExactVersion(new Version(/** @scrutinizer ignore-type */ $version), $direction);
Loading history...
105
106 4
        $migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory();
107 4
        $migratorConfiguration        = $migratorConfigurationFactory->getMigratorConfiguration($input);
108
109 4
        if ($path !== false) {
110 2
            $migratorConfiguration->setDryRun(true);
111 2
            $sql = $migrator->migrate($plan, $migratorConfiguration);
112
113 2
            $path = is_string($path) ? $path : getcwd();
114
115 2
            if (! is_string($path) || ! is_writable($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
116
                $output->writeln('<error>Path not writeable!</error>');
117
118
                return 1;
119
            }
120
121 2
            $writer = $this->getDependencyFactory()->getQueryWriter();
122 2
            $writer->write($path, $direction, $sql);
123
124 2
            return 0;
125
        }
126
127 2
        $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)';
128
129 2
        if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) {
130 1
            $output->writeln('<error>Migration cancelled!</error>');
131
132 1
            return 1;
133
        }
134
135 1
        $this->getDependencyFactory()->getMetadataStorage()->ensureInitialized();
136 1
        $migrator->migrate($plan, $migratorConfiguration);
137
138 1
        return 0;
139
    }
140
}
141