diff --git a/README.txt b/README.txt index 03a89ef..4bc9fa7 100644 --- a/README.txt +++ b/README.txt @@ -23,6 +23,8 @@ INSTALLATION: * Configure and use the module at admin/config/system/backup_migrate. OPTIONAL: +* To drop all tables before import, expand “Advanced options” panel + under the “Restore” and “Saved backups” tabs and tick the option. * Enable token.module to allow token replacement in backup file names. * To Backup to Amazon S3: - Download the S3 library from diff --git a/includes/destinations.db.mysql.inc b/includes/destinations.db.mysql.inc index af64385..1b8a1cf 100644 --- a/includes/destinations.db.mysql.inc +++ b/includes/destinations.db.mysql.inc @@ -212,6 +212,15 @@ public function _restore_db_from_file($file, $settings) { $num = 0; if ($file->open() && $conn = $this->_get_db_connection()) { + if ($settings->filters['utils_drop_all_tables']) { + // Drop all existing tables. + $all_tables = $this->_get_tables(); + $table_names = array_map(create_function('$a', 'return "`" . $a["name"] . "`";'), $all_tables); + $table_list = join(', ', $table_names); + $stmt = $conn->prepare("DROP TABLE IF EXISTS $table_list;\n"); + $stmt->execute(); + } + // Read one line at a time and run the query. while ($line = $this->_read_sql_command_from_file($file)) { if (_backup_migrate_check_timeout()) { diff --git a/includes/filters.utils.inc b/includes/filters.utils.inc index c488ec0..893ba04 100644 --- a/includes/filters.utils.inc +++ b/includes/filters.utils.inc @@ -119,6 +119,12 @@ public function restore_settings_form($settings) { '#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))), '#description' => t('Message to show visitors when the site is in off-line mode.'), ); + $form['advanced']['utils_drop_all_tables'] = array( + '#type' => 'checkbox', + '#title' => t('Drop all tables before import (MySQL only)'), + '#default_value' => !empty($settings['utils_drop_all_tables']) ? $settings['utils_drop_all_tables'] : NULL, + '#description' => t('Drop all existing database tables before restoring the backup. This option is currently available on MySQL servers only.'), + ); $form['advanced']['use_cli'] = array( "#type" => "checkbox", "#title" => t("Use cli commands"), diff --git a/includes/sources.db.mysql.inc b/includes/sources.db.mysql.inc index 56c3a76..f203048 100644 --- a/includes/sources.db.mysql.inc +++ b/includes/sources.db.mysql.inc @@ -200,6 +200,14 @@ public function _restore_db_from_file($file, $settings) { $num = 0; if ($file->open() && $conn = $this->_get_db_connection()) { + if ($settings->filters['utils_drop_all_tables']) { + // Drop all existing tables. + $all_tables = $this->_get_tables(); + $table_names = array_map(create_function('$a', 'return "`" . $a["name"] . "`";'), $all_tables); + $table_list = join(', ', $table_names); + $stmt = $conn->prepare("DROP TABLE IF EXISTS $table_list;\n"); + $stmt->execute(); + } // Read one line at a time and run the query. while ($line = $this->_read_sql_command_from_file($file)) { if (_backup_migrate_check_timeout()) {