summaryrefslogtreecommitdiff
path: root/bin/updatedb.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/updatedb.sh')
-rwxr-xr-xbin/updatedb.sh43
1 files changed, 38 insertions, 5 deletions
diff --git a/bin/updatedb.sh b/bin/updatedb.sh
index aeeaf6856..b4ed8b7ba 100755
--- a/bin/updatedb.sh
+++ b/bin/updatedb.sh
@@ -180,15 +180,48 @@ function update_db_schema($package, $version, $file)
function fix_table_names($sql)
{
- global $DB;
+ global $DB, $RC, $dir;
+ static $tables;
+ static $sequences;
+
+ $prefix = $RC->config->get('db_prefix');
+ $engine = $DB->db_provider;
+
+ if (empty($prefix)) {
+ return $sql;
+ }
+
+ if ($tables === null) {
+ $tables = array();
+ $sequences = array();
+
+ // read complete schema (initial) file
+ $filename = "$dir/../$engine.initial.sql";
+ $schema = @file_get_contents($filename);
- 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);
+ // find table names
+ if (preg_match_all('/CREATE TABLE (\[dbo\]\.|IF NOT EXISTS )?[`"\[\]]*([^`"\[\] \r\n]+)/i', $schema, $matches)) {
+ foreach ($matches[2] as $table) {
+ $tables[$table] = $prefix . $table;
+ }
+ }
+ // find sequence names
+ if ($engine == 'postgres' && preg_match_all('/CREATE SEQUENCE (IF NOT EXISTS )?"?([^" \n\r]+)/i', $schema, $matches)) {
+ foreach ($matches[2] as $sequence) {
+ $sequences[$sequence] = $prefix . $sequence;
+ }
}
}
+ // replace table names
+ foreach ($tables as $table => $real_table) {
+ $sql = preg_replace("/([^a-zA-Z0-9_])$table([^a-zA-Z0-9_])/", "\\1$real_table\\2", $sql);
+ }
+ // replace sequence names
+ foreach ($sequences as $sequence => $real_sequence) {
+ $sql = preg_replace("/([^a-zA-Z0-9_])$sequence([^a-zA-Z0-9_])/", "\\1$real_sequence\\2", $sql);
+ }
+
return $sql;
}