Skip to content

Commit

Permalink
Add DB schema migration infrastructure and script
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhowie committed Jun 24, 2011
1 parent 5cd0b33 commit 022d547
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ The proxy may at some point be able to purge old data periodically by itself. I
Upgrading
---------
When upgrading the proxy software, make sure to inspect any changes to `htdocs/config.inc.php.sample` and apply them (with customizations) as needed to your local configuration. Failure to do this might result in errant behavior.

You may additionally need to upgrade the database schema. To do this, feed the `database/migrate.sql` file to the database, either using something like phpMyAdmin or the `mysql` command-line client:

mysql -p -u user-name database-name < database/migrate.sql

This script will apply any schema changes to your existing database safely. When run against a database with the latest version of the schema, it will do nothing.
53 changes: 53 additions & 0 deletions database/migrate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*!40101 SET NAMES utf8 */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET SQL_NOTES=0 */;


DROP PROCEDURE IF EXISTS `upgrade_schema`;

DELIMITER $$

CREATE PROCEDURE `upgrade_schema`()
ret:
BEGIN
SET autocommit = 0;

-- Create settings table for users who don't have it.
CREATE TABLE IF NOT EXISTS `settings` (
`key` varchar(50) NOT NULL,
`value` varchar(50) NOT NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- Set default DB version number in case the user didn't have the table.
INSERT IGNORE INTO `settings` (`key`, `value`)
VALUES ('version', '0');

-- Get current DB version.
SELECT @version := `value` FROM `settings` WHERE `key` = 'version';

-- Upgrade paths for each version:

IF @version = '0' THEN
ALTER TABLE `submitted_work`
ADD INDEX `dashboard_status_index2` (`time`, `worker_id`);

COMMIT;
SET @version = '1';
END IF;

-- Store updated version.
UPDATE `settings` SET `value` = @version WHERE `key` = 'version';
COMMIT;

-- Message for the console.
SELECT 'Database upgraded successfully.';
END

$$

DELIMITER ;

CALL `upgrade_schema`();
DROP PROCEDURE `upgrade_schema`;
24 changes: 21 additions & 3 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ CREATE TABLE `submitted_work` (
`result` tinyint(1) NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `dashboard_status_index` (`worker_id`,`result`,`time`)
KEY `dashboard_status_index` (`worker_id`,`result`,`time`),
KEY `dashboard_status_index2` (`time`,`worker_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

Expand Down Expand Up @@ -101,12 +102,29 @@ CREATE TABLE `worker_pool` (
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

--
-- Table structure for table `settings`
--

/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `settings` (
`key` varchar(50) NOT NULL,
`value` varchar(50) NOT NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

INSERT INTO `settings` (`key`, `value`)
VALUES ('version', '1')

ON DUPLICATE KEY UPDATE `value` = '1';

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2011-04-07 10:04:01
10 changes: 10 additions & 0 deletions htdocs/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ public function indexDefaultView()
':average_interval_two' => $BTC_PROXY['average_interval']
));

$version = db_query($pdo, "
SELECT value FROM settings
WHERE `key` = 'version'
");

if ($version === false || count($version) == 0 || $version[0]['value'] != DB_SCHEMA_VERSION) {
$viewdata['old-schema'] = true;
}

return new AdminDashboardView($viewdata);
}
}
Expand Down
Binary file added htdocs/assets/icons/database_error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions htdocs/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ table.data tr.disabled:hover {
background-image: url('icons/information.png');
}

#old-schema span {
display: block;
padding-left: 20px;
background-repeat: no-repeat;
background-image: url('icons/database_error.png');
background-position: left top;
}

#old-schema {
width: 50%;
margin: 0 auto;

color: #ffe;
background-color: #440;
border: 2px solid #880;

padding: 0.5em;
}

#recent-submissions h2,
#recent-failed-submissions h2,
#worker-status h2 {
Expand Down
6 changes: 6 additions & 0 deletions htdocs/common.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

require_once(dirname(__FILE__) . '/config.inc.php');

define('DB_SCHEMA_VERSION', 1);

# This header satisfies the Section 13 requirement in the AGPL for both
# unauthenticated users and clients requesting work from the proxy.
header('X-Source-Code: https://github.com/cdhowie/Bitcoin-mining-proxy');
Expand All @@ -34,6 +36,10 @@ function db_connect() {
function db_query($pdo, $query, $args = array()) {
$q = $pdo->prepare($query);

if ($q === false) {
return false;
}

$q->execute($args);

$results = $q->fetchAll(PDO::FETCH_ASSOC);
Expand Down
4 changes: 4 additions & 0 deletions htdocs/views/admin/dashboard.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ protected function renderBody()

<div id="dashboard">

<?php if ($this->viewdata['old-schema']) { ?>
<div id="old-schema"><span>Your database schema is out of date. Please run the schema migration script (check the readme for instructions). Until you migrate your database schema, you may notice errors or poor performance.</span></div>
<?php } ?>

<div id="recent-submissions">

<h2>Recent work submissions</h2>
Expand Down

0 comments on commit 022d547

Please sign in to comment.