forked from Enalean/ForgeUpgrade
/
forgeupgrade.php
209 lines (182 loc) · 6.14 KB
/
forgeupgrade.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
<?php
/**
* Copyright (c) STMicroelectronics, 2010. All Rights Reserved.
*
* Originally written by Manuel Vacelet
*
* ForgeUpgrade is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ForgeUpgrade is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ForgeUpgrade. If not, see <http://www.gnu.org/licenses/>.
*/
// An upgrade process shouldn't end because it takes too much time ot too
// memory.
ini_set('max_execution_time', 0);
ini_set('memory_limit', -1);
ini_set('include_path', dirname(__FILE__).PATH_SEPARATOR.ini_get('include_path'));
require 'lib/log4php/Logger.php';
require 'src/ForgeUpgrade.php';
require 'src/LoggerAppenderConsoleColor.php';
// Parameters
$func = 'help';
$options = array();
for ($i = 1; $i < $argc; $i++) {
//
// Commands
switch ($argv[$i]) {
case 'help':
case 'record-only':
case 'update':
case 'check-update':
case 'run-pre':
case 'already-applied':
$func = $argv[$i];
break;
}
//
// Options
// --config
if (preg_match('/--config=(.*)/',$argv[$i], $matches)) {
if (is_file($matches[1])) {
$options = parse_ini_file($matches[1], true);
}
}
// --path
if (preg_match('/--path=(.*)/',$argv[$i], $matches)) {
if (is_dir($matches[1]) || is_file($matches[1])) {
$options['core']['path'][] = $matches[1];
} else {
echo 'Error "'.$matches[1].'" is not a valid directory'.PHP_EOL;
}
}
// --include
if (preg_match('/--include=(.*)/',$argv[$i], $matches)) {
$options['core']['include_path'][] = surroundBy($matches[1], '/');
}
// --exclude
if (preg_match('/--exclude=(.*)/',$argv[$i], $matches)) {
$options['core']['exclude_path'][] = surroundBy($matches[1], '/');
}
// --driver
if (preg_match('/--dbdriver=(.*)/',$argv[$i], $matches)) {
$options['core']['dbdriver'] = $matches[1];
}
//--ignore-preup
if (preg_match('/--ignore-preup/',$argv[$i], $matches)) {
$options['core']['ignore_preup'] = true;
}
//--force
if (preg_match('/--force/',$argv[$i], $matches)) {
$options['core']['force'] = true;
}
// --level
if (preg_match('/--verbose=(.*)/',$argv[$i], $matches)) {
$options['core']['verbose'] = LoggerLevel::toLevel($matches[1], 'INFO');
}
// --bucket
if (preg_match('/--bucket=(.*)/',$argv[$i], $matches)) {
$options['core']['bucket'] = $matches[1];
}
}
if ($func == 'help') {
usage();
exit;
}
if (!isset($options['core']['verbose'])) {
$options['core']['verbose'] = 'INFO';
}
// Get the DB connexion
// First try the file
if (is_file($options['core']['dbdriver'])) {
require $options['core']['dbdriver'];
//$className = $options['core']['dbdriver'];
$dbDriverName = basename($options['core']['dbdriver'], '.php');
} else {
$dbDriverName = ucfirst(strtolower($options['core']['dbdriver']));
$filePath = 'src/db/driver/'.$dbDriverName.'.php';
if (is_file($filePath)) {
require $filePath;
$dbDriverName = 'ForgeUpgrade_Db_Driver_'.$dbDriverName;
} else {
echo "Error: invalid --dbdriver".PHP_EOL;
}
}
try {
$dbDriver = new $dbDriverName();
} catch (PDOException $e) {
echo 'Connection faild: '.$e->getMessage().PHP_EOL;
return -1;
}
// Special logger to display nice colors according to levels
$logger = Logger::getRootLogger();
$logger->removeAllAppenders();
$appender = new LoggerAppenderConsoleColor('LoggerAppenderConsoleColor');
$appender->setLayout( new LoggerLayoutSimple() );
$appender->setThreshold($options['core']['verbose']);
$appender->activateOptions();
$logger->addAppender($appender);
// Go
$upg = new ForgeUpgrade($dbDriver);
$upg->setOptions($options);
$upg->run($func);
//
// Function definitions
//
/**
* Print Help
*/
function usage() {
echo <<<EOT
Usage: forgeupgrade.php [options] command
Commands:
already-applied List all applied buckets
check-update List all available migration buckets not already applied (pending)
run-pre Run pending migration buckets "pre" checks
update Execute pending migration buckets
record-only Record all available buckets as executed in the database without
actually executing them
Options:
--config=[/path] Path to ForgeUpgrade config file (you can define all options in a config.ini file)
--path=[/path] Path where to find migration buckets [default: current dir]
--include=[/path] Only consider paths that contains given pattern
--exclude=[/path] Don't consider paths that contains given pattern
--dbdriver=[name|/path] The database driver to use (either a name or a path
to the driver file for custom ones).
--ignore-preup Execute migration buckets whithout running "pre" checks
--force Execute migration buckets even there are errors
--verbose=[level] How verbose: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF
Default: INFO
--bucket=[bucket id] Used with already-applied command, to display the detailed
log for this bucket
EOT;
}
/**
* Surround a string by a char if not present
*
* @param String $str String to surround
* @param String $char Char to add
*
* @return String
*/
function surroundBy($str, $char) {
if (strpos($str, $char) === false) {
$str = $char.$str.$char;
} else {
if (strpos($str, $char) !== 0) {
$str = $char.$str;
}
if (strrpos($str, $char) !== (strlen($str) - 1)) {
$str = $str.$char;
}
}
return $str;
}
?>