From 62d6f183c4ec41365a73f56411534540eced8cf0 Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Tue, 11 Jun 2013 20:56:06 +1000 Subject: [PATCH] MDL-40103 Backup: Move install.xml templates to inline XML --- .../dbops/backup_controller_dbops.class.php | 82 ++++++++----------- .../dbops/restore_controller_dbops.class.php | 4 +- lib/db/install.xml | 38 --------- lib/db/upgrade.php | 13 +-- version.php | 2 +- 5 files changed, 45 insertions(+), 94 deletions(-) diff --git a/backup/util/dbops/backup_controller_dbops.class.php b/backup/util/dbops/backup_controller_dbops.class.php index 68c17526d2f..2e8da16d44b 100644 --- a/backup/util/dbops/backup_controller_dbops.class.php +++ b/backup/util/dbops/backup_controller_dbops.class.php @@ -103,58 +103,44 @@ abstract class backup_controller_dbops extends backup_dbops { } public static function create_backup_ids_temp_table($backupid) { - self::create_temptable_from_real_table($backupid, 'backup_ids_template', 'backup_ids_temp'); - } - - /** - * Given one "real" tablename, create one temp table suitable for be used in backup/restore operations - */ - public static function create_temptable_from_real_table($backupid, $realtablename, $temptablename) { global $CFG, $DB; $dbman = $DB->get_manager(); // We are going to use database_manager services - // As far as xmldb objects use a lot of circular references (prev and next) and we aren't destroying - // them at all, that causes one memory leak of about 3M per backup execution, not problematic for - // individual backups but critical for automated (multiple) ones. - // So we are statically caching the xmldb_table definition here to produce the leak "only" once - static $xmldb_tables = array(); - - // Not cached, get it - if (!isset($xmldb_tables[$realtablename])) { - // Note: For now we are going to load the realtablename from core lib/db/install.xml - // that way, any change in the "template" will be applied here automatically. If this causes - // too much slow, we can always forget about the template and keep maintained the xmldb_table - // structure inline - manually - here. - // TODO: Right now, loading the whole lib/db/install.xml is "eating" 10M, we should - // change our way here in order to decrease that memory usage - $templatetablename = $realtablename; - $targettablename = $temptablename; - $xmlfile = $CFG->dirroot . '/lib/db/install.xml'; - $xmldb_file = new xmldb_file($xmlfile); - if (!$xmldb_file->fileExists()) { - throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist'); - } - $loaded = $xmldb_file->loadXMLStructure(); - if (!$loaded || !$xmldb_file->isLoaded()) { - throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??'); - } - $xmldb_structure = $xmldb_file->getStructure(); - $xmldb_table = $xmldb_structure->getTable($templatetablename); - if (is_null($xmldb_table)) { - throw new ddl_exception('ddlunknowntable', null, 'The table ' . $templatetablename . ' is not defined in file ' . $xmlfile); - } - // Clean prev & next, we are alone - $xmldb_table->setNext(null); - $xmldb_table->setPrevious(null); - // Rename - $xmldb_table->setName($targettablename); - // Cache it - $xmldb_tables[$realtablename] = $xmldb_table; - } - // Arrived here, we have the table always in static cache, get it - $xmldb_table = $xmldb_tables[$realtablename]; + $xmldb_table = new xmldb_table('backup_ids_temp'); + $xmldb_table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); // Set default backupid (not needed but this enforce any missing backupid). That's hackery in action! - $xmldb_table->getField('backupid')->setDefault($backupid); + $xmldb_table->add_field('backupid', XMLDB_TYPE_CHAR, 32, null, XMLDB_NOTNULL, null, $backupid); + $xmldb_table->add_field('itemname', XMLDB_TYPE_CHAR, 160, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('itemid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('newitemid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); + $xmldb_table->add_field('parentitemid', XMLDB_TYPE_INTEGER, 10, null, null, null, '0'); + $xmldb_table->add_field('info', XMLDB_TYPE_TEXT, 1333, null, null, null, null); + $xmldb_table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $xmldb_table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid','itemname','itemid')); + $xmldb_table->add_index('backupid_parentitemid_ix', null, array('backupid','itemname','parentitemid')); + $xmldb_table->add_index('backupid_itemname_newitemid_ix', null, array('backupid','itemname','newitemid')); + + $dbman->create_temp_table($xmldb_table); // And create it + + } + + public static function create_backup_files_temp_table($backupid) { + global $CFG, $DB; + $dbman = $DB->get_manager(); // We are going to use database_manager services + + $xmldb_table = new xmldb_table('backup_files_temp'); + $xmldb_table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + // Set default backupid (not needed but this enforce any missing backupid). That's hackery in action! + $xmldb_table->add_field('backupid', XMLDB_TYPE_CHAR, 32, null, XMLDB_NOTNULL, null, $backupid); + $xmldb_table->add_field('contextid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('component', XMLDB_TYPE_CHAR, 100, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('filearea', XMLDB_TYPE_CHAR, 50, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('itemid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null); + $xmldb_table->add_field('info', XMLDB_TYPE_TEXT, 1333, null, null, null, null); + $xmldb_table->add_field('newcontextid', XMLDB_TYPE_INTEGER, 10, null, null, null, '0'); + $xmldb_table->add_field('newitemid', XMLDB_TYPE_INTEGER, 10, null, null, null, '0'); + $xmldb_table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $xmldb_table->add_index('backupid_contextid_component_filearea_itemid_ix', null, array('backupid','contextid','component','filearea','itemid')); $dbman->create_temp_table($xmldb_table); // And create it } diff --git a/backup/util/dbops/restore_controller_dbops.class.php b/backup/util/dbops/restore_controller_dbops.class.php index 94674e71e46..09a670b0c68 100644 --- a/backup/util/dbops/restore_controller_dbops.class.php +++ b/backup/util/dbops/restore_controller_dbops.class.php @@ -111,8 +111,8 @@ abstract class restore_controller_dbops extends restore_dbops { // TODO: If not match, exception, table corresponds to another backup/restore operation return true; } - backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_ids_template', 'backup_ids_temp'); - backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_files_template', 'backup_files_temp'); + create_backup_ids_temp_table($restoreid); + create_backup_files_temp_table($restoreid); return false; } diff --git a/lib/db/install.xml b/lib/db/install.xml index e81d01ffe51..67660b3b237 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -2736,44 +2736,6 @@ - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index dd52607a514..a81d80be191 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2163,19 +2163,22 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2013051400.01); } - if ($oldversion < 2013060600.00) { + if ($oldversion < 2013061400.01) { // MDL-40103: Remove unused template tables from the database. // These are now created inline with xmldb_table - $tablestocleanup = array('temp_enroled_template','temp_log_template'); + $tablestocleanup = array('temp_enroled_template','temp_log_template','backup_files_template','backup_ids_template'); + $dbman = $DB->get_manager(); - foreach($tablestocleanup as $table) { + foreach ($tablestocleanup as $table) { $xmltable = new xmldb_table($table); - $DB->drop_table($xmltable); + if ($dbman->table_exists($xmltable)) { + $dbman->drop_table($xmltable); + } } // Main savepoint reached. - upgrade_main_savepoint(true, 2013060600.00); + upgrade_main_savepoint(true, 2013061400.01); } return true; } diff --git a/version.php b/version.php index ac9bf0b0235..4d7a76b9c5b 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2013061400.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2013061400.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes