From 2491c6240cad60e68916a79c7a0689bedc2cefd9 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 15 Apr 2010 03:28:05 -0400
Subject: [PATCH] Add minimal database schema check to installer and update script

---
 installer/rcube_install.php |   43 ++++++++++
 bin/update.sh               |   24 +++++
 installer/test.php          |    9 -
 UPGRADING                   |  110 +--------------------------
 4 files changed, 70 insertions(+), 116 deletions(-)

diff --git a/UPGRADING b/UPGRADING
index 6362e0c..329983d 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -1,12 +1,9 @@
 UPGRADING instructions
 ======================
 
-First you should remove all subfolders from /program/localization/
-because most language codes have changed in 0.2-alpha. This way you
-can make sure that no old localization files remain on your disk.
-
-Then follow these instructions if upgrading from a previous version
-of RoundCube Webmail.
+Follow these instructions if upgrading from a previous version
+of Roundcube Webmail. We recommend to carefully backup the existing
+installation as well as the database before executig the following steps.
 
 1. Replace index.php and all files in
    - ./bin/
@@ -16,8 +13,8 @@
    - ./skins/default/
    - ./plugins/
 2. Run ./bin/update.sh from the commandline OR
-   open http://url-to-roundcube/installer/ in a browser. To enable
-   the latter one, you have to temporary set 'enable_installer' to true
+   open http://url-to-roundcube/installer/ in a browser and choose "3 Test config".
+   To enable the latter one, you have to temporary set 'enable_installer' to true
    in your local config/main.inc.php file.
 3. Let the update script/installer check your configuration and
    update your config files as suggested by the updater.
@@ -28,100 +25,3 @@
 6. Check .htaccess settings (some php settings could become required)
 
 
-For manually upgrading your RoundCube installation follow the instructions
-that match the currently installed version:
-
-from version 0.2-alpha
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /installer/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql
-  below the line "-- Updates from version 0.2-alpha"
-* check the config/main.inc.php.dist for new configuration 
-  options and add them to your config 
-  WARNING: 'skin_path' option was replaced by 'skin' option
-* WARNING: 'db_backend' option has been removed, now only
-  PEAR::MDB2 driver is supported
-
-
-from version 0.1.1
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql
-  below the line "-- Updates from version 0.1.1"
-* check the config/main.inc.php.dist for new configuration 
-  options and add them to your config 
-
-
-from version 0.1-stable
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql
-* check the config/main.inc.php.dist for new configuration options
-  and add them to your config 
-
-
-from version 0.1-rc2
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql
-
-
-from version 0.1-rc1
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* If you have LDAP servers configured you should re-configure
-  the config entries using the template given in /config/main.inc.php.dist
-
-
-from version 0.1-beta2
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql or
-  re-initalize the database with [yourdbtype].initial.sql
-* add these lines to /config/main.inc.php
-   $rcmail_config['draft_autosave'] = 300;
-   $rcmail_config['date_today'] = 'H:i';
-* If you have LDAP servers configured you should re-configure
-  the config entries using the template given in /config/main.inc.php.dist
-
-
-form version 0.1-beta
-----------------------------------------
-* replace index.php
-* replace all files in folder /bin/
-* replace all files in folder /program/
-* replace all files in folder /skins/default/
-* run all commands in SQL/[yourdbtype].update.sql or
-  re-initalize the database with [yourdbtype].initial.sql
-* add this line to /config/db.inc.php
-   $rcmail_config['db_persistent'] = false;
-* add these lines to /config/main.inc.php
-   $rcmail_config['drafts_mbox'] = 'Drafts';
-   $rcmail_config['junk_mbox'] = 'Junk';
-   $rcmail_config['product_name'] = 'RoundCube Webmail';
-   $rcmail_config['read_when_deleted'] = false;
-   $rcmail_config['enable_spellcheck'] = false;
-   $rcmail_config['protect_default_folders'] = false;
-* replace the following line from /config/main.inc.php
-   @include($_SERVER['HTTP_HOST'].'.inc.php');
-  with 
-   $rcmail_config['include_host_config'] = false;
diff --git a/bin/update.sh b/bin/update.sh
index c93d92d..7a4d1cf 100755
--- a/bin/update.sh
+++ b/bin/update.sh
@@ -12,7 +12,10 @@
 $RCI->load_config();
 
 if ($RCI->configured) {
+  $success = true;
+  
   if ($messages = $RCI->check_config()) {
+    $success = false;
     $err = 0;
 
     // list missing config options
@@ -100,9 +103,26 @@
       echo "Please fix your config files and run this script again!\n";
       echo "See ya.\n";
     }
-
   }
