| | |
| | | $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); |
| | | } |
| | | |
| | |
| | | exit(1); |
| | | } |
| | | |
| | | // Read DB schema version from database (if system table exists) |
| | | // 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('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 |
| | |
| | | $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; |
| | | } |
| | |
| | | |
| | | 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(0); |
| | | |
| | | function update_db_schema($label, $version, $file) |
| | | function update_db_schema($package, $version, $file) |
| | | { |
| | | global $DB; |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | $DB->query("UPDATE " . $DB->quote_identifier('system') |
| | | $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(); |