|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Phinx\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
|
|
17
|
|
|
class Migrate extends AbstractCommand |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $defaultName = 'migrate'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::configure(); |
|
32
|
|
|
|
|
33
|
|
|
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment'); |
|
34
|
|
|
|
|
35
|
|
|
$this->setDescription('Migrate the database') |
|
36
|
|
|
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to') |
|
37
|
|
|
->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to') |
|
38
|
|
|
->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it') |
|
39
|
|
|
->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any migrations selected as run, but don't actually execute them") |
|
40
|
35 |
|
->setHelp( |
|
41
|
|
|
<<<EOT |
|
42
|
35 |
|
The <info>migrate</info> command runs all available migrations, optionally up to a specific version |
|
43
|
|
|
|
|
44
|
35 |
|
<info>phinx migrate -e development</info> |
|
45
|
|
|
<info>phinx migrate -e development -t 20110103081132</info> |
|
46
|
35 |
|
<info>phinx migrate -e development -d 20110103</info> |
|
47
|
35 |
|
<info>phinx migrate -e development -v</info> |
|
48
|
35 |
|
|
|
49
|
35 |
|
EOT |
|
50
|
35 |
|
); |
|
51
|
35 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Migrate the database. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input Input |
|
57
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output Output |
|
58
|
|
|
* |
|
59
|
|
|
* @return int integer 0 on success, or an error code. |
|
60
|
|
|
*/ |
|
61
|
35 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
62
|
35 |
|
{ |
|
63
|
|
|
$this->bootstrap($input, $output); |
|
64
|
|
|
|
|
65
|
|
|
$version = $input->getOption('target'); |
|
66
|
|
|
$date = $input->getOption('date'); |
|
67
|
|
|
$fake = (bool)$input->getOption('fake'); |
|
68
|
|
|
|
|
69
|
|
|
$environment = $this->getEnvironment($input, $output); |
|
70
|
|
|
if ($environment === null) { |
|
71
|
3 |
|
return self::CODE_ERROR; |
|
72
|
|
|
} |
|
73
|
3 |
|
|
|
74
|
|
|
$envOptions = $this->getConfig()->getEnvironment($environment); |
|
75
|
3 |
|
if (!$this->checkEnvironmentOptions($envOptions, $output)) { |
|
76
|
3 |
|
return self::CODE_ERROR; |
|
77
|
3 |
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
$versionOrder = $this->getConfig()->getVersionOrder(); |
|
80
|
2 |
|
$output->writeln('<info>ordering by</info> ' . $versionOrder . ' time'); |
|
81
|
2 |
|
|
|
82
|
2 |
|
if ($fake) { |
|
83
|
1 |
|
$output->writeln('<comment>warning</comment> performing fake migrations'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
try { |
|
87
|
3 |
|
// run the migrations |
|
88
|
2 |
|
$start = microtime(true); |
|
89
|
2 |
|
if ($date !== null) { |
|
90
|
|
|
$this->getManager()->migrateToDateTime($environment, new DateTime($date), $fake); |
|
91
|
3 |
|
} else { |
|
92
|
|
|
if ($version) { |
|
93
|
|
|
$version = (int)$version; |
|
94
|
|
|
} |
|
95
|
3 |
|
$this->getManager()->migrate($environment, $version, $fake); |
|
96
|
2 |
|
} |
|
97
|
2 |
|
$end = microtime(true); |
|
98
|
1 |
|
} catch (Exception $e) { |
|
99
|
1 |
|
$output->writeln('<error>' . $e->__toString() . '</error>'); |
|
100
|
|
|
|
|
101
|
|
|
return self::CODE_ERROR; |
|
102
|
2 |
|
} catch (Throwable $e) { |
|
103
|
|
|
$output->writeln('<error>' . $e->__toString() . '</error>'); |
|
104
|
|
|
|
|
105
|
2 |
|
return self::CODE_ERROR; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$output->writeln(''); |
|
109
|
|
|
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>'); |
|
110
|
2 |
|
|
|
111
|
2 |
|
return self::CODE_SUCCESS; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|