1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of Cli for CodeIgniter |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/codeigniter-cli |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kenjis\CodeIgniter_Cli\Command; |
12
|
|
|
|
13
|
|
|
use Aura\Cli\Status; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property \CI_Migration $migration |
17
|
|
|
* @property \CI_Loader $load |
18
|
|
|
* @property \CI_Config $config |
19
|
|
|
*/ |
20
|
|
|
class Migrate extends Command |
21
|
|
|
{ |
22
|
|
|
public function __invoke($command = null) |
23
|
|
|
{ |
24
|
|
|
$this->load->library('migration'); |
25
|
|
|
$this->load->config('migration'); |
26
|
|
|
|
27
|
|
|
if ($command === 'status') { |
28
|
|
|
$this->listMigrationFiles(); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($command === 'version') { |
33
|
|
|
$this->showVersions(); |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// if argument is digits, migrate to the version |
38
|
|
|
if (ctype_digit($command)) { |
39
|
|
|
if ($this->migrateToVersion($command) === false) { |
40
|
|
|
return Status::FAILURE; |
41
|
|
|
} else { |
42
|
|
|
$this->showVersions(); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($command !== null) { |
48
|
|
|
$this->stdio->errln( |
49
|
|
|
'<<red>>No such command: ' . $command . '<<reset>>' |
50
|
|
|
); |
51
|
|
|
return Status::USAGE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// if no argument, migrate to current |
55
|
|
View Code Duplication |
if ($this->migration->current() === false) { |
|
|
|
|
56
|
|
|
$this->stdio->errln( |
57
|
|
|
'<<red>>' . $this->migration->error_string() . '<<reset>>' |
58
|
|
|
); |
59
|
|
|
return Status::FAILURE; |
60
|
|
|
} else { |
61
|
|
|
$this->showVersions(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function migrateToVersion($version) |
66
|
|
|
{ |
67
|
|
View Code Duplication |
if ($this->migration->version($version) === false) { |
|
|
|
|
68
|
|
|
$this->stdio->errln( |
69
|
|
|
'<<red>>' . $this->migration->error_string() . '<<reset>>' |
70
|
|
|
); |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getDbVersion() |
78
|
|
|
{ |
79
|
|
|
$row = $this->db->select('version')->get($this->config->item('migration_table'))->row(); |
80
|
|
|
return $row ? $row->version : '0'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function showVersions() |
84
|
|
|
{ |
85
|
|
|
$this->stdio->outln( |
86
|
|
|
' current: <<green>>' . $this->config->item('migration_version') . '<<reset>>' |
87
|
|
|
. ' (in config/migration.php)' |
88
|
|
|
); |
89
|
|
|
$version = $this->getDbVersion(); |
90
|
|
|
$this->stdio->outln( |
91
|
|
|
'database: <<bold>>' . $version . '<<reset>>' |
92
|
|
|
. ' (in database table)' |
93
|
|
|
); |
94
|
|
|
$version = $this->getLatestVersion(); |
95
|
|
|
$this->stdio->outln( |
96
|
|
|
' latest: ' . $version . '' |
97
|
|
|
. ' (in migration files)' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getLatestVersion() |
102
|
|
|
{ |
103
|
|
|
$files = $this->migration->find_migrations(); |
104
|
|
|
|
105
|
|
|
if ($files === []) { |
106
|
|
|
return 'null'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
end($files); |
110
|
|
|
return key($files); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function listMigrationFiles() |
114
|
|
|
{ |
115
|
|
|
$this->stdio->outln( |
116
|
|
|
$this->config->item('migration_path') |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$current = $this->config->item('migration_version'); |
120
|
|
|
$db = $this->getDbVersion(); |
121
|
|
|
|
122
|
|
|
$files = $this->migration->find_migrations(); |
123
|
|
|
foreach ($files as $v => $file) { |
124
|
|
|
if ($v == $current && $v == $db) { |
125
|
|
|
$this->stdio->outln( |
126
|
|
|
' <<green>>' . basename($file) .' (current/database)<<reset>>' |
127
|
|
|
); |
128
|
|
|
} elseif ($v == $current) { |
129
|
|
|
$this->stdio->outln( |
130
|
|
|
' <<green>>' . basename($file) .' (current)<<reset>>' |
131
|
|
|
); |
132
|
|
|
} elseif ($v == $db) { |
133
|
|
|
$this->stdio->outln( |
134
|
|
|
' <<bold>>' . basename($file) .' (database)<<reset>>' |
135
|
|
|
); |
136
|
|
|
} else { |
137
|
|
|
$this->stdio->outln(' ' . basename($file)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.