summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2014-04-29 11:34:43 +0200
committerAleksander Machniak <alec@alec.pl>2014-04-29 11:34:43 +0200
commit90f7aa9e39e744e708a597ff6d1ac83f7c4fcef5 (patch)
treee1f87bcbf54f21efd6eccbfda2a7c012e3f9ec56 /bin
parentd5038ee5d910ffe200e5cad60ca407f615b2691d (diff)
Fix varius db_prefix issues (#1489839)
- Move DDL script execution code to rcube_db class(es). - Improve prefix replacement code, so index names are also modified
Diffstat (limited to 'bin')
-rwxr-xr-xbin/updatedb.sh64
1 files changed, 3 insertions, 61 deletions
diff --git a/bin/updatedb.sh b/bin/updatedb.sh
index 1f5e18434..f504634f1 100755
--- a/bin/updatedb.sh
+++ b/bin/updatedb.sh
@@ -146,20 +146,9 @@ function update_db_schema($package, $version, $file)
global $DB;
// read DDL file
- if ($lines = file($file)) {
- $sql = '';
- foreach ($lines as $line) {
- if (preg_match('/^--/', $line) || trim($line) == '')
- continue;
-
- $sql .= $line . "\n";
- if (preg_match('/(;|^GO)$/', trim($line))) {
- @$DB->query(fix_table_names($sql));
- $sql = '';
- if ($error = $DB->is_error()) {
- return $error;
- }
- }
+ if ($sql = file_get_contents($file)) {
+ if (!$DB->exec_script($sql)) {
+ return $DB->is_error();
}
}
@@ -185,51 +174,4 @@ function update_db_schema($package, $version, $file)
return $DB->is_error();
}
-function fix_table_names($sql)
-{
- 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);
-
- // 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;
-}
-
?>