Aleksander Machniak
2015-02-28 23bcfc5849a4f5ce0a3e8afbd1fa38011da9b33a
bin/updatedb.sh
@@ -31,18 +31,15 @@
));
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 @@
// 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)
@@ -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,
@@ -113,8 +116,7 @@
$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,33 +134,21 @@
    $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)
{
    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();
        }
    }
@@ -182,20 +172,6 @@
    }
    return $DB->is_error();
}
function fix_table_names($sql)
{
    global $DB;
    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);
        }
    }
    return $sql;
}
?>