function definition() { $mform = $this->_form; $mform->addElement('header', 'database', get_string('dbtransfer', 'tool_dbtransfer')); $supported = array('mysqli/native', 'pgsql/native', 'mssql/native', 'oci/native', 'sqlsrv/native'); $drivers = array(); foreach ($supported as $driver) { list($dbtype, $dblibrary) = explode('/', $driver); $targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary); if ($targetdb->driver_installed() !== true) { continue; } $drivers[$driver] = $driver; } $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('text', 'dbpass', get_string('password')); $mform->addElement('text', 'prefix', get_string('dbprefix', 'install')); $mform->addElement('text', 'dbport', get_string('dbport', 'install')); $mform->addElement('text', 'dbsocket', get_string('databasesocket', 'install')); $mform->addRule('dbhost', get_string('required'), 'required', null); $mform->addRule('dbname', get_string('required'), 'required', null); $mform->addRule('dbuser', get_string('required'), 'required', null); $mform->addRule('dbpass', get_string('required'), 'required', null); $mform->addRule('prefix', get_string('required'), 'required', null); $this->add_action_buttons(false, get_string('transferdata', 'tool_dbtransfer')); }
require_capability('tool/unittest:execute', $syscontext); $baseurl = new moodle_url('/admin/tool/unittest/other/filtersettingsperformancetester.php'); $title = 'filter_get_active_in_context performance test'; $PAGE->set_url($baseurl); $PAGE->set_context($syscontext); $PAGE->navbar->add($title); $PAGE->set_title($title); $PAGE->set_heading($title); echo $OUTPUT->header(); // Complain if we get this far and $CFG->unittestprefix is not set. if (empty($CFG->unittestprefix)) { throw new coding_exception('This page requires $CFG->unittestprefix to be set in config.php.'); } $requiredtables = array('context', 'filter_active', 'filter_config'); $realdb = $DB; $testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); $testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix); $DB = $testdb; $dbman = $testdb->get_manager(); $issetup = 0; foreach ($requiredtables as $table) { if ($dbman->table_exists(new xmldb_table($table))) { $issetup++; } } switch (optional_param('action', '', PARAM_ACTION)) { case 'setup': require_sesskey(); if ($issetup == 0) { foreach ($requiredtables as $table) { $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', $table);
/** * Sets up global $DB moodle_database instance * * @global stdClass $CFG The global configuration instance. * @see config.php * @see config-dist.php * @global stdClass $DB The global moodle_database instance. * @return void|bool Returns true when finished setting up $DB. Returns void when $DB has already been set. */ function setup_ExternalDB() { global $CFG, $DB, $remotedb; // Use a custom $remotedb (and not current system's $DB) if set - code sourced from configurable // Reports plugin. $remotedbhost = get_config('report_myfeedback', 'dbhost'); $remotedbname = get_config('report_myfeedback', 'dbname'); $remotedbuser = get_config('report_myfeedback', 'dbuser'); $remotedbpass = get_config('report_myfeedback', 'dbpass'); if (empty($remotedbhost) or empty($remotedbname) or empty($remotedbuser)) { $remotedb = $DB; setup_DB(); } else { // if (!isset($CFG->dblibrary)) { $CFG->dblibrary = 'native'; // use new drivers instead of the old adodb driver names switch ($CFG->dbtype) { case 'postgres7': $CFG->dbtype = 'pgsql'; break; case 'mssql_n': $CFG->dbtype = 'mssql'; break; case 'oci8po': $CFG->dbtype = 'oci'; break; case 'mysql': $CFG->dbtype = 'mysqli'; break; } } if (!isset($CFG->dboptions)) { $CFG->dboptions = array(); } if (isset($CFG->dbpersist)) { $CFG->dboptions['dbpersist'] = $CFG->dbpersist; } if (!($remotedb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary))) { throw new dml_exception('dbdriverproblem', "Unknown driver {$CFG->dblibrary}/{$CFG->dbtype}"); } try { $remotedb->connect($remotedbhost, $remotedbuser, $remotedbpass, $remotedbname, $CFG->prefix, $CFG->dboptions); } catch (moodle_exception $e) { if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { $body = "Connection error: " . $CFG->wwwroot . "\n\nInfo:" . "\n\tError code: " . $e->errorcode . "\n\tDebug info: " . $e->debuginfo . "\n\tServer: " . $_SERVER['SERVER_NAME'] . " (" . $_SERVER['SERVER_ADDR'] . ")"; if (file_exists($CFG->dataroot . '/emailcount')) { $fp = @fopen($CFG->dataroot . '/emailcount', 'r'); $content = @fread($fp, 24); @fclose($fp); if (time() - (int) $content > 600) { //email directly rather than using messaging @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, $body); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } else { //email directly rather than using messaging @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, $body); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } // rethrow the exception throw $e; } $CFG->dbfamily = $remotedb->get_dbfamily(); // TODO: BC only for now return true; } return false; }
/** * Sets up global $DB moodle_database instance * * @global stdClass $CFG The global configuration instance. * @see config.php * @see config-dist.php * @global stdClass $DB The global moodle_database instance. * @return void|bool Returns true when finished setting up $DB. Returns void when $DB has already been set. */ function vmoodle_setup_DB($vmoodle) { global $CFG; if (!isset($vmoodle->vdblogin)) { $vmoodle->vdblogin = ''; } if (!isset($vmoodle->vdbpass)) { $vmoodle->vdbpass = ''; } if (!isset($vmoodle->vdbname)) { $vmoodle->vdbname = ''; } if (!isset($vmoodle->dblibrary)) { $vmoodle->dblibrary = 'native'; // Use new drivers instead of the old adodb driver names. switch ($vmoodle->vdbtype) { case 'postgres7': $vmoodle->vdbtype = 'pgsql'; break; case 'mssql_n': $vmoodle->vdbtype = 'mssql'; break; case 'oci8po': $vmoodle->vdbtype = 'oci'; break; case 'mysql': $vmoodle->vdbtype = 'mysqli'; break; } } if (!isset($vmoodle->dboptions)) { $vmoodle->dboptions = array(); } if (isset($vmoodle->vdbpersist)) { $vmoodle->dboptions['dbpersist'] = $vmoodle->vdbpersist; } if (!($vdb = moodle_database::get_driver_instance($vmoodle->vdbtype, $vmoodle->dblibrary))) { throw new dml_exception('dbdriverproblem', "Unknown driver {$vmoodle->dblibrary}/{$vmoodle->dbtype}"); } $vdb->connect($vmoodle->vdbhost, $vmoodle->vdblogin, $vmoodle->vdbpass, $vmoodle->vdbname, $vmoodle->vdbprefix, $vmoodle->dboptions); $vmoodle->vdbfamily = $vdb->get_dbfamily(); // TODO: BC only for now return $vdb; }
/** * Sets up global $DB moodle_database instance * @return void */ function setup_DB() { global $CFG, $DB; if (isset($DB)) { return; } if (!isset($CFG->dbuser)) { $CFG->dbuser = ''; } if (!isset($CFG->dbpass)) { $CFG->dbpass = ''; } if (!isset($CFG->dbname)) { $CFG->dbname = ''; } if (!isset($CFG->dblibrary)) { switch ($CFG->dbtype) { case 'postgres7': $CFG->dbtype = 'pgsql'; // continue, no break here // continue, no break here case 'pgsql': $CFG->dblibrary = 'native'; break; case 'mysql': if (!extension_loaded('mysqli')) { $CFG->dblibrary = 'adodb'; break; } $CFG->dbtype = 'mysqli'; // continue, no break here // continue, no break here case 'mysqli': $CFG->dblibrary = 'native'; break; default: // the rest of drivers is not converted yet - keep adodb for now $CFG->dblibrary = 'adodb'; } } if (!isset($CFG->dboptions)) { $CFG->dboptions = array(); } if (isset($CFG->dbpersist)) { $CFG->dboptions['dbpersist'] = $CFG->dbpersist; } if (!($DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary))) { throw new dml_exception('dbdriverproblem', "Unknown driver {$CFG->dblibrary}/{$CFG->dbtype}"); } try { $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions); } catch (moodle_exception $e) { if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { if (file_exists($CFG->dataroot . '/emailcount')) { $fp = @fopen($CFG->dataroot . '/emailcount', 'r'); $content = @fread($fp, 24); @fclose($fp); if (time() - (int) $content > 600) { @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, 'Connection error: ' . $CFG->wwwroot); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } else { @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, 'Connection error: ' . $CFG->wwwroot); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } // rethrow the exception throw $e; } $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now return true; }
/** * Initialises the database connection for the 'otherdb' cache type. * @return moodle_database the DB connection to use. */ protected static function get_other_db() { if (!is_null(self::$otherdb)) { return self::$otherdb; } $dboptions = array(); if (!empty(self::$config->cascachedbsocket)) { $dboptions['dbsocket'] = true; } self::$otherdb = moodle_database::get_driver_instance(self::$config->cascachedbtype, self::$config->cascachedblibrary); self::$otherdb->connect(self::$config->cascachedbhost, self::$config->cascachedbuser, self::$config->cascachedbpass, self::$config->cascachedbname, self::$config->cascachedbprefix, $dboptions); return self::$otherdb; }
$checked = $config->dbsocket ? 'checked="checked' : ''; echo '<div class="formrow"><label for="id_dbsocket" class="formlabel">' . $strdbsocket . '</label>'; echo '<input type="hidden" value="0" name="dbsocket" />'; echo '<input type="checkbox" id="id_dbsocket" value="1" name="dbsocket" ' . $checked . ' class="forminput" />'; echo '</div>'; } echo '<div class="hint">' . $hint_database . '</div>'; echo '</div>'; install_print_footer($config); die; } if ($config->stage == INSTALL_DATABASETYPE) { /// Finally ask for DB type install_print_header($config, get_string('database', 'install'), get_string('databasetypehead', 'install'), get_string('databasetypesub', 'install')); // TODO: move this PHP5 code to lib/installib.php so that this file parses in PHP4 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'), 'oci' => moodle_database::get_driver_instance('oci', 'native')); echo '<div class="userinput">'; echo '<div class="formrow"><label class="formlabel" for="dbtype">' . get_string('dbtype', 'install') . '</label>'; echo '<select id="dbtype" name="dbtype" class="forminput">'; $disabled = array(); $options = array(); foreach ($databases as $type => $database) { if ($database->driver_installed() !== true) { $disabled[$type] = $database; continue; } echo '<option value="' . s($type) . '">' . $database->get_name() . '</option>'; } if ($disabled) { echo '<optgroup label="' . s(get_string('notavailable')) . '">'; foreach ($disabled as $type => $database) {
if ($config->stage == INSTALL_DATABASETYPE) { $CFG->early_install_lang = false; // Finally ask for DB type install_print_header($config, get_string('database', 'install'), get_string('databasetypehead', 'install'), get_string('databasetypesub', 'install')); $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), 'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'), 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'), 'oci' => moodle_database::get_driver_instance('oci', 'native'), 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver ); echo '<div class="userinput">'; echo '<div class="formrow"><label class="formlabel" for="dbtype">'.get_string('dbtype', 'install').'</label>'; echo '<select id="dbtype" name="dbtype" class="forminput">'; $disabled = array(); $options = array(); foreach ($databases as $type=>$database) { if ($database->driver_installed() !== true) { $disabled[$type] = $database; continue; } echo '<option value="'.s($type).'">'.$database->get_name().'</option>'; } if ($disabled) {
public function test_concurrent_temp_tables() { $DB = $this->tdb; // Do not use global $DB! $dbman = $this->tdb->get_manager(); // Define 2 records. $record1 = (object) array('course' => 1, 'secondname' => '11 important', 'intro' => '111 important'); $record2 = (object) array('course' => 2, 'secondname' => '22 important', 'intro' => '222 important'); // Create temp table1 and insert 1 record (in DB). $table = $this->tables['test_table1']; $dbman->create_temp_table($table); $this->assertTrue($dbman->table_exists('test_table1')); $inserted = $DB->insert_record('test_table1', $record1); // Switch to new connection. $cfg = $DB->export_dbconfig(); if (!isset($cfg->dboptions)) { $cfg->dboptions = array(); } $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); $dbman2 = $DB2->get_manager(); $this->assertFalse($dbman2->table_exists('test_table1')); // Temp table not exists in DB2. // Create temp table1 and insert 1 record (in DB2). $table = $this->tables['test_table1']; $dbman2->create_temp_table($table); $this->assertTrue($dbman2->table_exists('test_table1')); $inserted = $DB2->insert_record('test_table1', $record2); $dbman2->drop_table($table); // Drop temp table before closing DB2. $this->assertFalse($dbman2->table_exists('test_table1')); $DB2->dispose(); // Close DB2. $this->assertTrue($dbman->table_exists('test_table1')); // Check table continues existing for DB. $dbman->drop_table($table); // Drop temp table. $this->assertFalse($dbman->table_exists('test_table1')); }
require_capability('moodle/site:config', $context); require_sesskey(); navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section' => 'logsettingdatabase'))); admin_externalpage_setup('logstoredbtestsettings'); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('testingsettings', 'logstore_database')); // NOTE: this is not localised intentionally, admins are supposed to understand English at least a bit... raise_memory_limit(MEMORY_HUGE); $dbtable = get_config('logstore_database', 'dbtable'); if (empty($dbtable)) { echo $OUTPUT->notification('External table not specified.', 'notifyproblem'); die; } $dbdriver = get_config('logstore_database', 'dbdriver'); list($dblibrary, $dbtype) = explode('/', $dbdriver); if (!($db = \moodle_database::get_driver_instance($dbtype, $dblibrary, true))) { echo $OUTPUT->notification("Unknown driver {$dblibrary}/{$dbtype}", "notifyproblem"); die; } $olddebug = $CFG->debug; $olddisplay = ini_get('display_errors'); ini_set('display_errors', '1'); $CFG->debug = DEBUG_DEVELOPER; error_reporting($CFG->debug); $dboptions = array(); $dboptions['dbpersist'] = get_config('logstore_database', 'dbpersist'); $dboptions['dbsocket'] = get_config('logstore_database', 'dbsocket'); $dboptions['dbport'] = get_config('logstore_database', 'dbport'); $dboptions['dbschema'] = get_config('logstore_database', 'dbschema'); $dboptions['dbcollation'] = get_config('logstore_database', 'dbcollation'); try {
} else { spl_autoload_register('core_component::classloader'); } require $CFG->dirroot . '/version.php'; $CFG->target_release = $release; \core\session\manager::init_empty_session(); global $SESSION; global $USER; global $COURSE; $COURSE = new stdClass(); $COURSE->id = 1; global $SITE; $SITE = $COURSE; define('SITEID', 1); //Database types $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), 'mariadb' => moodle_database::get_driver_instance('mariadb', 'native'), 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'), 'oci' => moodle_database::get_driver_instance('oci', 'native'), 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), 'mssql' => moodle_database::get_driver_instance('mssql', 'native')); foreach ($databases as $type => $database) { if ($database->driver_installed() !== true) { unset($databases[$type]); } } if (empty($databases)) { $defaultdb = ''; } else { reset($databases); $defaultdb = key($databases); } // now get cli options list($options, $unrecognized) = cli_get_params(array('chmod' => isset($distro->directorypermissions) ? sprintf('%04o', $distro->directorypermissions) : '2777', 'lang' => $CFG->lang, 'wwwroot' => '', 'dataroot' => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))) . '/moodledata') : $distro->dataroot, 'dbtype' => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, 'dbhost' => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, 'dbname' => 'moodle', 'dbuser' => empty($distro->dbuser) ? 'root' : $distro->dbuser, 'dbpass' => '', 'dbport' => '', 'dbsocket' => '', 'prefix' => 'mdl_', 'fullname' => '', 'shortname' => '', 'summary' => '', 'adminuser' => 'admin', 'adminpass' => '', 'adminemail' => '', 'non-interactive' => false, 'agree-license' => false, 'allow-unstable' => false, 'help' => false), array('h' => 'help')); $interactive = empty($options['non-interactive']); // set up language
/** * Returns list of fully working database drivers present in system. * @return array */ public static function get_drivers() { return array('' => get_string('choosedots'), 'native/mysqli' => \moodle_database::get_driver_instance('mysqli', 'native')->get_name(), 'native/mariadb' => \moodle_database::get_driver_instance('mariadb', 'native')->get_name(), 'native/pgsql' => \moodle_database::get_driver_instance('pgsql', 'native')->get_name(), 'native/oci' => \moodle_database::get_driver_instance('oci', 'native')->get_name(), 'native/sqlsrv' => \moodle_database::get_driver_instance('sqlsrv', 'native')->get_name(), 'native/mssql' => \moodle_database::get_driver_instance('mssql', 'native')->get_name()); }
public function test_session_locks() { $DB = $this->tdb; $dbman = $DB->get_manager(); // Open second connection $cfg = $DB->export_dbconfig(); if (!isset($cfg->dboptions)) { $cfg->dboptions = array(); } $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); // Testing that acquiring a lock efectively locks // Get a session lock on connection1 $rowid = rand(100, 200); $timeout = 1; $DB->get_session_lock($rowid, $timeout); // Try to get the same session lock on connection2 try { $DB2->get_session_lock($rowid, $timeout); $DB2->release_session_lock($rowid); // Should not be excuted, but here for safety $this->fail('An Exception is missing, expected due to session lock acquired.'); } catch (exception $e) { $this->assertTrue($e instanceof dml_sessionwait_exception); $DB->release_session_lock($rowid); // Release lock on connection1 } // Testing that releasing a lock efectively frees // Get a session lock on connection1 $rowid = rand(100, 200); $timeout = 1; $DB->get_session_lock($rowid, $timeout); // Release the lock on connection1 $DB->release_session_lock($rowid); // Get the just released lock on connection2 $DB2->get_session_lock($rowid, $timeout); // Release the lock on connection2 $DB2->release_session_lock($rowid); $DB2->dispose(); }
/** * Sets up global $DB moodle_database instance * * @global object * @global object * @return void */ function setup_DB() { global $CFG, $DB; if (isset($DB)) { return; } if (!isset($CFG->dbuser)) { $CFG->dbuser = ''; } if (!isset($CFG->dbpass)) { $CFG->dbpass = ''; } if (!isset($CFG->dbname)) { $CFG->dbname = ''; } if (!isset($CFG->dblibrary)) { $CFG->dblibrary = 'native'; // use new drivers instead of the old adodb driver names switch ($CFG->dbtype) { case 'postgres7': $CFG->dbtype = 'pgsql'; break; case 'mssql_n': $CFG->dbtype = 'mssql'; break; case 'oci8po': $CFG->dbtype = 'oci'; break; case 'mysql': $CFG->dbtype = 'mysqli'; break; } } if (!isset($CFG->dboptions)) { $CFG->dboptions = array(); } if (isset($CFG->dbpersist)) { $CFG->dboptions['dbpersist'] = $CFG->dbpersist; } if (!($DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary))) { throw new dml_exception('dbdriverproblem', "Unknown driver {$CFG->dblibrary}/{$CFG->dbtype}"); } try { $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions); } catch (moodle_exception $e) { if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { if (file_exists($CFG->dataroot . '/emailcount')) { $fp = @fopen($CFG->dataroot . '/emailcount', 'r'); $content = @fread($fp, 24); @fclose($fp); if (time() - (int) $content > 600) { //email directly rather than using messaging @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, 'Connection error: ' . $CFG->wwwroot); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } else { //email directly rather than using messaging @mail($CFG->emailconnectionerrorsto, 'WARNING: Database connection error: ' . $CFG->wwwroot, 'Connection error: ' . $CFG->wwwroot); $fp = @fopen($CFG->dataroot . '/emailcount', 'w'); @fwrite($fp, time()); } } // rethrow the exception throw $e; } $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now return true; }
/** * Returns list of fully working database drivers present in system. * @return array */ function tool_dbtransfer_get_drivers() { global $CFG; $files = new RegexIterator(new DirectoryIterator("{$CFG->libdir}/dml"), '|^.*_moodle_database\\.php$|'); $drivers = array(); foreach ($files as $file) { $matches = null; preg_match('|^([a-z0-9]+)_([a-z]+)_moodle_database\\.php$|', $file->getFilename(), $matches); if (!$matches) { continue; } $dbtype = $matches[1]; $dblibrary = $matches[2]; if ($dbtype === 'sqlite3') { // Blacklist unfinished drivers. continue; } $targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary, false); if ($targetdb->driver_installed() !== true) { continue; } $driver = $dbtype . '/' . $dblibrary; $drivers[$driver] = $targetdb->get_name(); } return $drivers; }
} if (!isset($options['dbport'])) { cli_heading(get_string('dbport', 'install')); $options['dbport'] = cli_input(get_string('clitypevalue', 'admin')); } if ($CFG->ostype !== 'WINDOWS') { if (!isset($options['dbsocket'])) { cli_heading(get_string('databasesocket', 'install')); $options['dbsocket'] = cli_input(get_string('clitypevalue', 'admin')); } } $a = (object) array('dbtypefrom' => $CFG->dbtype, 'dbtype' => $options['dbtype'], 'dbname' => $options['dbname'], 'dbhost' => $options['dbhost']); cli_heading(get_string('transferringdbto', 'tool_dbtransfer', $a)); // Try target DB connection. $problem = ''; $targetdb = moodle_database::get_driver_instance($options['dbtype'], $options['dblibrary']); $dboptions = array(); if ($options['dbport']) { $dboptions['dbport'] = $options['dbport']; } if ($options['dbsocket']) { $dboptions['dbsocket'] = $options['dbsocket']; } try { $targetdb->connect($options['dbhost'], $options['dbuser'], $options['dbpass'], $options['dbname'], $options['prefix'], $dboptions); if ($targetdb->get_tables()) { $problem .= get_string('targetdatabasenotempty', 'tool_dbtransfer'); } } catch (moodle_exception $e) { $problem .= $e->debuginfo . "\n\n"; $problem .= get_string('notargetconectexception', 'tool_dbtransfer');
/** * Setup the Database. * * @return bool */ protected function init() { if (isset($this->extdb)) { return !empty($this->extdb); } $dbdriver = $this->get_config('dbdriver'); if (empty($dbdriver)) { $this->extdb = false; return false; } list($dblibrary, $dbtype) = explode('/', $dbdriver); if (!($db = \moodle_database::get_driver_instance($dbtype, $dblibrary, true))) { debugging("Unknown driver {$dblibrary}/{$dbtype}", DEBUG_DEVELOPER); $this->extdb = false; return false; } $dboptions = array(); $dboptions['dbpersist'] = $this->get_config('dbpersist', '0'); $dboptions['dbsocket'] = $this->get_config('dbsocket', ''); $dboptions['dbport'] = $this->get_config('dbport', ''); $dboptions['dbschema'] = $this->get_config('dbschema', ''); $dboptions['dbcollation'] = $this->get_config('dbcollation', ''); try { $db->connect($this->get_config('dbhost'), $this->get_config('dbuser'), $this->get_config('dbpass'), $this->get_config('dbname'), false, $dboptions); $tables = $db->get_tables(); if (!in_array($this->get_config('dbtable'), $tables)) { debugging('Cannot find the specified table', DEBUG_DEVELOPER); $this->extdb = false; return false; } } catch (\moodle_exception $e) { debugging('Cannot connect to external database: ' . $e->getMessage(), DEBUG_DEVELOPER); $this->extdb = false; return false; } $this->extdb = $db; return true; }
function test_concurent_transactions() { // Notes about this test: // 1- MySQL needs to use one engine with transactions support (InnoDB). // 2- MSSQL needs to have enabled versioning for read committed // transactions (ALTER DATABASE xxx SET READ_COMMITTED_SNAPSHOT ON) $DB = $this->tdb; $dbman = $DB->get_manager(); $table = $this->get_test_table(); $tablename = $table->getName(); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table); $transaction = $DB->start_delegated_transaction(); $data = (object) array('course' => 1); $this->assertEqual(0, $DB->count_records($tablename)); $DB->insert_record($tablename, $data); $this->assertEqual(1, $DB->count_records($tablename)); //open second connection $cfg = $DB->export_dbconfig(); if (!isset($cfg->dboptions)) { $cfg->dboptions = array(); } $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); // second instance should not see pending inserts $this->assertEqual(0, $DB2->count_records($tablename)); $data = (object) array('course' => 2); $DB2->insert_record($tablename, $data); $this->assertEqual(1, $DB2->count_records($tablename)); // first should see the changes done from second $this->assertEqual(2, $DB->count_records($tablename)); // now commit and we should see it finally in second connections $transaction->allow_commit(); $this->assertEqual(2, $DB2->count_records($tablename)); $DB2->dispose(); }
/** * Call this statically to connect to the DB using the unittest prefix, instantiate * the unit test db, store it as a member variable, instantiate $this and use it as the new global $DB. */ public static function instantiate() { global $CFG, $DB; UnitTestDB::$real_db = clone $DB; if (empty($CFG->unittestprefix)) { print_error("prefixnotset", 'tool_unittest'); } if (empty(UnitTestDB::$DB)) { UnitTestDB::$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); UnitTestDB::$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix); } $manager = UnitTestDB::$DB->get_manager(); if (!$manager->table_exists('user')) { print_error('tablesnotsetup', 'tool_unittest'); } $DB = new UnitTestDB(); }
* @copyright 2008 Petr Skoda * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('NO_OUTPUT_BUFFERING', true); require '../../../config.php'; require_once 'locallib.php'; require_once 'database_transfer_form.php'; require_login(); admin_externalpage_setup('tooldbtransfer'); // Create the form $form = new database_transfer_form(); // If we have valid input. if ($data = $form->get_data()) { // Connect to the other database. list($dbtype, $dblibrary) = explode('/', $data->driver); $targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary); $dboptions = array(); if ($data->dbport) { $dboptions['dbport'] = $data->dbport; } if ($data->dbsocket) { $dboptions['dbsocket'] = $data->dbsocket; } if (!$targetdb->connect($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname, $data->prefix, $dboptions)) { throw new dbtransfer_exception('notargetconectexception', null, "{$CFG->wwwroot}/{$CFG->admin}/tool/dbtransfer/"); } if ($targetdb->get_tables()) { throw new dbtransfer_exception('targetdatabasenotempty', null, "{$CFG->wwwroot}/{$CFG->admin}/tool/dbtransfer/"); } // Start output. echo $OUTPUT->header();
public function test_concurrent_autosaves() { // This test simulates the following scenario: // 1. Student opens a page of the quiz in two separate browser. // 2. Autosave starts in both at the same time. // In this situation, one autosave will work, and the other one will // get a unique key violation error. This is OK. global $DB; // Open second connection $cfg = $DB->export_dbconfig(); if (!isset($cfg->dboptions)) { $cfg->dboptions = array(); } $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); // Since we need to commit our transactions in a given order, close the // standard unit test transaction. $this->preventResetByRollback(); $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $question = $generator->create_question('shortanswer', null, array('category' => $cat->id)); // Start attempt at a shortanswer question. $q = question_bank::load_question($question->id); $this->start_attempt_at_question($q, 'deferredfeedback', 1); $this->save_quba(); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); $this->check_step_count(1); // Start to process an autosave on $DB. $transaction = $DB->start_delegated_transaction(); $this->load_quba($DB); $this->process_autosave(array('answer' => 'autosaved response 1')); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); $this->check_step_count(2); $this->save_quba($DB); // Don't commit the transaction yet. // Now process a real submit on $DB2 (using a different response). $transaction2 = $DB2->start_delegated_transaction(); $this->load_quba($DB2); $this->process_autosave(array('answer' => 'autosaved response 2')); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); $this->check_step_count(2); // Now commit the first transaction. $transaction->allow_commit(); // Now commit the other transaction. $this->setExpectedException('dml_write_exception'); $this->save_quba($DB2); $transaction2->allow_commit(); // Now re-load and check how that is re-displayed. $this->load_quba(); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); $this->check_step_count(2); $this->render(); $this->check_output_contains_text_input('answer', 'autosaved response 1'); $this->check_output_contains_hidden_input(':sequencecheck', 1); $DB2->dispose(); }