forked from MPOS/php-mpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_upgrades.php
executable file
·59 lines (52 loc) · 1.73 KB
/
run_upgrades.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
#!/usr/bin/php
<?php
/* Upgarde script for https://github.com/MPOS/php-mpos/issues/1981 */
// Change to working directory
chdir(dirname(__FILE__));
// Include all settings and classes
require_once('shared.inc.php');
// Fetch current version
$db_version_now = $setting->getValue('DB_VERSION');
// Helper functions
function run_db_upgrade($db_version_now) {
// Dirty, but need it here
global $setting;
// Drop caches from our settings for live data
$setting->flushCache();
// Find upgrades
$files = glob(dirname(__FILE__) . "/definitions/${db_version_now}_to_*");
if (count($files) == 0) die('No upgrade definitions found for ' . $db_version_now . PHP_EOL);
foreach ($files as $file) {
$db_version_to = preg_replace("/(.*)\/definitions\/${db_version_now}_to_/", '', $file);
$db_version_to = preg_replace("/.inc.php/", '', $db_version_to);
$run_string = preg_replace("/\./", '', $db_version_to);
if (!require_once($file)) die('Failed to load upgrade definition: ' . $file);
echo "+ Running upgrade from $db_version_now to $db_version_to" . PHP_EOL;
$run = "run_$run_string";
if (function_exists($run)) {
$run();
run_db_upgrade($db_version_to);
} else {
echo 'Could find upgrade function ' . $run . '!' . PHP_EOL;
exit(1);
}
}
}
function execute_db_upgrade($aSql) {
global $mysqli;
// Run the upgrade
echo '- Starting database migration ' . PHP_EOL;
foreach ($aSql as $sql) {
echo '- Preparing: ' . $sql . PHP_EOL;
$stmt = $mysqli->prepare($sql);
if ($stmt && $stmt->execute()) {
echo '- success' . PHP_EOL;
} else {
echo '- failed: ' . $mysqli->error . PHP_EOL;
exit(1);
}
}
}
// Initial caller
run_db_upgrade($db_version_now);
?>