From 15e4c89ce6bbb381f4ec1070de0f0bd4a8574e33 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Thu, 21 Mar 2013 09:58:43 -0400
Subject: [PATCH] Consider alternative table names from config when running DB updates

---
 bin/updatedb.sh |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/bin/updatedb.sh b/bin/updatedb.sh
index c856e0d..1c7e1c7 100755
--- a/bin/updatedb.sh
+++ b/bin/updatedb.sh
@@ -56,7 +56,7 @@
 }
 
 // Read DB schema version from database (if 'system' table exists)
-if (in_array('system', (array)$DB->list_tables())) {
+if (in_array($DB->table_name('system'), (array)$DB->list_tables())) {
     $DB->query("SELECT " . $DB->quote_identifier('value')
         ." FROM " . $DB->quote_identifier($DB->table_name('system'))
         ." WHERE " . $DB->quote_identifier('name') ." = ?",
@@ -150,7 +150,7 @@
 
             $sql .= $line . "\n";
             if (preg_match('/(;|^GO)$/', trim($line))) {
-                @$DB->query($sql);
+                @$DB->query(fix_table_names($sql));
                 $sql = '';
                 if ($error = $DB->is_error()) {
                     return $error;
@@ -181,4 +181,18 @@
     return $DB->is_error();
 }
 
+function fix_table_names($sql)
+{
+    global $DB;
+
+    foreach (array('users','identities','contacts','contactgroups','contactgroupmembers','session','cache','cache_index','cache_index','cache_messages','dictionary','searches','system') as $table) {
+        $real_table = $DB->table_name($table);
+        if ($real_table != $table) {
+            $sql = preg_replace("/([^a-z0-9_])$table([^a-z0-9_])/i", "\\1$real_table\\2", $sql);
+        }
+    }
+
+    return $sql;
+}
+
 ?>

--
Gitblit v1.9.1