Skip to content

Commit

Permalink
add database upgrade script, update version in initial database
Browse files Browse the repository at this point in the history
  • Loading branch information
r4sas committed Apr 25, 2018
1 parent cdba6ce commit b007bfa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sql/000_base_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ CREATE TABLE IF NOT EXISTS `settings` (
UNIQUE KEY `setting` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '1.0.2');
INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '1.0.3');

CREATE TABLE IF NOT EXISTS `shares` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
Expand Down
36 changes: 36 additions & 0 deletions upgrade/definitions/1.0.2_to_1.0.3.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
function run_103() {
// Ugly but haven't found a better way
global $setting, $config, $coin_address, $user, $mysqli;

// Version information
$db_version_old = '1.0.2'; // What version do we expect
$db_version_new = '1.0.3'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed

// Upgrade specific variables
$aSql[] = "
ALTER TABLE `statistics_shares`
CHANGE `valid` `valid` BIGINT(20) NOT NULL DEFAULT '0',
CHANGE `invalid` `invalid` BIGINT(20) NOT NULL DEFAULT '0',
CHANGE `pplns_valid` `pplns_valid` BIGINT(20) NOT NULL DEFAULT '0',
CHANGE `pplns_invalid` `pplns_invalid` BIGINT(20) NOT NULL DEFAULT '0';
";
$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '" . $db_version_new . "' 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);
}
}
}
}
?>

0 comments on commit b007bfa

Please sign in to comment.