Skip to content
This repository
Browse code

Add DB schema migration infrastructure and script

  • Loading branch information...
commit 022d547f4d9691b7935fe15d71b85ecd48b56adb 1 parent 5cd0b33
Chris Howie authored
6 README.markdown
Source Rendered
@@ -95,3 +95,9 @@ The proxy may at some point be able to purge old data periodically by itself. I
95 95 Upgrading
96 96 ---------
97 97 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.
  98 +
  99 +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:
  100 +
  101 + mysql -p -u user-name database-name < database/migrate.sql
  102 +
  103 +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 database/migrate.sql
... ... @@ -0,0 +1,53 @@
  1 +/*!40101 SET NAMES utf8 */;
  2 +/*!40103 SET TIME_ZONE='+00:00' */;
  3 +/*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  4 +/*!40111 SET SQL_NOTES=0 */;
  5 +
  6 +
  7 +DROP PROCEDURE IF EXISTS `upgrade_schema`;
  8 +
  9 +DELIMITER $$
  10 +
  11 +CREATE PROCEDURE `upgrade_schema`()
  12 +ret:
  13 +BEGIN
  14 + SET autocommit = 0;
  15 +
  16 + -- Create settings table for users who don't have it.
  17 + CREATE TABLE IF NOT EXISTS `settings` (
  18 + `key` varchar(50) NOT NULL,
  19 + `value` varchar(50) NOT NULL,
  20 + PRIMARY KEY (`key`)
  21 + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  22 +
  23 + -- Set default DB version number in case the user didn't have the table.
  24 + INSERT IGNORE INTO `settings` (`key`, `value`)
  25 + VALUES ('version', '0');
  26 +
  27 + -- Get current DB version.
  28 + SELECT @version := `value` FROM `settings` WHERE `key` = 'version';
  29 +
  30 + -- Upgrade paths for each version:
  31 +
  32 + IF @version = '0' THEN
  33 + ALTER TABLE `submitted_work`
  34 + ADD INDEX `dashboard_status_index2` (`time`, `worker_id`);
  35 +
  36 + COMMIT;
  37 + SET @version = '1';
  38 + END IF;
  39 +
  40 + -- Store updated version.
  41 + UPDATE `settings` SET `value` = @version WHERE `key` = 'version';
  42 + COMMIT;
  43 +
  44 + -- Message for the console.
  45 + SELECT 'Database upgraded successfully.';
  46 +END
  47 +
  48 +$$
  49 +
  50 +DELIMITER ;
  51 +
  52 +CALL `upgrade_schema`();
  53 +DROP PROCEDURE `upgrade_schema`;
24 database/schema.sql
@@ -45,7 +45,8 @@ CREATE TABLE `submitted_work` (
45 45 `result` tinyint(1) NOT NULL,
46 46 `time` datetime NOT NULL,
47 47 PRIMARY KEY (`id`),
48   - KEY `dashboard_status_index` (`worker_id`,`result`,`time`)
  48 + KEY `dashboard_status_index` (`worker_id`,`result`,`time`),
  49 + KEY `dashboard_status_index2` (`time`,`worker_id`)
49 50 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
50 51 /*!40101 SET character_set_client = @saved_cs_client */;
51 52
@@ -101,6 +102,25 @@ CREATE TABLE `worker_pool` (
101 102 /*!40101 SET character_set_client = @saved_cs_client */;
102 103 /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
103 104
  105 +--
  106 +-- Table structure for table `settings`
  107 +--
  108 +
  109 +/*!40101 SET @saved_cs_client = @@character_set_client */;
  110 +/*!40101 SET character_set_client = utf8 */;
  111 +CREATE TABLE IF NOT EXISTS `settings` (
  112 + `key` varchar(50) NOT NULL,
  113 + `value` varchar(50) NOT NULL,
  114 + PRIMARY KEY (`key`)
  115 +) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  116 +/*!40101 SET character_set_client = @saved_cs_client */;
  117 +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  118 +
  119 +INSERT INTO `settings` (`key`, `value`)
  120 + VALUES ('version', '1')
  121 +
  122 + ON DUPLICATE KEY UPDATE `value` = '1';
  123 +
104 124 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
105 125 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
106 126 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
@@ -108,5 +128,3 @@ CREATE TABLE `worker_pool` (
108 128 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
109 129 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
110 130 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
111   -
112   --- Dump completed on 2011-04-07 10:04:01
10 htdocs/admin/index.php
@@ -181,6 +181,16 @@ public function indexDefaultView()
181 181 ':average_interval_two' => $BTC_PROXY['average_interval']
182 182 ));
183 183
  184 + $version = db_query($pdo, "
  185 + SELECT value FROM settings
  186 +
  187 + WHERE `key` = 'version'
  188 + ");
  189 +
  190 + if ($version === false || count($version) == 0 || $version[0]['value'] != DB_SCHEMA_VERSION) {
  191 + $viewdata['old-schema'] = true;
  192 + }
  193 +
184 194 return new AdminDashboardView($viewdata);
185 195 }
186 196 }
BIN  htdocs/assets/icons/database_error.png
19 htdocs/assets/style.css
@@ -169,6 +169,25 @@ table.data tr.disabled:hover {
169 169 background-image: url('icons/information.png');
170 170 }
171 171
  172 +#old-schema span {
  173 + display: block;
  174 + padding-left: 20px;
  175 + background-repeat: no-repeat;
  176 + background-image: url('icons/database_error.png');
  177 + background-position: left top;
  178 +}
  179 +
  180 +#old-schema {
  181 + width: 50%;
  182 + margin: 0 auto;
  183 +
  184 + color: #ffe;
  185 + background-color: #440;
  186 + border: 2px solid #880;
  187 +
  188 + padding: 0.5em;
  189 +}
  190 +
172 191 #recent-submissions h2,
173 192 #recent-failed-submissions h2,
174 193 #worker-status h2 {
6 htdocs/common.inc.php
@@ -21,6 +21,8 @@
21 21
22 22 require_once(dirname(__FILE__) . '/config.inc.php');
23 23
  24 +define('DB_SCHEMA_VERSION', 1);
  25 +
24 26 # This header satisfies the Section 13 requirement in the AGPL for both
25 27 # unauthenticated users and clients requesting work from the proxy.
26 28 header('X-Source-Code: https://github.com/cdhowie/Bitcoin-mining-proxy');
@@ -34,6 +36,10 @@ function db_connect() {
34 36 function db_query($pdo, $query, $args = array()) {
35 37 $q = $pdo->prepare($query);
36 38
  39 + if ($q === false) {
  40 + return false;
  41 + }
  42 +
37 43 $q->execute($args);
38 44
39 45 $results = $q->fetchAll(PDO::FETCH_ASSOC);
4 htdocs/views/admin/dashboard.view.php
@@ -61,6 +61,10 @@ protected function renderBody()
61 61
62 62 <div id="dashboard">
63 63
  64 +<?php if ($this->viewdata['old-schema']) { ?>
  65 +<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>
  66 +<?php } ?>
  67 +
64 68 <div id="recent-submissions">
65 69
66 70 <h2>Recent work submissions</h2>

0 comments on commit 022d547

Please sign in to comment.
Something went wrong with that request. Please try again.