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('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 |
|
->setHelp( |
52
|
|
|
<<<EOT |
53
|
|
|
The <info>migrate</info> command runs all available migrations, optionally up to a specific version |
54
|
|
|
|
55
|
|
|
<info>phinx migrate -e development</info> |
56
|
|
|
<info>phinx migrate -e development -t 20110103081132</info> |
57
|
|
|
<info>phinx migrate -e development -d 20110103</info> |
58
|
|
|
<info>phinx migrate -e development -v</info> |
59
|
|
|
|
60
|
|
|
EOT |
61
|
35 |
|
); |
62
|
35 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Migrate the database. |
66
|
|
|
* |
67
|
|
|
* @param InputInterface $input |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @return integer integer 0 on success, or an error code. |
70
|
|
|
*/ |
71
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
3 |
|
$this->bootstrap($input, $output); |
74
|
|
|
|
75
|
3 |
|
$version = $input->getOption('target'); |
76
|
3 |
|
$environment = $input->getOption('environment'); |
77
|
3 |
|
$date = $input->getOption('date'); |
78
|
|
|
|
79
|
3 |
View Code Duplication |
if ($environment === null) { |
|
|
|
|
80
|
2 |
|
$environment = $this->getConfig()->getDefaultEnvironment(); |
81
|
2 |
|
$output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment); |
82
|
2 |
|
} else { |
83
|
1 |
|
$output->writeln('<info>using environment</info> ' . $environment); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
$envOptions = $this->getConfig()->getEnvironment($environment); |
87
|
3 |
|
if (isset($envOptions['adapter'])) { |
88
|
2 |
|
$output->writeln('<info>using adapter</info> ' . $envOptions['adapter']); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
3 |
|
if (isset($envOptions['wrapper'])) { |
92
|
|
|
$output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
View Code Duplication |
if (isset($envOptions['name'])) { |
|
|
|
|
96
|
2 |
|
$output->writeln('<info>using database</info> ' . $envOptions['name']); |
97
|
2 |
|
} else { |
98
|
1 |
|
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>'); |
99
|
1 |
|
|
100
|
|
|
return 1; |
101
|
|
|
} |
102
|
2 |
|
|
103
|
|
|
if (isset($envOptions['table_prefix'])) { |
104
|
|
|
$output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']); |
105
|
2 |
|
} |
106
|
|
|
if (isset($envOptions['table_suffix'])) { |
107
|
|
|
$output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
// run the migrations |
111
|
2 |
|
$start = microtime(true); |
112
|
|
|
if ($date !== null) { |
113
|
|
|
$this->getManager()->migrateToDateTime($environment, new \DateTime($date)); |
114
|
2 |
|
} else { |
115
|
|
|
$this->getManager()->migrate($environment, $version); |
116
|
2 |
|
} |
117
|
|
|
$end = microtime(true); |
118
|
2 |
|
|
119
|
2 |
|
$output->writeln(''); |
120
|
|
|
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>'); |
121
|
2 |
|
|
122
|
|
|
return 0; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.