diff options
author | Aleksander Machniak <alec@alec.pl> | 2013-04-27 18:31:40 +0200 |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2013-04-27 18:31:40 +0200 |
commit | 399db1b647e14947e97a865c09215969f56a7efe (patch) | |
tree | 4801fc0482e3e6fe94fd6668dae03b13926b9d17 /bin | |
parent | d7fcd8ce422a24f794d18e2212163690bf3ca753 (diff) |
Add db_prefix configuration option in place of db_table_*/db_sequence_* options
Make possible to use db_prefix for schema initialization in Installer (#1489067)
Fix updatedb.sh script so it recognizes also table prefix for external DDL files
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/indexcontacts.sh | 2 | ||||
-rwxr-xr-x | bin/moduserprefs.sh | 2 | ||||
-rwxr-xr-x | bin/updatedb.sh | 43 |
3 files changed, 40 insertions, 7 deletions
diff --git a/bin/indexcontacts.sh b/bin/indexcontacts.sh index 413dc4b3e..c85a535a7 100755 --- a/bin/indexcontacts.sh +++ b/bin/indexcontacts.sh @@ -35,7 +35,7 @@ if (!$db->is_connected() || $db->is_error()) { } // iterate over all users -$sql_result = $db->query("SELECT user_id FROM " . $RCMAIL->config->get('db_table_users', 'users')." WHERE 1=1"); +$sql_result = $db->query("SELECT user_id FROM " . $db->table_name('users') . " ORDER BY user_id"); while ($sql_result && ($sql_arr = $db->fetch_assoc($sql_result))) { echo "Indexing contacts for user " . $sql_arr['user_id'] . "..."; diff --git a/bin/moduserprefs.sh b/bin/moduserprefs.sh index 049372c2f..9bbc885a6 100755 --- a/bin/moduserprefs.sh +++ b/bin/moduserprefs.sh @@ -61,7 +61,7 @@ if ($args['user']) $query = 'user_id=' . intval($args['user']); // iterate over all users -$sql_result = $db->query("SELECT * FROM " . $rcmail->config->get('db_table_users', 'users')." WHERE $query"); +$sql_result = $db->query("SELECT * FROM " . $db->table_name('users') . " WHERE $query"); while ($sql_result && ($sql_arr = $db->fetch_assoc($sql_result))) { echo "Updating prefs for user " . $sql_arr['user_id'] . "..."; 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; } |