Thomas B.
2013-10-24 4e4c2511bc00cfc0214acab63b097efe142187a7
bin/updatedb.sh
@@ -27,22 +27,19 @@
$opts = rcube_utils::get_opt(array(
    'v' => 'version',
    'd' => 'dir',
    'l' => 'label',
    'p' => 'package',
));
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['label'])) {
  echo "ERROR: Database schema label not specified (--label).\n";
  exit(1);
if (empty($opts['package'])) {
    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,19 +48,18 @@
// 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)
if (in_array('system', (array)$DB->list_tables())) {
// Read DB schema version from database (if 'system' table exists)
if (in_array($DB->table_name('system'), (array)$DB->list_tables())) {
    $DB->query("SELECT " . $DB->quote_identifier('value')
        ." FROM " . $DB->quote_identifier('system')
        ." FROM " . $DB->quote_identifier($DB->table_name('system'))
        ." WHERE " . $DB->quote_identifier('name') ." = ?",
        $opts['label'] . '-version');
        $opts['package'] . '-version');
    $row     = $DB->fetch_array();
    $version = $row[0];
    $version = preg_replace('/[^0-9]/', '', $row[0]);
}
// DB version not found, but release version is specified
@@ -76,13 +72,20 @@
        '0.2-alpha'  => 2008040500,
        '0.2-beta'   => 2008060900,
        '0.2-stable' => 2008092100,
        '0.2.1'      => 2008092100,
        '0.2.2'      => 2008092100,
        '0.3-stable' => 2008092100,
        '0.3.1'      => 2009090400,
        '0.4-beta'   => 2009103100,
        '0.4'        => 2010042300,
        '0.4.1'      => 2010042300,
        '0.4.2'      => 2010042300,
        '0.5-beta'   => 2010100600,
        '0.5'        => 2010100600,
        '0.5.1'      => 2010100600,
        '0.5.2'      => 2010100600,
        '0.5.3'      => 2010100600,
        '0.5.4'      => 2010100600,
        '0.6-beta'   => 2011011200,
        '0.6'        => 2011011200,
        '0.7-beta'   => 2011092800,
@@ -90,6 +93,7 @@
        '0.7.1'      => 2011111600,
        '0.7.2'      => 2011111600,
        '0.7.3'      => 2011111600,
        '0.7.4'      => 2011111600,
        '0.8-beta'   => 2011121400,
        '0.8-rc'     => 2011121400,
        '0.8.0'      => 2011121400,
@@ -97,21 +101,22 @@
        '0.8.2'      => 2011121400,
        '0.8.3'      => 2011121400,
        '0.8.4'      => 2011121400,
        '0.8.5'      => 2011121400,
        '0.8.6'      => 2011121400,
        '0.9-beta'   => 2012080700,
    );
    $version = $map[$opts['version']];
}
// Assume last version before the system table was added
// Assume last version before the 'system' table was added
if (empty($version)) {
    $version = 2012080700;
}
$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);
@@ -126,18 +131,17 @@
foreach ($result as $v) {
    echo "Updating database schema ($v)... ";
    $error = update_db_schema($opts['label'], $v, $dir . DIRECTORY_SEPARATOR . "$v.sql");
    $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($label, $version, $file)
function update_db_schema($package, $version, $file)
{
    global $DB;
@@ -150,7 +154,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;
@@ -159,19 +163,73 @@
        }
    }
    $DB->query("UPDATE " . $DB->quote_identifier('system')
    // escape if 'system' table does not exist
    if ($version < 2013011000) {
        return;
    }
    $system_table = $DB->quote_identifier($DB->table_name('system'));
    $DB->query("UPDATE " . $system_table
        ." SET " . $DB->quote_identifier('value') . " = ?"
        ." WHERE " . $DB->quote_identifier('name') . " = ?",
        $version, $label . '-version');
        $version, $package . '-version');
    if (!$DB->is_error() && !$DB->affected_rows()) {
        $DB->query("INSERT INTO " . $DB->quote_identifier('system')
        $DB->query("INSERT INTO " . $system_table
            ." (" . $DB->quote_identifier('name') . ", " . $DB->quote_identifier('value') . ")"
            ." VALUES (?, ?)",
            $label . '-version', $version);
            $package . '-version', $version);
    }
    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;
}
?>