Passed
Pull Request — master (#739)
by Michael
02:38
created

ExecuteCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 109
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 69 1
B execute() 0 36 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\MigratorConfiguration;
8
use Doctrine\Migrations\Version\Direction;
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
15
/**
16
 * The ExecutCommand class is responsible for executing a single migration version up or down.
17
 */
18
class ExecuteCommand extends AbstractCommand
19
{
20 14
    protected function configure() : void
21
    {
22
        $this
23 14
            ->setName('migrations:execute')
24 14
            ->setAliases(['execute'])
25 14
            ->setDescription(
26 14
                'Execute a single migration version up or down manually.'
27
            )
28 14
            ->addArgument(
29 14
                'version',
30 14
                InputArgument::REQUIRED,
31 14
                'The version to execute.',
32 14
                null
33
            )
34 14
            ->addOption(
35 14
                'write-sql',
36 14
                null,
37 14
                InputOption::VALUE_OPTIONAL,
38 14
                'The path to output the migration SQL file instead of executing it. Defaults to current working directory.',
39 14
                false
40
            )
41 14
            ->addOption(
42 14
                'dry-run',
43 14
                null,
44 14
                InputOption::VALUE_NONE,
45 14
                'Execute the migration as a dry run.'
46
            )
47 14
            ->addOption(
48 14
                'up',
49 14
                null,
50 14
                InputOption::VALUE_NONE,
51 14
                'Execute the migration up.'
52
            )
53 14
            ->addOption(
54 14
                'down',
55 14
                null,
56 14
                InputOption::VALUE_NONE,
57 14
                'Execute the migration down.'
58
            )
59 14
            ->addOption(
60 14
                'query-time',
61 14
                null,
62 14
                InputOption::VALUE_NONE,
63 14
                'Time all the queries individually.'
64
            )
65 14
            ->setHelp(<<<EOT
66 14
The <info>%command.name%</info> command executes a single migration version up or down manually:
67
68
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
69
70
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
71
72
    <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
73
74
You can also execute the migration as a <comment>--dry-run</comment>:
75
76
    <info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info>
77
78
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>:
79
80
    <info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info>
81
82
Or you can also execute the migration without a warning message which you need to interact with:
83
84
    <info>%command.full_name% YYYYMMDDHHMMSS --no-interaction</info>
85
EOT
86
        );
87
88 14
        parent::configure();
89 14
    }
90
91 4
    public function execute(InputInterface $input, OutputInterface $output) : ?int
92
    {
93 4
        $version        = $input->getArgument('version');
94 4
        $timeAllQueries = (bool) $input->getOption('query-time');
95 4
        $dryRun         = (bool) $input->getOption('dry-run');
96 4
        $path           = $input->getOption('write-sql');
97 4
        $direction      = $input->getOption('down') !== false
98 4
            ? Direction::DOWN
99 4
            : Direction::UP;
100
101 4
        $version = $this->migrationRepository->getVersion($version);
102
103 4
        if ($path !== false) {
104 2
            $path = $path === null ? getcwd() : $path;
105
106 2
            $version->writeSqlFile($path, $direction);
107
108 2
            return 0;
109
        }
110
111 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)';
112
113 2
        if (! $dryRun && ! $this->canExecute($question, $input, $output)) {
114 1
            $output->writeln('<error>Migration cancelled!</error>');
115
116 1
            return 1;
117
        }
118
119 1
        $migratorConfiguration = (new MigratorConfiguration())
120 1
            ->setDryRun($dryRun)
121 1
            ->setTimeAllQueries($timeAllQueries)
122
        ;
123
124 1
        $version->execute($direction, $migratorConfiguration);
125
126 1
        return 0;
127
    }
128
}
129