summaryrefslogtreecommitdiff
path: root/bin/updatedb.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/updatedb.sh')
-rwxr-xr-xbin/updatedb.sh63
1 files changed, 45 insertions, 18 deletions
diff --git a/bin/updatedb.sh b/bin/updatedb.sh
index 7fa8cceb9..b4ed8b7ba 100755
--- a/bin/updatedb.sh
+++ b/bin/updatedb.sh
@@ -31,18 +31,15 @@ $opts = rcube_utils::get_opt(array(
));
if (empty($opts['dir'])) {
- echo "ERROR: Database schema directory not specified (--dir).\n";
- exit(1);
+ rcube::raise_error("Database schema directory not specified (--dir).", false, true);
}
if (empty($opts['package'])) {
- echo "ERROR: Database schema package name not specified (--package).\n";
- exit(1);
+ rcube::raise_error("Database schema package name not specified (--package).", false, true);
}
// Check if directory exists
if (!file_exists($opts['dir'])) {
- echo "ERROR: Specified database schema directory doesn't exist.\n";
- exit(1);
+ rcube::raise_error("Specified database schema directory doesn't exist.", false, true);
}
$RC = rcube::get_instance();
@@ -51,8 +48,7 @@ $DB = rcube_db::factory($RC->config->get('db_dsnw'));
// Connect to database
$DB->db_connect('w');
if (!$DB->is_connected()) {
- echo "Error connecting to database: " . $DB->is_error() . ".\n";
- exit(1);
+ rcube::raise_error("Error connecting to database: " . $DB->is_error(), false, true);
}
// Read DB schema version from database (if 'system' table exists)
@@ -113,8 +109,7 @@ if (empty($version)) {
$dir = $opts['dir'] . DIRECTORY_SEPARATOR . $DB->db_provider;
if (!file_exists($dir)) {
- echo "DDL Upgrade files for " . $DB->db_provider . " driver not found.\n";
- exit(1);
+ rcube::raise_error("DDL Upgrade files for " . $DB->db_provider . " driver not found.", false, true);
}
$dh = opendir($dir);
@@ -132,13 +127,12 @@ foreach ($result as $v) {
$error = update_db_schema($opts['package'], $v, $dir . DIRECTORY_SEPARATOR . "$v.sql");
if ($error) {
- echo "\nError in DDL upgrade $v: $error\n";
- exit(1);
+ echo "[FAILED]\n";
+ rcube::raise_error("Error in DDL upgrade $v: $error", false, true);
}
echo "[OK]\n";
}
-exit(0);
function update_db_schema($package, $version, $file)
{
@@ -186,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;
}