-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yentu.php
273 lines (244 loc) · 8.07 KB
/
Yentu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/*
* The MIT License
*
* Copyright 2015 James Ekow Abaka Ainooson.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace yentu;
use clearice\ClearIce;
use ntentan\config\Config;
/**
* Utility class for yentu related functions.
*/
class Yentu
{
/**
* The current home for yentu.
* @see Yentu::setDefaultHome()
* @var string
*/
private static $home = './yentu';
/**
* Current version of yentu.
* @var string
*/
const VERSION = '0.1.0';
/**
* Set the current home of yentu.
* The home of yentu contains is a directory which contains the yentu
* configurations and migrations. Configurations are stored in the config
* sub directory and the migrations are stored in the migrations sub
* directory.
* @param string $home
*/
public static function setDefaultHome($home)
{
self::$home = $home;
}
/**
* Returns a path relative to the current yentu home.
* @param string $path
* @return string
*/
public static function getPath($path)
{
return self::$home . "/$path";
}
/**
* Returns information about all migration paths.
* Yentu can be configured to use multiple migration homes, for multiple
* database configurations. This makes it possible to use a single command
* to run multiple migrations accross multiple databases.
* @return array
*/
public static function getMigrationPathsInfo()
{
$variables = Config::get('yentu:default.variables', []);
$otherMigrations = Config::get('yentu:default.other_migrations', []);
return array_merge(
array(
array(
'home' => Yentu::getPath('migrations'),
'variables' => $variables
)
),
$otherMigrations
);
}
/**
* Returns an array of all migrations that have been run on the database.
* The information returned includes the timestamp, the name of the migration
* and the default schema on which it was run.
* @return array
*/
public static function getRunMirations()
{
$db = DatabaseManipulator::create();
$runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version");
$migrations = array();
foreach ($runMigrations as $migration) {
$migrations[$migration['version']] = array(
'timestamp' => $migration['version'],
'migration' => $migration['migration'],
'default_schema' => $migration['default_schema']
);
}
return $migrations;
}
/**
* Returns an array of all migrations, in all configured migrations
* directories.
* @return array
*/
public static function getAllMigrations()
{
$migrations = array();
foreach (self::getMigrationPathsInfo() as $migration) {
$migrations = $migrations + self::getMigrations($migration['home']);
}
return $migrations;
}
/**
* Return an array of all migrations available.
*
* @param string $path
* @return array
*/
public static function getMigrations($path)
{
if(!file_exists($path)) return [];
$migrationFiles = scandir($path, 0);
$migrations = array();
foreach ($migrationFiles as $migration) {
$details = self::getMigrationDetails($migration);
if ($details === false)
continue;
$migrations[$details['timestamp']] = $details;
unset($migrations[$details['timestamp']][0]);
unset($migrations[$details['timestamp']][1]);
unset($migrations[$details['timestamp']][2]);
}
return $migrations;
}
/**
* Return the details of a migration extracted from the file name.
* This method uses a regular expression to extract the timestamp and
* migration name from the migration script.
*
* @param string $migration
* @return array
*/
private static function getMigrationDetails($migration)
{
if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) {
$details['file'] = $migration;
} else {
$details = false;
}
return $details;
}
/**
* Announce a migration based on the command and the arguments called for
* the migration.
*
* @param string $command The action being performed
* @param string $itemType The type of item
* @param array $arguments The arguments of the
*/
public static function announce($command, $itemType, $arguments)
{
ClearIce::output(
"\n - " . ucfirst("{$command}ing ") .
preg_replace("/([a-z])([A-Z])/", "$1 $2", $itemType) . " " .
self::getDetails($command, Parameters::wrap($arguments)), ClearIce::OUTPUT_LEVEL_2
);
ClearIce::output(".");
}
/**
* Convert the arguments of a migration event to a string description.
*
* @param string $command
* @param array $arguments
* @return string
*/
private static function getDetails($command, $arguments)
{
$dir = '';
$destination = '';
$arguments = Parameters::wrap(
$arguments,
['name' => null]
);
if ($command == 'add') {
$dir = 'to';
} else if ($command == 'drop') {
$dir = 'from';
}
if (isset($arguments['table']) && isset($arguments['schema'])) {
$destination = "table " .
($arguments['schema'] != '' ? "{$arguments['schema']}." : '' ) .
"{$arguments['table']}'";
} elseif (isset($arguments['schema']) && !isset($arguments['table'])) {
$destination = "schema '{$arguments['schema']}'";
}
if(is_string($arguments)) {
return $arguments;
}
if (isset($arguments['column'])) {
$item = $arguments['column'];
} else {
$item = $arguments['name'];
}
return "'$item' $dir $destination";
}
/**
* Reverses a command which is reversible.
*
* @param \yentu\Reversible $command
*/
public static function reverseCommand($command)
{
if ($command instanceof \yentu\Reversible) {
$command->reverse();
}
}
/**
* Display the greeting for the CLI user interface.
*/
public static function greet()
{
$version = Yentu::getVersion();
$welcome = <<<WELCOME
Yentu Database Migration Tool
Version $version
WELCOME;
ClearIce::output($welcome);
}
public static function getVersion()
{
if (defined('PHING_BUILD_VERSION')) {
return PHING_BUILD_VERSION;
} else {
$version = new \SebastianBergmann\Version(Yentu::VERSION, dirname(__DIR__));
return $version->getVersion();
}
}
}