[IMPROVED] Advanced upgrade structure

This commit is contained in:
Sebastian Grewe 2014-03-21 15:12:13 +01:00
parent d72b4f6a51
commit f9cba03c68
3 changed files with 65 additions and 36 deletions

View File

@ -1,36 +0,0 @@
#!/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');
// Version information
$db_version_old = '0.0.7'; // What version do we expect
$db_version_new = '0.0.8'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed
// Upgrade specific variables
$dDifficulty = POW(2, ($config['difficulty'] - 16));
$aSql[] = "UPDATE " . $statistics->getTableName() . " SET valid = valid * $dDifficulty, invalid = invalid * $dDifficulty, pplns_valid = pplns_valid * $dDifficulty, pplns_invalid = pplns_invalid * $dDifficulty";
$aSql[] = "UPDATE " . $block->getTableName() . " SET shares = shares * $dDifficulty";
$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.8' WHERE name = 'DB_VERSION'";
if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {
// Run the upgrade
echo 'Starting database migration to version ' . $db_version_new . 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);
}
}
}
?>

View File

@ -0,0 +1,32 @@
<?php
function run_008() {
// Ugly but haven't found a better way
global $setting, $config, $statistics, $block, $mysqli;
// Version information
$db_version_old = '0.0.7'; // What version do we expect
$db_version_new = '0.0.8'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed
// Upgrade specific variables
$dDifficulty = POW(2, ($config['difficulty'] - 16));
$aSql[] = "UPDATE " . $statistics->getTableName() . " SET valid = valid / $dDifficulty, invalid = invalid / $dDifficulty, pplns_valid = pplns_valid / $dDifficulty, pplns_invalid = pplns_invalid / $dDifficulty";
$aSql[] = "UPDATE " . $block->getTableName() . " SET shares = shares / $dDifficulty";
$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.8' WHERE name = 'DB_VERSION'";
if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {
// Run the upgrade
echo '- Starting database migration to version ' . $db_version_new . 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);
}
}
}
}
?>

33
upgrade/run_upgrades.php Executable file
View File

@ -0,0 +1,33 @@
#!/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 function
function run_db_upgrade($db_version_now) {
// 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";
$run();
run_db_upgrade($db_version_to);
}
}
// Initial caller
run_db_upgrade($db_version_now);
?>