JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add db_upgrade system
authorJason Woofenden <jason@jasonwoof.com>
Thu, 12 Nov 2015 21:03:31 +0000 (16:03 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Thu, 12 Nov 2015 21:03:31 +0000 (16:03 -0500)
config.php
inc/db_upgrade.php [new file with mode: 0644]
inc/wfpl

index e60cbd7..59b1c98 100644 (file)
@@ -29,3 +29,9 @@ require_once(DOCROOT . 'inc/misc.php');
 
 # Connect to the database
 db_connect(WFPL_DB, WFPL_DB_USER, WFPL_DB_PASS);
+
+# upgrade db (only) on special trigger (assures only one upgrade at a time)
+if (isset($_SERVER['HTTP_X_UPGRADE_DB_NOW'])) {
+       require_once(DOCROOT . 'inc/db_upgrade.php');
+       db_upgrade();
+}
diff --git a/inc/db_upgrade.php b/inc/db_upgrade.php
new file mode 100644 (file)
index 0000000..07c633f
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+require_once(DOCROOT . 'inc/wfpl/persistent.php');
+
+# instructions:
+#
+# 1.   Write the next db_upgrade_to_XX() at the bottom of this file
+#
+# 2.   curl -H "X-UPGRADE-DB-NOW: y" http://this-site.com/
+
+function db_upgrade() {
+       header("Content-Type: text/plain");
+       ini_set('display_errors', '1');
+       ini_set('html_errors', '0');
+
+       # initialize where we store the current db schema version
+       db_send_query('create table if not exists persistent ( k varchar(30) binary not null default "", v varchar(255) binary not null default "", primary key (k)) CHARSET=utf8;');
+
+       # now we can just do the needed upgrades
+       $cur = persistent_get('db_schema_version');
+       if ($cur === null) {
+               $cur = 0;
+       }
+
+       echo "db schema at version $cur\n\n";
+
+       $upgraded = false;
+       for ($next = $cur + 1; function_exists("db_upgrade_$next"); $next += 1) {
+               echo "upgrading DB to version $next...\n";
+               flush();
+               call_user_func("db_upgrade_$next");
+               persistent_set('db_schema_version', $next);
+               echo "done\n\n";
+               flush();
+               $upgraded = true;
+       }
+
+       if ($upgraded) {
+               echo "Upgrades complete\n";
+       } else {
+               echo "No upgrades needed\n";
+       }
+
+       exit();
+}
+
+#function db_upgrade_1() {
+#      db_send_query("alter table foo add bar int(11) not null default 0");
+#}
index f8a8d44..1cc2fd7 160000 (submodule)
--- a/inc/wfpl
+++ b/inc/wfpl
@@ -1 +1 @@
-Subproject commit f8a8d44598bdb6e4446920b8d10d1bc62916c9aa
+Subproject commit 1cc2fd791d55bca1a63afbeb2628dd1a8bd2ba3f