1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Commands; |
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
|
|
|
* Rollback Command |
11
|
|
|
* |
12
|
|
|
* Returns to a previous or specified migration. |
13
|
|
|
* |
14
|
|
|
* @package Refinery |
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class RollbackCommand extends AbstractCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Sets the configurations of the specified command. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
33 |
|
protected function configure() |
25
|
|
|
{ |
26
|
33 |
|
$this->setName('rollback') |
27
|
33 |
|
->setDescription('Returns to a previous or specified migration') |
28
|
33 |
|
->addArgument('version', InputArgument::OPTIONAL, 'Specified version of the migration'); |
29
|
33 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Executes the command. |
33
|
|
|
* |
34
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
35
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
36
|
|
|
* @return object|\Symfony\Component\Console\Output\OutputInterface |
37
|
|
|
*/ |
38
|
9 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
39
|
|
|
{ |
40
|
9 |
|
list($filenames, $migrations) = $this->getMigrations(APPPATH . 'migrations'); |
41
|
|
|
|
42
|
9 |
|
$current = $this->getLatestVersion(true); |
43
|
6 |
|
$version = $input->getArgument('version'); |
44
|
|
|
|
45
|
6 |
|
$this->isValidVersion($migrations, $version); |
46
|
|
|
|
47
|
3 |
|
$migration = $migrations[count($migrations) - 1]; |
48
|
3 |
|
$fileName = $filenames[count($migrations) - 1]; |
49
|
|
|
|
50
|
3 |
|
empty($version) || $migration = $version; |
51
|
|
|
|
52
|
|
|
// Enable migration and change the current version to a latest one |
53
|
3 |
|
$this->toggleMigration(true); |
54
|
3 |
|
$this->changeVersion($current, $migration); |
55
|
|
|
|
56
|
3 |
|
$this->codeigniter->load->library('migration'); |
|
|
|
|
57
|
3 |
|
$this->codeigniter->migration->current(); |
|
|
|
|
58
|
|
|
|
59
|
3 |
|
$this->toggleMigration(); |
60
|
|
|
|
61
|
3 |
|
$message = "Database is reverted back to version $migration ($fileName)"; |
62
|
|
|
|
63
|
3 |
|
return $output->writeln('<info>' . $message . '</info>'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks if the version is valid to be rolled back. |
68
|
|
|
* |
69
|
|
|
* @param array $migrations |
70
|
|
|
* @param string $version |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
6 |
|
protected function isValidVersion(array $migrations, $version) |
74
|
|
|
{ |
75
|
6 |
|
foreach ($migrations as $migration) { |
76
|
6 |
|
if ($migration == $version || empty($version)) { |
77
|
3 |
|
return true; |
78
|
|
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
3 |
|
throw new \InvalidArgumentException('Cannot rollback to version ' . $version); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.