Skip to content

Commit

Permalink
CONTRIB-3522 - XMLDB and upgrade process with custom exception manage…
Browse files Browse the repository at this point in the history
…ment to be more specific
  • Loading branch information
dmonllao committed Mar 14, 2012
1 parent 5c62fdc commit cb05dea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
14 changes: 7 additions & 7 deletions db/install.xml
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/admin_presets/db" VERSION="20110630" COMMENT="Admin presets block tables, to store exported/imported presets"
<XMLDB PATH="blocks/admin_presets/db" VERSION="20120314" COMMENT="Admin presets block tables, to store exported/imported presets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="admin_preset" COMMENT="Table to store presets data" NEXT="admin_preset_item">
<TABLE NAME="block_admin_presets" COMMENT="Table to store presets data" NEXT="block_admin_presets_it">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
Expand All @@ -21,7 +21,7 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="admin_preset_item" COMMENT="Table to store settings" PREVIOUS="admin_preset" NEXT="admin_preset_item_attr">
<TABLE NAME="block_admin_presets_it" COMMENT="Table to store settings" PREVIOUS="block_admin_presets" NEXT="block_admin_presets_it_a">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="adminpresetid"/>
<FIELD NAME="adminpresetid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="plugin"/>
Expand All @@ -36,7 +36,7 @@
<INDEX NAME="adminpresetid" UNIQUE="false" FIELDS="adminpresetid"/>
</INDEXES>
</TABLE>
<TABLE NAME="admin_preset_item_attr" COMMENT="For settings with attributes (extra values like 'advanced')" PREVIOUS="admin_preset_item" NEXT="admin_preset_apply">
<TABLE NAME="block_admin_presets_it_a" COMMENT="Admin presets items attributes. For settings with attributes (extra values like 'advanced')" PREVIOUS="block_admin_presets_it" NEXT="block_admin_presets_app">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="itemid"/>
<FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
Expand All @@ -50,7 +50,7 @@
<INDEX NAME="itemid" UNIQUE="false" FIELDS="itemid"/>
</INDEXES>
</TABLE>
<TABLE NAME="admin_preset_apply" COMMENT="Applied presets" PREVIOUS="admin_preset_item_attr" NEXT="admin_preset_apply_item">
<TABLE NAME="block_admin_presets_app" COMMENT="Applied presets" PREVIOUS="block_admin_presets_it_a" NEXT="block_admin_presets_app_it">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="adminpresetid"/>
<FIELD NAME="adminpresetid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="userid"/>
Expand All @@ -64,7 +64,7 @@
<INDEX NAME="adminpresetid" UNIQUE="false" FIELDS="adminpresetid"/>
</INDEXES>
</TABLE>
<TABLE NAME="admin_preset_apply_item" COMMENT="config_log relation" PREVIOUS="admin_preset_apply" NEXT="admin_preset_apply_item_attr">
<TABLE NAME="block_admin_presets_app_it" COMMENT="Admin presets applied items. To maintain the relation with config_log" PREVIOUS="block_admin_presets_app" NEXT="block_admin_presets_app_it_a">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="adminpresetapplyid"/>
<FIELD NAME="adminpresetapplyid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="configlogid"/>
Expand All @@ -78,7 +78,7 @@
<INDEX NAME="adminpresetapplyid" UNIQUE="false" FIELDS="adminpresetapplyid" PREVIOUS="configlogid"/>
</INDEXES>
</TABLE>
<TABLE NAME="admin_preset_apply_item_attr" COMMENT="Setting attributes applications" PREVIOUS="admin_preset_apply_item">
<TABLE NAME="block_admin_presets_app_it_a" COMMENT="Attributes of the applied items" PREVIOUS="block_admin_presets_app_it">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="adminpresetapplyid"/>
<FIELD NAME="adminpresetapplyid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="id" NEXT="configlogid"/>
Expand Down
41 changes: 41 additions & 0 deletions db/upgrade.php
Expand Up @@ -22,5 +22,46 @@ function xmldb_block_admin_presets_upgrade($oldversion, $block) {
upgrade_block_savepoint(true, 2011063000, 'admin_presets');
}

// Renaming DB tables
if ($oldversion < 2012031401) {

$tablenamechanges = array('admin_preset' => 'block_admin_presets',
'admin_preset_apply' => 'block_admin_presets_app',
'admin_preset_apply_item' => 'block_admin_presets_app_it',
'admin_preset_apply_item_attr' => 'block_admin_presets_app_it_a',
'admin_preset_item' => 'block_admin_presets_it',
'admin_preset_item_attr' => 'block_admin_presets_it_a');

// Just in case it gets to the max number of chars defined in the XSD
try {

// Renaming the tables
foreach ($tablenamechanges as $from => $to) {

$table = new xmldb_table($from);
if ($dbman->table_exists($table)) {
$dbman->rename_table($table, $to);
}
}

// Print error and rollback changes
} catch (Exception $e) {

// Rollback tablename changes
foreach ($tablenamechanges as $to => $from) {

$table = new xmldb_table($from);
if ($dbman->table_exists($table)) {
$dbman->rename_table($table, $to);
}
}

$debuginfo = get_string('errorupgradetablenamesdebug', 'block_admin_presets');
throw new moodle_exception('errorupgradetablenames', 'block_admin_presets', '', null, $debuginfo);
return false;
}

upgrade_block_savepoint(true, 2012031401, 'admin_presets');
}
return true;
}
2 changes: 2 additions & 0 deletions lang/en/block_admin_presets.php
Expand Up @@ -17,6 +17,8 @@
$string['deletepreviouslyapplied'] = 'This preset has been previously applied, if you delete it you can not return to the previous state';
$string['deleteexecute'] = 'execution';
$string['deleteshow'] = 'confirm';
$string['errorupgradetablenames'] = 'admin_presets upgrade failed, upgrade Moodle in order to upgrade admin_presets. You can restore the previous blocks/admin_presets code until then';
$string['errorupgradetablenamesdebug'] = 'The table names exceeds the limit of allowed characters, this is solved using the latest Moodle 2.0, Moodle 2.1 and Moodle 2.2 releases';
$string['errordeleting'] = 'Error deleting from DB';
$string['errorinserting'] = 'Error inserting into DB';
$string['errornopreset'] = 'It doesn\'t exists a preset with that name';
Expand Down
10 changes: 5 additions & 5 deletions version.php
@@ -1,8 +1,8 @@
<?php

$module->version = 2012031400;
$module->release = '1.0(Build: 2012031400)';
$plugin->version = 2012031401;
$plugin->release = '1.0(Build: 2012031401)';

$module->requires = 2010091500;
$module->cron = 0;
$module->maturity = MATURITY_STABLE;
$plugin->requires = 2010091500;
$plugin->cron = 0;
$plugin->maturity = MATURITY_STABLE;

0 comments on commit cb05dea

Please sign in to comment.