From f9cba03c689cd8af2f085daffc327bd3eda71bd4 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 21 Mar 2014 15:12:13 +0100 Subject: [PATCH] [IMPROVED] Advanced upgrade structure --- upgrade/db_version_0.0.8.php | 36 ---------------------- upgrade/definitions/0.0.7_to_0.0.8.inc.php | 32 +++++++++++++++++++ upgrade/run_upgrades.php | 33 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 36 deletions(-) delete mode 100755 upgrade/db_version_0.0.8.php create mode 100755 upgrade/definitions/0.0.7_to_0.0.8.inc.php create mode 100755 upgrade/run_upgrades.php diff --git a/upgrade/db_version_0.0.8.php b/upgrade/db_version_0.0.8.php deleted file mode 100755 index de58dd30..00000000 --- a/upgrade/db_version_0.0.8.php +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/php -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); - } - } -} -?> diff --git a/upgrade/definitions/0.0.7_to_0.0.8.inc.php b/upgrade/definitions/0.0.7_to_0.0.8.inc.php new file mode 100755 index 00000000..623af7e1 --- /dev/null +++ b/upgrade/definitions/0.0.7_to_0.0.8.inc.php @@ -0,0 +1,32 @@ +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); + } + } + } +} +?> diff --git a/upgrade/run_upgrades.php b/upgrade/run_upgrades.php new file mode 100755 index 00000000..dd949c8a --- /dev/null +++ b/upgrade/run_upgrades.php @@ -0,0 +1,33 @@ +#!/usr/bin/php +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); +?>