Permalink
Browse files

MDL-34441 add maintenance mode to db migration and improve lang strings

  • Loading branch information...
1 parent 56db97b commit 7b293e01260af898d5d32e20d706748202baef07 @skodak skodak committed Jul 22, 2012
@@ -41,18 +41,18 @@ protected function definition() {
$mform = $this->_form;
- $mform->addElement('header', 'database', get_string('dbtransfer', 'tool_dbtransfer'));
+ $mform->addElement('header', 'database', get_string('targetdatabase', 'tool_dbtransfer'));
$drivers = tool_dbtransfer_get_drivers();
$drivers = array_reverse($drivers, true);
$drivers[''] = get_string('choosedots');
$drivers = array_reverse($drivers, true);
$mform->addElement('select', 'driver', get_string('dbtype', 'install'), $drivers);
- $mform->addElement('text', 'dbhost', get_string('dbhost', 'install'));
- $mform->addElement('text', 'dbname', get_string('database', 'install'));
- $mform->addElement('text', 'dbuser', get_string('user'));
- $mform->addElement('passwordunmask', 'dbpass', get_string('password'));
+ $mform->addElement('text', 'dbhost', get_string('databasehost', 'install'));
+ $mform->addElement('text', 'dbname', get_string('databasename', 'install'));
+ $mform->addElement('text', 'dbuser', get_string('databaseuser', 'install'));
+ $mform->addElement('passwordunmask', 'dbpass', get_string('databasepass', 'install'));
$mform->addElement('text', 'prefix', get_string('dbprefix', 'install'));
$mform->addElement('text', 'dbport', get_string('dbport', 'install'));
if ($CFG->ostype !== 'WINDOWS') {
@@ -70,6 +70,11 @@ protected function definition() {
$mform->addRule('prefix', get_string('required'), 'required', null);
}
+ $mform->addElement('header', 'database', get_string('options', 'tool_dbtransfer'));
+
+ $mform->addElement('advcheckbox', 'enablemaintenance', get_string('enablemaintenance', 'tool_dbtransfer'));
+ $mform->addHelpButton('enablemaintenance', 'enablemaintenance', 'tool_dbtransfer');
+
$this->add_action_buttons(false, get_string('transferdata', 'tool_dbtransfer'));
}
@@ -53,23 +53,38 @@
$problem .= get_string('targetdatabasenotempty', 'tool_dbtransfer');
}
} catch (moodle_exception $e) {
- $problem .= html_writer::tag('h3', get_string('notargetconectexception', 'tool_dbtransfer'));
- $problem .= $e->getMessage().'<br />'.$e->debuginfo;
+ $problem .= get_string('notargetconectexception', 'tool_dbtransfer').'<br />'.$e->debuginfo;
}
if ($problem === '') {
// Scroll down to the bottom when finished.
$PAGE->requires->js_init_code("window.scrollTo(0, 5000000);");
+ // Enable CLI maintenance mode if requested.
+ if ($data->enablemaintenance) {
+ $PAGE->set_pagelayout('maintenance');
+ tool_dbtransfer_create_maintenance_file();
+ }
+
// Start output.
echo $OUTPUT->header();
$data->dbtype = $dbtype;
echo $OUTPUT->heading(get_string('transferringdbto', 'tool_dbtransfer', $data));
// Do the transfer.
- $feedback = new html_list_progress_trace();
- tool_dbtransfer_transfer_database($DB, $targetdb, $feedback);
- $feedback->finished();
+ $CFG->tool_dbransfer_migration_running = true;
+ try {
+ $feedback = new html_list_progress_trace();
+ tool_dbtransfer_transfer_database($DB, $targetdb, $feedback);
+ $feedback->finished();
+ } catch (Exception $e) {
+ if ($data->enablemaintenance) {
+ tool_dbtransfer_maintenance_callback();
+ }
+ unset($CFG->tool_dbransfer_migration_running);
+ throw $e;
+ }
+ unset($CFG->tool_dbransfer_migration_running);
// Finish up.
echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
@@ -82,7 +97,10 @@
// Otherwise display the settings form.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('transferdbtoserver', 'tool_dbtransfer'));
-echo '<p>', get_string('transferdbintro', 'tool_dbtransfer'), "</p>\n\n";
+
+$info = format_text(get_string('transferdbintro', 'tool_dbtransfer'), FORMAT_MARKDOWN);
+echo $OUTPUT->box($info);
+
$form->display();
if ($problem !== '') {
echo $OUTPUT->box($problem, 'generalbox error');
@@ -15,22 +15,27 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * Strings for component 'tool_generator', language 'en', branch 'MOODLE_22_STABLE'
+ * Strings for component 'tool_generator', language 'en'.
*
* @package tool_dbtransfer
* @copyright 2011 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
+$string['climigrationnotice'] = 'Database migration in progress, please wait until the migration completes and server administrator updates configuration and deletes the $CFG->dataroot/climaintenance.html file.';
$string['convertinglogdisplay'] = 'Converting log display actions';
$string['dbexport'] = 'Database export';
-$string['dbtransfer'] = 'Database transfer';
+$string['dbtransfer'] = 'Database migration';
+$string['enablemaintenance'] = 'Enable maintenance mode';
+$string['enablemaintenance_help'] = 'This option enables maintanance mode during and after the database migration, it prevents access of all users until the migration is completed. Please note that administrator has to manually delete $CFG->dataroot/climaintenance.html file after updating config.php settings to resume normal operation.';
$string['exportdata'] = 'Export data';
$string['notargetconectexception'] = 'Can not connect target database, sorry.';
+$string['options'] = 'Options';
$string['pluginname'] = 'Database transfer';
+$string['targetdatabase'] = 'Target database';
$string['targetdatabasenotempty'] = 'Target database must not contain any tables with given prefix!';
$string['transferdata'] = 'Transfer data';
-$string['transferdbintro'] = 'This script will transfer the entire contents of this database to another database server.';
+$string['transferdbintro'] = 'This script will transfer the entire contents of this database to another database server. It is often used for migration of data to different database type.';
$string['transferdbtoserver'] = 'Transfer this Moodle database to another server';
$string['transferringdbto'] = 'Transferring this database to {$a->dbtype} database {$a->dbname} on {$a->dbhost}';
@@ -158,3 +158,52 @@ function tool_dbtransfer_get_drivers() {
return $drivers;
}
+
+/**
+ * Create CLI maintenance file to prevent all access.
+ */
+function tool_dbtransfer_create_maintenance_file() {
+ global $CFG;
+
+ register_shutdown_function('tool_dbtransfer_maintenance_callback');
+
+ $options = new stdClass();
+ $options->trusted = false;
+ $options->noclean = false;
+ $options->smiley = false;
+ $options->filter = false;
+ $options->para = true;
+ $options->newlines = false;
+
+ $message = format_text(get_string('climigrationnotice', 'tool_dbtransfer'), FORMAT_MARKDOWN, $options);
+ $message = bootstrap_renderer::early_error_content($message, '', '', array());
+ $html = <<<OET
+<!DOCTYPE html>
+<html>
+<header><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><header/>
+<body>$message</body>
+</html>
+OET;
+
+ file_put_contents("$CFG->dataroot/climaintenance.html", $html);
+ @chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions);
+}
+
+/**
+ * This callback is responsible for unsetting maintenance mode
+ * if the migration is interrupted.
+ */
+function tool_dbtransfer_maintenance_callback() {
+ global $CFG;
+
+ if (empty($CFG->tool_dbransfer_migration_running)) {
+ // Migration was finished properly - keep the maintenance file in place.
+ return;
+ }
+
+ if (file_exists("$CFG->dataroot/climaintenance.html")) {
+ // Failed migration, revert to normal site operation.
+ unlink("$CFG->dataroot/climaintenance.html");
+ error_log('tool_dbtransfer: Interrupted database migration detected, switching off CLI maintenance mode.');
+ }
+}

0 comments on commit 7b293e0

Please sign in to comment.