Completed
Push — master ( 40a73d...3c49e9 )
by José
01:52 queued 10s
created

Migrate::configure()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Console
28
 */
29
namespace Phinx\Console\Command;
30
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class Migrate extends AbstractCommand
36
{
37
    /**
38
     * {@inheritdoc}
39
     */
40 35
    protected function configure()
41
    {
42 35
        parent::configure();
43
44 35
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
45
46 35
        $this->setName($this->getName() ?: 'migrate')
47 35
            ->setDescription('Migrate the database')
48 35
            ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to')
49 35
            ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to')
50 35
            ->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
51 35
            ->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any migrations selected as run, but don't actually execute them")
52
            ->setHelp(
53
                <<<EOT
54
The <info>migrate</info> command runs all available migrations, optionally up to a specific version
55
56
<info>phinx migrate -e development</info>
57
<info>phinx migrate -e development -t 20110103081132</info>
58
<info>phinx migrate -e development -d 20110103</info>
59
<info>phinx migrate -e development -v</info>
60
61 35
EOT
62 35
            );
63
    }
64
65
    /**
66
     * Migrate the database.
67
     *
68
     * @param \Symfony\Component\Console\Input\InputInterface $input
69
     * @param \Symfony\Component\Console\Output\OutputInterface $output
70
     * @return int integer 0 on success, or an error code.
71 3
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73 3
    {
74
        $this->bootstrap($input, $output);
75 3
76 3
        $version = $input->getOption('target');
77 3
        $environment = $input->getOption('environment');
78
        $date = $input->getOption('date');
79 3
        $fake = (bool)$input->getOption('fake');
80 2
81 2 View Code Duplication
        if ($environment === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82 2
            $environment = $this->getConfig()->getDefaultEnvironment();
83 1
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
84
        } else {
85
            $output->writeln('<info>using environment</info> ' . $environment);
86 3
        }
87 3
88 2
        $envOptions = $this->getConfig()->getEnvironment($environment);
89 2
        if (isset($envOptions['adapter'])) {
90
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
91 3
        }
92
93
        if (isset($envOptions['wrapper'])) {
94
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
95 3
        }
96 2
97 2 View Code Duplication
        if (isset($envOptions['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
98 1
            $output->writeln('<info>using database</info> ' . $envOptions['name']);
99 1
        } else {
100
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
101
102 2
            return 1;
103
        }
104
105 2
        if (isset($envOptions['table_prefix'])) {
106
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
107
        }
108
        if (isset($envOptions['table_suffix'])) {
109
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
110 2
        }
111 2
112
        if ($fake) {
113
            $output->writeln('<comment>warning</comment> performing fake migrations');
114 2
        }
115
116 2
        // run the migrations
117
        $start = microtime(true);
118 2
        if ($date !== null) {
119 2
            $this->getManager()->migrateToDateTime($environment, new \DateTime($date), $fake);
120
        } else {
121 2
            $this->getManager()->migrate($environment, $version, $fake);
122
        }
123
        $end = microtime(true);
124
125
        $output->writeln('');
126
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
127
128
        return 0;
129
    }
130
}
131