Aleksander Machniak
2013-01-17 f5c7df4c324d3157ab47469f492ecdb2a4b82e93
bin/updatedb.sh
@@ -27,15 +27,15 @@
$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);
}
if (empty($opts['label'])) {
  echo "ERROR: Database schema label not specified (--label).\n";
if (empty($opts['package'])) {
  echo "ERROR: Database schema package name not specified (--package).\n";
  exit(1);
}
@@ -45,8 +45,29 @@
  exit(1);
}
// version is specified, use release-to-version map
if ($opts['version']) {
$RC = rcube::get_instance();
$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);
}
// Read DB schema version from database (if 'system' table exists)
if (in_array('system', (array)$DB->list_tables())) {
    $DB->query("SELECT " . $DB->quote_identifier('value')
        ." FROM " . $DB->quote_identifier($DB->table_name('system'))
        ." WHERE " . $DB->quote_identifier('name') ." = ?",
        $opts['package'] . '-version');
    $row     = $DB->fetch_array();
    $version = $row[0];
}
// DB version not found, but release version is specified
if (!$version && $opts['version']) {
    // Map old release version string to DB schema version
    // Note: This is for backward compat. only, do not need to be updated
    $map = array(
@@ -82,28 +103,7 @@
    $version = $map[$opts['version']];
}
$RC = rcube::get_instance();
$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);
}
// Read DB schema version from database
if (empty($version)) {
    @$DB->query("SELECT " . $DB->quote_identifier('value')
        ." FROM " . $DB->quote_identifier('system')
        ." WHERE name = ?",
        $opts['label'] . '-version');
    $row     = $DB->fetch_array();
    $version = $row[0];
}
// Assume last version without the "system" table
// Assume last version before the 'system' table was added
if (empty($version)) {
    $version = 2012080700;
}
@@ -126,7 +126,7 @@
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";
@@ -137,7 +137,7 @@
exit(0);
function update_db_schema($label, $version, $file)
function update_db_schema($package, $version, $file)
{
    global $DB;
@@ -159,16 +159,23 @@
        }
    }
    $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, $opts['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 (?, ?)",
            $opts['label'] . '-version', $version);
            $package . '-version', $version);
    }
    return $DB->is_error();