Completed
Push — master ( 70026a...ff8ed5 )
by Mike
07:46
created

ExecuteCommand::execute()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 20
cts 20
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 12
nop 2
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
21
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputOption;
26
27
/**
28
 * Command for executing single migrations up or down manually.
29
 *
30
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
31
 * @link    www.doctrine-project.org
32
 * @since   2.0
33
 * @author  Jonathan Wage <[email protected]>
34
 */
35
class ExecuteCommand extends AbstractCommand
36
{
37 13 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 13
        $this
40 13
            ->setName('migrations:execute')
41 13
            ->setDescription('Execute a single migration version up or down manually.')
42 13
            ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null)
43 13
            ->addOption('write-sql', null, InputOption::VALUE_NONE, 'The path to output the migration SQL file instead of executing it.')
44 13
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run.')
45 13
            ->addOption('up', null, InputOption::VALUE_NONE, 'Execute the migration up.')
46 13
            ->addOption('down', null, InputOption::VALUE_NONE, 'Execute the migration down.')
47 13
            ->addOption('query-time', null, InputOption::VALUE_NONE, 'Time all the queries individually.')
48 13
            ->setHelp(<<<EOT
49
The <info>%command.name%</info> command executes a single migration version up or down manually:
50
51
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
52
53
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
54
55
    <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
56
57
You can also execute the migration as a <comment>--dry-run</comment>:
58
59
    <info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info>
60
61
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>:
62
63
    <info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info>
64
65
Or you can also execute the migration without a warning message which you need to interact with:
66
67
    <info>%command.full_name% --no-interaction</info>
68
EOT
69 13
        );
70
71 13
        parent::configure();
72 13
    }
73
74 5
    public function execute(InputInterface $input, OutputInterface $output)
75
    {
76 5
        $version = $input->getArgument('version');
77 5
        $direction = $input->getOption('down') ? 'down' : 'up';
78
79 5
        $configuration = $this->getMigrationConfiguration($input, $output);
80 5
        $version = $configuration->getVersion($version);
81
82 5
        $timeAllqueries = $input->getOption('query-time');
83
84 5
        if ($path = $input->getOption('write-sql')) {
85 2
            $path = is_bool($path) ? getcwd() : $path;
86 2
            $version->writeSqlFile($path, $direction);
87 2
        } else {
88 3
            if ($input->isInteractive()) {
89 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)';
90 2
                $execute = $this->askConfirmation($question, $input, $output);
91 2
            } else {
92 1
                $execute = true;
93
            }
94
95 3
            if ($execute) {
96 2
                $version->execute($direction, (boolean) $input->getOption('dry-run'), $timeAllqueries);
97 2
            } else {
98 1
                $output->writeln('<error>Migration cancelled!</error>');
99
            }
100
        }
101 5
    }
102
}
103