forked from jhaagsma/eemphyre-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrationManager.class.php
221 lines (180 loc) · 6.51 KB
/
MigrationManager.class.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
<?php namespace EmPHyre;
/**
* ABSTRACT CLASS, Migration Manager object for the EmPHyre project
*
* PHP version 7
*
* ------
* These files are part of the empiresPHPframework;
* The original framework core (specifically the mysql.php
* the router.php and the errorlog) was started by Timo Ewalds,
* and rewritten to use APC and extended by Julian Haagsma,
* for use in Earth Empires (located at http://www.earthempires.com );
* it was spun out for use on other projects.
*
* The general.php contains content from Earth Empires
* written by Dave McVittie and Joe Obbish.
*
* The example website files were written by Julian Haagsma.
*
* @category Core
* @package EmPHyre
* @author Julian Haagsma <jhaagsma@gmail.com>
* @license All files are licensed under the MIT License.
* @link https://github.com/jhaagsma/emPHyre
* @since Recombined from empiresPHPframework extensions on Nov 4 2016
*/
abstract class MigrationManager
{
protected static $db;
protected static $major;
protected static $minor;
protected static $release;
protected static $build;
protected static $maxregistered;
protected static $upgradeChain = [];
public static function setDb($db = null)
{
if ($db == null) {
self::$db = Container::getDb();
} else {
self::$db = $db;
}
}//end setDb()
public static function goToLatest()
{
self::setDb();
self::checkVersion();
static::buildChain();
if (self::$maxregistered != self::dotName()) {
self::doUpgrades();
}
}//end goToLatest()
abstract protected static function buildChain();
protected static function dotName($maj = null, $min = null, $rel = null, $build = null)
{
if ($maj === null) {
$maj = static::$major ? static::$major : 0;
$min = static::$minor ? static::$minor : 0;
$rel = static::$release ? static::$release : 0;
$build = static::$build ? static::$build : null;
}
if ($build !== null) {
return "$maj.$min.$rel-$build";
} elseif ($rel !== 0) {
return "$maj.$min.$rel";
} else {
return "$maj.$min";
}
}//end dotName()
protected static function explodeDotName($dotName)
{
$partA = explode('-', $dotName, 2);
$build = isset($partA[1]) ? $partA[1] : null;
$partB = explode('.', $partA[0], 3);
return [
'major' => $partB[0],
'minor' => $partB[1],
'release' => isset($partB[2]) ? $partB[2] : 0,
'build' => $build,
];
}//end explodeDotName()
protected static function checkVersion()
{
if (!self::$db->tableExists('version')) {
self::$major = self::$minor = self::$release = 0;
self::$build = null;
self::$db->pquery(
"
CREATE TABLE IF NOT EXISTS `version` (
`version_id` SMALLINT NOT NULL AUTO_INCREMENT ,
`major` TINYINT NOT NULL DEFAULT '0' ,
`minor` TINYINT NOT NULL DEFAULT '0' ,
`rel` TINYINT NOT NULL DEFAULT '0' ,
`build` VARCHAR(8) NULL DEFAULT 'null' ,
PRIMARY KEY (`version_id`)) ENGINE = InnoDB;"
);
self::$db->pquery(
"INSERT INTO `version` (`major`,`minor`,`rel`,`build`)
VALUES (0,0,0,null)"
);
} else {
$version = self::$db->pquery(
"SELECT `major`, `minor`, `rel`, `build` FROM `version`
ORDER BY `major` DESC, `minor` DESC, `rel` DESC, `build` DESC
LIMIT 1"
)->fetchRow();
static::$major = $version['major'];
static::$minor = $version['minor'];
static::$release = $version['rel'];
static::$build = $version['build'];
}//end if
}//end checkVersion()
protected static function register($dotName, $class)
{
static::$maxregistered = $dotName;
static::$upgradeChain[] = [
'name' => $dotName,
'class' => $class,
];
}//end register()
protected static function doUpgrades()
{
// self::out(var_export(self::$upgradeChain, true));
foreach (self::$upgradeChain as $upgrade) {
$name = $upgrade['name'];
$class = $upgrade['class'];
$dotName = self::explodeDotName($name);
$maj = $dotName['major'];
$min = $dotName['minor'];
$rel = $dotName['release'];
$bui = $dotName['build'];
// self::out($name . ' --- ' . $class . ' --- ' . self::$maxregistered);
$upgrade = false;
if ($maj > static::$major) {
$upgrade = true;
} elseif ($maj < static::$major) {
continue;
} elseif ($min > static::$minor) {
$upgrade = true;
} elseif ($maj < static::$major) {
continue;
} elseif ($rel != 0 && $rel > static::$release) {
$upgrade = true;
} elseif ($rel < static::$release) {
continue;
} elseif ($bui !== null) {
// self::out($bui);
if ($bui > static::$build) {
$upgrade = true;
} elseif ($bui < static::$build) {
continue;
}
}
if (!$upgrade) {
continue;
}
// self::out($name . ' --- ' . self::$maxregistered);
$migration = new $class($name);
$worked = $migration->up();
if (!$worked) {
$migration->out("UPGRADE FAILED");
return;
// in case migrations are dependent, return here
}
$version_id = self::$db->pquery(
"INSERT INTO `version` (`major`, `minor`, `rel`, `build`)
VALUES (?, ?, ?, ?)",
$dotName['major'],
$dotName['minor'],
$dotName['release'],
$dotName['build']
)->insertid();
self::out("UPGRADED TO $name");
}//end foreach
}//end doUpgrades()
public static function out($string)
{
trigger_error($string, E_USER_NOTICE);
}//end out()
}//end class