aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-09-23 17:27:09 +0000
committerben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-09-23 17:27:09 +0000
commitb35e8cbe9e9776d86be034365c1033b61497bbb0 (patch)
tree10072e2ac96e2f0c1a969782c603986f3209fad6
parent2d0bd94c643add43312294bab13789b16461878a (diff)
downloadelgg-b35e8cbe9e9776d86be034365c1033b61497bbb0.tar.gz
elgg-b35e8cbe9e9776d86be034365c1033b61497bbb0.tar.bz2
The database schema will now automatically update itself on upgrade.
git-svn-id: https://code.elgg.org/elgg/trunk@2107 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/lib/database.php47
-rw-r--r--engine/lib/version.php36
-rw-r--r--engine/schema/upgrades/2008092301.sql33
-rw-r--r--languages/en.php2
-rw-r--r--version.php4
5 files changed, 116 insertions, 6 deletions
diff --git a/engine/lib/database.php b/engine/lib/database.php
index a071a4f6f..f1b4a5871 100644
--- a/engine/lib/database.php
+++ b/engine/lib/database.php
@@ -400,7 +400,11 @@
$statement = trim($statement);
$statement = str_replace("prefix_",$CONFIG->dbprefix,$statement);
if (!empty($statement)) {
- $result = update_data($statement);
+ try {
+ $result = update_data($statement);
+ } catch (DatabaseException $e) {
+ $errors[] = $e->getMessage();
+ }
}
}
if (!empty($errors)) {
@@ -414,6 +418,47 @@
throw new DatabaseException(sprintf(elgg_echo('DatabaseException:ScriptNotFound'), $scriptlocation));
}
+ }
+
+ function db_upgrade($version) {
+
+ global $CONFIG;
+
+ // Elgg and its database must be installed to upgrade it!
+ if (!is_db_installed() || !is_installed()) return false;
+
+ $version = (int) $version;
+
+ if ($handle = opendir($CONFIG->path . 'engine/schema/upgrades/')) {
+
+ $sqlupgrades = array();
+
+ while ($sqlfile = readdir($handle)) {
+
+ if (!is_dir($CONFIG->path . 'engine/schema/upgrades/' . $sqlfile)) {
+ if (preg_match('/([0-9]*)\.sql/',$sqlfile,$matches)) {
+ $sql_version = (int) $matches[1];
+ if ($sql_version > $version) {
+ $sqlupgrades[] = $sqlfile;
+ }
+ }
+ }
+
+ }
+
+ asort($sqlupgrades);
+ if (sizeof($sqlupgrades) > 0) {
+ foreach($sqlupgrades as $sqlfile) {
+ try {
+ run_sql_script($CONFIG->path . 'engine/schema/upgrades/' . $sqlfile);
+ } catch (DatabaseException $e) {
+ error_log($e->getmessage());
+ }
+ }
+ }
+
+ }
+
}
/**
diff --git a/engine/lib/version.php b/engine/lib/version.php
index ee707ca1d..405237298 100644
--- a/engine/lib/version.php
+++ b/engine/lib/version.php
@@ -21,7 +21,7 @@
global $CONFIG;
if (@include($CONFIG->path . "version.php")) {
- if ($humanreadable) return $version;
+ if (!$humanreadable) return $version;
return $release;
}
@@ -34,9 +34,9 @@
*
* @return true|false Depending on whether or not the db version matches the code version
*/
- function db_upgrade_check() {
+ function version_upgrade_check() {
- $dbversion = (int) get_datalist('version');
+ $dbversion = (int) datalist_get('version');
$version = get_version();
if ($version > $dbversion) {
@@ -45,5 +45,35 @@
return false;
}
+
+ /**
+ * Upgrades Elgg
+ *
+ */
+ function version_upgrade() {
+
+ $dbversion = (int) datalist_get('version');
+ db_upgrade($dbversion);
+ datalist_set('version', get_version());
+ system_message(elgg_echo('upgrade:db'));
+ forward();
+ exit;
+
+ }
+
+ /**
+ * Runs an upgrade check on boot.
+ *
+ */
+ function version_boot() {
+
+ if (version_upgrade_check()) {
+ version_upgrade();
+ }
+
+ }
+
+ // Register the boot handler for version
+ register_elgg_event_handler("boot","system","version_boot");
?> \ No newline at end of file
diff --git a/engine/schema/upgrades/2008092301.sql b/engine/schema/upgrades/2008092301.sql
index d6f1a950a..2ae52e0c9 100644
--- a/engine/schema/upgrades/2008092301.sql
+++ b/engine/schema/upgrades/2008092301.sql
@@ -18,6 +18,39 @@ ALTER TABLE `prefix_users_apisessions` DEFAULT CHARACTER SET utf8;
ALTER TABLE `prefix_datalists` DEFAULT CHARACTER SET utf8;
ALTER TABLE `prefix_system_log` DEFAULT CHARACTER SET utf8;
+-- Drop keys to avoid errors (grr)
+
+ALTER TABLE `prefix_entities` DROP KEY `type`;
+ALTER TABLE `prefix_entities` DROP KEY `subtype`;
+ALTER TABLE `prefix_entities` DROP KEY `owner_guid`;
+ALTER TABLE `prefix_entities` DROP KEY `container_guid`;
+ALTER TABLE `prefix_entities` DROP KEY `access_id`;
+ALTER TABLE `prefix_entities` DROP KEY `time_created`;
+ALTER TABLE `prefix_entities` DROP KEY `time_updated`;
+
+ALTER TABLE `prefix_users_entity` DROP KEY `email`;
+ALTER TABLE `prefix_users_entity` DROP KEY `code`;
+
+ALTER TABLE `prefix_annotations` DROP KEY `entity_guid`;
+ALTER TABLE `prefix_annotations` DROP KEY `name_id`;
+ALTER TABLE `prefix_annotations` DROP KEY `value_id`;
+ALTER TABLE `prefix_annotations` DROP KEY `owner_guid`;
+ALTER TABLE `prefix_annotations` DROP KEY `access_id`;
+
+ALTER TABLE `prefix_metadata` DROP KEY `entity_guid`;
+ALTER TABLE `prefix_metadata` DROP KEY `name_id`;
+ALTER TABLE `prefix_metadata` DROP KEY `value_id`;
+ALTER TABLE `prefix_metadata` DROP KEY `owner_guid`;
+ALTER TABLE `prefix_metadata` DROP KEY `access_id`;
+
+ALTER TABLE `prefix_users_apisessions` DROP KEY `token`;
+
+ALTER TABLE `prefix_system_log` DROP KEY `object_id`;
+ALTER TABLE `prefix_system_log` DROP KEY `object_class`;
+ALTER TABLE `prefix_system_log` DROP KEY `event`;
+ALTER TABLE `prefix_system_log` DROP KEY `performed_by_guid`;
+ALTER TABLE `prefix_system_log` DROP KEY `time_created`;
+
-- New keys
ALTER TABLE `prefix_entities` ADD KEY `type` (`type`);
diff --git a/languages/en.php b/languages/en.php
index 1789184af..baf160767 100644
--- a/languages/en.php
+++ b/languages/en.php
@@ -653,6 +653,8 @@ Alternatively, you can enter your database settings below and we will try and do
'installation:usage' => "This option lets Elgg send anonymous usage statistics back to Curverider.",
'installation:usage:label' => "Send anonymous usage statistics",
'installation:view' => "Enter the view which will be used as the default for your site or leave this blank for the default view (if in doubt, leave as default):",
+
+ 'upgrade:db' => 'Your database was upgraded.',
/**
* Welcome
diff --git a/version.php b/version.php
index c35749058..4ba64e5a9 100644
--- a/version.php
+++ b/version.php
@@ -13,9 +13,9 @@
* @link http://elgg.org/
*/
- $version = 2008082101; // YYYYMMDD = Elgg Date
+ $version = 2008092301; // YYYYMMDD = Elgg Date
// XX = Interim incrementer
- $release = '1.0'; // Human-friendly version name
+ $release = '1.05'; // Human-friendly version name
?> \ No newline at end of file