1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Console; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Revert Command |
11
|
|
|
* |
12
|
|
|
* @package Refinery |
13
|
|
|
* @author Rougin Gutib <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class RevertCommand extends ChangeCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $done = 'Rolled back: '; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $pending = 'Rolling back:'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
protected $reversed = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Configures the current command. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
24 |
|
protected function configure() |
38
|
|
|
{ |
39
|
24 |
|
$this->setName('rollback')->setDescription('Returns to a previous or specified version'); |
40
|
|
|
|
41
|
24 |
|
$this->addArgument('version', InputArgument::OPTIONAL, 'Version number of the migration'); |
42
|
24 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Executes the command. |
46
|
|
|
* |
47
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
48
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
9 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
9 |
|
list($current, $version) = $this->version($input); |
54
|
|
|
|
55
|
9 |
|
if ($current !== '0' && $current !== '000') { |
56
|
6 |
|
$result = $this->rollback($current, (string) $version); |
57
|
|
|
|
58
|
6 |
|
return $this->migrate($output, $result[0], $result[1]); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$text = (string) 'There is nothing to migrate.'; |
62
|
|
|
|
63
|
3 |
|
$output->writeln('<info>' . $text . '</info>'); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Performs a rollback to a specified migration. |
68
|
|
|
* |
69
|
|
|
* @param string $current |
70
|
|
|
* @param string $version |
71
|
|
|
* @return string|boolean |
72
|
|
|
*/ |
73
|
6 |
|
protected function rollback($current, $version) |
74
|
|
|
{ |
75
|
6 |
|
list($migrations, $versions) = $this->migrations(); |
76
|
|
|
|
77
|
6 |
|
$index = array_search($version, (array) $versions); |
78
|
|
|
|
79
|
6 |
|
$file = $version === $current ? $current : $version; |
80
|
|
|
|
81
|
6 |
|
$filename = (string) basename($migrations[$file]); |
82
|
|
|
|
83
|
6 |
|
$index = $version === $current ? $index + 1 : $index + 0; |
84
|
|
|
|
85
|
6 |
|
return array($filename, $versions[(integer) $index]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the current and user-specified versions. |
90
|
|
|
* |
91
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
9 |
|
protected function version(InputInterface $input) |
95
|
|
|
{ |
96
|
9 |
|
$version = $input->getArgument('version'); |
97
|
|
|
|
98
|
9 |
|
$current = $this->manager->current(); |
99
|
|
|
|
100
|
9 |
|
is_null($version) && $version = $current; |
101
|
|
|
|
102
|
9 |
|
return array($current, $version); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|