Permalink
Browse files

Add DB schema migration infrastructure and script

  • Loading branch information...
1 parent 5cd0b33 commit 022d547f4d9691b7935fe15d71b85ecd48b56adb @cdhowie committed Jun 24, 2011
View
6 README.markdown
@@ -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.
View
53 database/migrate.sql
@@ -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`;
View
24 database/schema.sql
@@ -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 */;
@@ -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
View
10 htdocs/admin/index.php
@@ -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);
}
}
View
BIN htdocs/assets/icons/database_error.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
19 htdocs/assets/style.css
@@ -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 {
View
6 htdocs/common.inc.php
@@ -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');
@@ -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);
View
4 htdocs/views/admin/dashboard.view.php
@@ -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>

0 comments on commit 022d547

Please sign in to comment.