MDL-82395 tool_xmldb: Table layout update

This commit is contained in:
Safat 2024-07-25 03:07:42 +10:00 committed by Huong Nguyen
parent 4cfa5bca7c
commit a11b849ebe
No known key found for this signature in database
GPG key ID: 40D88AB693A3E72A

View file

@ -36,6 +36,11 @@ class new_table_from_mysql extends XMLDBAction {
function init() { function init() {
parent::init(); parent::init();
global $DB;
if ($DB->get_dbfamily() !== 'mysql') {
throw new moodle_exception('DB family not supported');
}
// Set own custom attributes // Set own custom attributes
// Get needed strings // Get needed strings
@ -55,8 +60,6 @@ class new_table_from_mysql extends XMLDBAction {
function invoke() { function invoke() {
parent::invoke(); parent::invoke();
$result = true;
// Set own core attributes // Set own core attributes
$this->does_generate = ACTION_GENERATE_HTML; $this->does_generate = ACTION_GENERATE_HTML;
@ -125,11 +128,15 @@ class new_table_from_mysql extends XMLDBAction {
// If table, retrofit information and, if everything works, // If table, retrofit information and, if everything works,
// go to the table edit action // go to the table edit action
} else { } else {
// Get some params (table is mandatory here) // Get some params (table is mandatory here).
$tableparam = required_param('table', PARAM_CLEAN); $tableparam = required_param('table', PARAM_ALPHAEXT);
$afterparam = required_param('after', PARAM_CLEAN); $afterparam = required_param('after', PARAM_ALPHAEXT);
// Create one new xmldb_table if (empty($tableparam) || empty($afterparam)) {
throw new moodle_exception('Invalid param value detected.');
}
// Create one new xmldb_table.
$table = new xmldb_table(strtolower(trim($tableparam))); $table = new xmldb_table(strtolower(trim($tableparam)));
$table->setComment($table->getName() . ' table retrofitted from MySQL'); $table->setComment($table->getName() . ' table retrofitted from MySQL');
// Get fields info from ADODb // Get fields info from ADODb
@ -147,7 +154,6 @@ class new_table_from_mysql extends XMLDBAction {
// Get PK, UK and indexes info from ADODb // Get PK, UK and indexes info from ADODb
$dbindexes = $DB->get_indexes($tableparam); $dbindexes = $DB->get_indexes($tableparam);
if ($dbindexes) { if ($dbindexes) {
$lastkey = NULL; //To temp store the last key processed
foreach ($dbindexes as $indexname => $dbindex) { foreach ($dbindexes as $indexname => $dbindex) {
// Add the indexname to the array // Add the indexname to the array
$dbindex['name'] = $indexname; $dbindex['name'] = $indexname;
@ -156,9 +162,6 @@ class new_table_from_mysql extends XMLDBAction {
$key = new xmldb_key(strtolower($dbindex['name'])); $key = new xmldb_key(strtolower($dbindex['name']));
// Set key with info retrofitted // Set key with info retrofitted
$key->setFromADOKey($dbindex); $key->setFromADOKey($dbindex);
// Set default comment to PKs
if ($key->getType() == XMLDB_KEY_PRIMARY) {
}
// Add key to the table // Add key to the table
$table->addKey($key); $table->addKey($key);
@ -172,18 +175,17 @@ class new_table_from_mysql extends XMLDBAction {
} }
} }
} }
// Finally, add the whole retroffited table to the structure // Finally, add the whole retrofitted table to the structure in the place specified.
// in the place specified
$structure->addTable($table, $afterparam); $structure->addTable($table, $afterparam);
} }
// Launch postaction if exists (leave this here!) // Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) { if ($this->getPostAction()) {
return $this->launch($this->getPostAction()); return $this->launch($this->getPostAction());
} }
// Return ok if arrived here // Return ok if arrived here
return $result; return true;
} }
} }