-  else {
+
+  // check database schema
+  if ($RCI->config['db_dsnw']) {
+    $DB = new rcube_mdb2($RCI->config['db_dsnw'], '', false);
+    $DB->db_connect('w');
+    if ($db_error_msg = $DB->is_error()) {
+      echo "Error connecting to database: $db_error_msg\n";
+      $success = false;
+    }
+    else if ($RCI->db_schema_check($DB, false)) {
+      $updatefile = INSTALL_PATH . 'SQL/' . $DB->db_provider . '.update.sql';
+      echo "WARNING: Database schema needs to be updated!\n";
+      echo "Open $updatefile and execute all queries that are superscribed with the currently installed version number\n";
+      $success = false;
+    }
+  }
+  
+  
+  if ($success) {
     echo "This instance of RoundCube is up-to-date.\n";
     echo "Have fun!\n";
   }
diff --git a/installer/rcube_install.php b/installer/rcube_install.php
index b0d59fd..ae568d2 100644
--- a/installer/rcube_install.php
+++ b/installer/rcube_install.php
@@ -40,8 +40,8 @@
     'addrbook_show_images' => 'show_images',
   );
   
-  // these config options are optional or can be set to null
-  var $required_config = array('db_dsnw', 'des_key');
+  // these config options are required for a working system
+  var $required_config = array('db_dsnw', 'db_table_contactgroups', 'db_table_contactgroupmembers', 'des_key');
   
   /**
    * Constructor
@@ -325,6 +325,43 @@
     }
   }
   
+  /**
+   * Compare the local database schema with the reference schema
+   * required for this version of RoundCube
+   *
+   * @param boolean True if the schema schould be updated
+   * @return boolean True if the schema is up-to-date, false if not or an error occured
+   */
+  function db_schema_check($DB, $update = false)
+  {
+    if (!$this->configured)
+      return false;
+    
+    // simple ad hand-made db schema
+    $db_schema = array(
+      'users' => array(),
+      'identities' => array(),
+      'contacts' => array(),
+      'contactgroups' => array(),
+      'contactgroupmembers' => array(),
+      'cache' => array(),
+      'messages' => array(),
+      'session' => array(),
+    );
+    
+    $errors = array();
+    
+    // check list of tables
+    $existing_tables = $DB->list_tables();
+    foreach ($db_schema as $table => $cols) {
+      if (!in_array($this->config['db_table_'.$table], $existing_tables))
+        $errors[] = "Missing table ".$table;
+      
+      // TODO: check cols and indices
+    }
+    
+    return !empty($errors) ? $errors : false;
+  }
   
   /**
    * Compare the local database schema with the reference schema
@@ -333,7 +370,7 @@
    * @param boolean True if the schema schould be updated
    * @return boolean True if the schema is up-to-date, false if not or an error occured
    */
-  function db_schema_check($update = false)
+  function mdb2_schema_check($update = false)
   {
     if (!$this->configured)
       return false;
diff --git a/installer/test.php b/installer/test.php
index 1495715..66698d7 100644
--- a/installer/test.php
+++ b/installer/test.php
@@ -164,15 +164,12 @@
         echo '<p><input type="submit" name="initdb" value="Initialize database" /></p>';
         $db_working = false;
     }
-  /*
-    else if (!$RCI->db_schema_check($update = !empty($_POST['updatedb']))) {
+    else if ($RCI->db_schema_check($DB, $update = !empty($_POST['updatedb']))) {
         $RCI->fail('DB Schema', "Database schema differs");
-        
-        echo $update ? '<p class="warning">Failed to update the database schema! Please manually execute the SQL statements from the SQL/*.update.sql file on your database</p>' :
-          '<p><input type="submit" name="updatedb" value="Update schema now" /></p>';
+        $updatefile = INSTALL_PATH . 'SQL/' . $DB->db_provider . '.update.sql';
+        echo '<p class="warning">Please manually execute the SQL statements from '.$updatefile.' on your database</p>';
         $db_working = false;
     }
-  */
     else {
         $RCI->pass('DB Schema');
         echo '<br />';

--
Gitblit v1.9.1