forked from Enalean/ForgeUpgrade
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigration.php
More file actions
175 lines (152 loc) · 4.78 KB
/
migration.php
File metadata and controls
175 lines (152 loc) · 4.78 KB
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
<?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 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 General Public License for more details.
*
* You should have received a copy of the GNU 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';
$paths = array();
$includePaths = array();
$excludePaths = 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':
case 'ignore-preup':
$func = $argv[$i];
break;
}
//
// Options
// --path
if (preg_match('/--path=(.*)/',$argv[$i], $matches)) {
if (is_dir($matches[1]) || is_file($matches[1])) {
$paths[] = $matches[1];
} else {
echo 'Error "'.$matches[1].'" is not a valid directory'.PHP_EOL;
}
}
// --include
if (preg_match('/--include=(.*)/',$argv[$i], $matches)) {
$includePaths[] = surroundBy($matches[1], '/');
}
// --exclude
if (preg_match('/--exclude=(.*)/',$argv[$i], $matches)) {
$excludePaths[] = surroundBy($matches[1], '/');
}
// --driver
if (preg_match('/--dbdriver=(.*)/',$argv[$i], $matches)) {
// First try the file
if (is_file($matches[1])) {
require $matches[1];
$className = $matches[1];
$dbDriverName = basename($matches[1], '.php');
} else {
$dbDriverName = ucfirst(strtolower($matches[1]));
$filePath = 'src/db/driver/'.$dbDriverName.'.php';
if (is_file($filePath)) {
require $filePath;
$dbDriverName = 'ForgeUpgrade_Db_Driver_'.$dbDriverName;
} else {
echo "Error: invalid --dbdriver".PHP_EOL;
}
}
}
}
if ($func == 'help') {
usage();
exit;
}
// Get the DB connexion
try {
$dbDriver = new $dbDriverName();
} catch (PDOException $e) {
echo 'Connection faild: '.$e->getMessage().PHP_EOL;
return -1;
}
// Go
$logger = Logger::getRootLogger();
$logger->removeAllAppenders();
$appender = new LoggerAppenderConsoleColor('LoggerAppenderConsoleColor');
$appender->setLayout( new LoggerLayoutTTCC() );
$appender->activateOptions();
$logger->addAppender($appender);
$upg = new ForgeUpgrade($dbDriver);
$upg->setIncludePaths($includePaths);
$upg->setExcludePaths($excludePaths);
$upg->run($func, $paths);
//
// Function definitions
//
/**
* Print Help
*/
function usage() {
echo <<<EOT
Usage: migration.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
ignore-preup Execute migration buckets whithout running "pre" checks
Options:
--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).
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;
}
?>