|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
/* |
|
|
|
|
|
|
4
|
|
|
* The MIT License |
|
|
|
|
|
|
5
|
|
|
* |
|
|
|
|
|
|
6
|
|
|
* Copyright 2015 James Ekow Abaka Ainooson. |
|
|
|
|
|
|
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 deal |
|
|
|
|
|
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
|
|
|
|
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
|
|
|
|
|
12
|
|
|
* 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 FROM, |
|
|
|
|
|
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
|
|
|
|
|
24
|
|
|
* THE SOFTWARE. |
|
|
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
namespace yentu; |
|
28
|
|
|
use clearice\io\Io; |
|
29
|
|
|
use ntentan\config\Config; |
|
30
|
|
|
use yentu\factories\DatabaseManipulatorFactory; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Utility class for yentu related functions. |
|
35
|
|
|
*/ |
|
|
|
|
|
|
36
|
|
|
class Yentu |
|
37
|
|
|
{ |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The current home path for yentu. |
|
41
|
|
|
* The home path represents the location of migrations and the configurations used for the yentu session. |
|
|
|
|
|
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $home; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* An instance of the clearice Io class |
|
49
|
|
|
* @var Io |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
private $io; |
|
|
|
|
|
|
52
|
|
|
private $databaseManipulatorFactory; |
|
|
|
|
|
|
53
|
|
|
private $config; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Current version of yentu. |
|
57
|
|
|
* @var string |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
const VERSION = 'v0.3.0'; |
|
60
|
|
|
|
|
61
|
41 |
|
public function __construct(Io $io, DatabaseManipulatorFactory $databaseManipulatorFactory) |
|
|
|
|
|
|
62
|
|
|
{ |
|
|
|
|
|
|
63
|
41 |
|
$this->databaseManipulatorFactory = $databaseManipulatorFactory; |
|
64
|
41 |
|
$this->io = $io; |
|
|
|
|
|
|
65
|
41 |
|
} |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the current home of yentu. |
|
69
|
|
|
* The home of yentu contains is a directory which contains the yentu |
|
70
|
|
|
* configurations and migrations. Configurations are stored in the config |
|
71
|
|
|
* sub directory and the migrations are stored in the migrations sub |
|
72
|
|
|
* directory. |
|
73
|
|
|
* @param string $home |
|
|
|
|
|
|
74
|
|
|
*/ |
|
|
|
|
|
|
75
|
41 |
|
public function setDefaultHome($home) |
|
|
|
|
|
|
76
|
|
|
{ |
|
|
|
|
|
|
77
|
41 |
|
$this->home = $home; |
|
78
|
41 |
|
} |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Inject a configuration object |
|
82
|
|
|
* @param Config $config |
|
|
|
|
|
|
83
|
|
|
*/ |
|
|
|
|
|
|
84
|
41 |
|
public function setConfig(Config $config) |
|
85
|
|
|
{ |
|
|
|
|
|
|
86
|
41 |
|
$this->config = $config; |
|
87
|
41 |
|
} |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
public function getConfig() : Config |
|
|
|
|
|
|
90
|
|
|
{ |
|
|
|
|
|
|
91
|
|
|
return $this->config; |
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns a path relative to the current yentu home. |
|
96
|
|
|
* @param string $path |
|
|
|
|
|
|
97
|
|
|
* @return string |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
41 |
|
public function getPath($path) |
|
|
|
|
|
|
100
|
|
|
{ |
|
|
|
|
|
|
101
|
41 |
|
return $this->home . "/$path"; |
|
|
|
|
|
|
102
|
|
|
} |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns an array of all migrations that have been run on the database. |
|
106
|
|
|
* The information returned includes the timestamp, the name of the migration |
|
107
|
|
|
* and the default schema on which it was run. |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
* @throws exceptions\DatabaseManipulatorException |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public function getExecutedMigrations() |
|
113
|
|
|
{ |
|
|
|
|
|
|
114
|
|
|
$db = $this->databaseManipulatorFactory->createManipulator(); |
|
|
|
|
|
|
115
|
|
|
$runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version"); |
|
|
|
|
|
|
116
|
|
|
$migrations = array(); |
|
|
|
|
|
|
117
|
|
|
foreach ($runMigrations as $migration) { |
|
118
|
|
|
$migrations[$migration['version']] = array( |
|
|
|
|
|
|
119
|
|
|
'timestamp' => $migration['version'], |
|
|
|
|
|
|
120
|
|
|
'migration' => $migration['migration'], |
|
|
|
|
|
|
121
|
|
|
'default_schema' => $migration['default_schema'] |
|
|
|
|
|
|
122
|
|
|
); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $migrations; |
|
126
|
|
|
} |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns an array of all migrations, in all configured migrations directories. |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getAllMigrations() |
|
134
|
|
|
{ |
|
|
|
|
|
|
135
|
|
|
$migrations = array(); |
|
|
|
|
|
|
136
|
|
|
foreach ($this->getMigrationPaths() as $migration) { |
|
137
|
|
|
$migrations = $migrations + $this->getMigrationsFromPath($migration['home']); |
|
|
|
|
|
|
138
|
|
|
} |
|
|
|
|
|
|
139
|
|
|
return $migrations; |
|
140
|
|
|
} |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
10 |
|
public function getMigrationPaths() |
|
|
|
|
|
|
143
|
|
|
{ |
|
|
|
|
|
|
144
|
10 |
|
$variables = $this->config->get('variables', []); |
|
|
|
|
|
|
145
|
10 |
|
$otherMigrations = $this->config->get('other_migrations', []); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
10 |
|
return array_merge( |
|
148
|
|
|
array( |
|
|
|
|
|
|
149
|
|
|
array( |
|
|
|
|
|
|
150
|
10 |
|
'home' => $this->getPath('migrations'), |
|
|
|
|
|
|
151
|
10 |
|
'variables' => $variables |
|
|
|
|
|
|
152
|
|
|
) |
|
|
|
|
|
|
153
|
10 |
|
), $otherMigrations |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return an array of all migrations from a given migration path |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $path |
|
|
|
|
|
|
161
|
|
|
* @return array |
|
|
|
|
|
|
162
|
|
|
*/ |
|
163
|
10 |
|
public function getMigrationsFromPath($path) |
|
|
|
|
|
|
164
|
|
|
{ |
|
|
|
|
|
|
165
|
10 |
|
if (!file_exists($path)) |
|
|
|
|
|
|
166
|
|
|
return []; |
|
|
|
|
|
|
167
|
10 |
|
$migrationFiles = scandir($path, 0); |
|
168
|
10 |
|
$migrations = array(); |
|
|
|
|
|
|
169
|
10 |
|
foreach ($migrationFiles as $migration) { |
|
170
|
10 |
|
$details = $this->getMigrationDetails($migration); |
|
171
|
10 |
|
if ($details === false) |
|
|
|
|
|
|
172
|
10 |
|
continue; |
|
173
|
10 |
|
$migrations[$details['timestamp']] = $details; |
|
174
|
10 |
|
unset($migrations[$details['timestamp']][0]); |
|
175
|
10 |
|
unset($migrations[$details['timestamp']][1]); |
|
176
|
10 |
|
unset($migrations[$details['timestamp']][2]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
10 |
|
return $migrations; |
|
180
|
|
|
} |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Return the details of a migration extracted from the file name. This method uses a regular expression to extract |
|
|
|
|
|
|
184
|
|
|
* the timestamp and migration name from the migration script. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $migration |
|
|
|
|
|
|
187
|
|
|
* @return array|bool |
|
|
|
|
|
|
188
|
|
|
*/ |
|
189
|
10 |
|
private function getMigrationDetails($migration) |
|
|
|
|
|
|
190
|
|
|
{ |
|
|
|
|
|
|
191
|
10 |
|
if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) { |
|
|
|
|
|
|
192
|
10 |
|
$details['file'] = $migration; |
|
193
|
|
|
} else { |
|
194
|
10 |
|
$details = false; |
|
|
|
|
|
|
195
|
|
|
} |
|
|
|
|
|
|
196
|
10 |
|
return $details; |
|
197
|
|
|
} |
|
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Announce a migration based on the command and the arguments called for the migration. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $command The action being performed |
|
|
|
|
|
|
203
|
|
|
* @param string $itemType The type of item |
|
|
|
|
|
|
204
|
|
|
* @param array $arguments The arguments of the |
|
|
|
|
|
|
205
|
|
|
*/ |
|
|
|
|
|
|
206
|
10 |
|
public function announce($command, $itemType, $arguments) |
|
|
|
|
|
|
207
|
|
|
{ |
|
|
|
|
|
|
208
|
10 |
|
$this->io->output( |
|
209
|
10 |
|
"\n - " . ucfirst("{$command}ing ") . |
|
|
|
|
|
|
210
|
10 |
|
preg_replace("/([a-z])([A-Z])/", "$1 $2", $itemType) . " " . |
|
|
|
|
|
|
211
|
10 |
|
$this->getEventDescription($command, Parameters::wrap($arguments)), Io::OUTPUT_LEVEL_2 |
|
212
|
|
|
); |
|
213
|
10 |
|
$this->io->output("."); |
|
|
|
|
|
|
214
|
10 |
|
} |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Convert the arguments of a migration event to a string description. |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $command |
|
|
|
|
|
|
220
|
|
|
* @param array $arguments |
|
|
|
|
|
|
221
|
|
|
* @return string |
|
|
|
|
|
|
222
|
|
|
*/ |
|
223
|
10 |
|
private function getEventDescription($command, $arguments) |
|
|
|
|
|
|
224
|
|
|
{ |
|
|
|
|
|
|
225
|
10 |
|
$dir = ''; |
|
|
|
|
|
|
226
|
10 |
|
$destination = ''; |
|
227
|
10 |
|
$arguments = Parameters::wrap($arguments, ['name' => null]); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
10 |
|
if ($command == 'add') { |
|
|
|
|
|
|
230
|
10 |
|
$dir = 'to'; |
|
231
|
6 |
|
} else if ($command == 'drop') { |
|
|
|
|
|
|
232
|
|
|
$dir = 'from'; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
10 |
|
if (isset($arguments['table']) && isset($arguments['schema'])) { |
|
236
|
|
|
$destination = "table " . |
|
|
|
|
|
|
237
|
10 |
|
($arguments['schema'] != '' ? "{$arguments['schema']}." : '' ) . |
|
|
|
|
|
|
238
|
10 |
|
"{$arguments['table']}'"; |
|
|
|
|
|
|
239
|
10 |
|
} elseif (isset($arguments['schema']) && !isset($arguments['table'])) { |
|
|
|
|
|
|
240
|
10 |
|
$destination = "schema '{$arguments['schema']}'"; |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
10 |
|
if (is_string($arguments)) { |
|
244
|
1 |
|
return $arguments; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
10 |
|
if (isset($arguments['column'])) { |
|
248
|
10 |
|
$item = $arguments['column']; |
|
249
|
|
|
} else { |
|
250
|
10 |
|
$item = $arguments['name']; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
10 |
|
return "'$item' $dir $destination"; |
|
|
|
|
|
|
254
|
|
|
} |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Reverses a command which is reversible. |
|
258
|
|
|
* |
|
259
|
|
|
* @param \yentu\Reversible $command |
|
|
|
|
|
|
260
|
|
|
*/ |
|
|
|
|
|
|
261
|
|
|
public function reverseCommand($command) |
|
|
|
|
|
|
262
|
|
|
{ |
|
|
|
|
|
|
263
|
|
|
if ($command instanceof \yentu\Reversible) { |
|
|
|
|
|
|
264
|
|
|
$command->reverse(); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Display the greeting for the CLI user interface. |
|
270
|
|
|
*/ |
|
|
|
|
|
|
271
|
41 |
|
public function greet() |
|
272
|
|
|
{ |
|
|
|
|
|
|
273
|
41 |
|
$version = $this->getVersion(); |
|
274
|
|
|
$welcome = <<<WELCOME |
|
|
|
|
|
|
275
|
|
|
Yentu Database Migration Tool |
|
276
|
41 |
|
Version $version |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
WELCOME; |
|
280
|
41 |
|
$this->io->output($welcome); |
|
281
|
41 |
|
} |
|
|
|
|
|
|
282
|
|
|
|
|
283
|
41 |
|
public function getVersion() |
|
|
|
|
|
|
284
|
|
|
{ |
|
|
|
|
|
|
285
|
41 |
|
if (defined('PHING_BUILD_VERSION')) { |
|
286
|
41 |
|
return PHING_BUILD_VERSION; |
|
|
|
|
|
|
287
|
|
|
} else { |
|
288
|
|
|
$version = new \SebastianBergmann\Version(Yentu::VERSION, dirname(__DIR__)); |
|
|
|
|
|
|
289
|
|
|
return $version->getVersion(); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
} |
|
|
|
|
|
|
294
|
|
|
|