This is a guide for NWiki developers that explains step by step what they have to do if they want change NWiki database schema.
1. Changing installation file
mod/wiki/db/install.xml defines NWiki database schema. This file is used during NWiki installation. It’s necessary to modify this file according to the new development.
Adding a new field is an easy job, you only need to write a line. Be aware of the fact that every field defines which is the previous and the next field, so don’t copy an xml item only modify the name.
Adding a new table or a lot of fields more hateful. I recommend to use XMLDB editor that provides Moodle. You can access to this tool from admin block.
2. Changing NWiki version
mod/wiki/version.php contains module version. You will have to change $module->version to a new one. Version numbers are dates, so version format is YYYYMMDDVV, where YYYY is the current year, MM is the current month, DD is the current day and VV is the version number.
If I would need to change version today I would modify:
$module->version = 2008012401
Creating several versions a long the same day increments VV. Next version would be:
$module->version = 2008012402
3. Changing upgrade script
mod/wiki/db/upgrade.php contains NWiki upgrade script. This file defines what actions must be done into database to upgrade it. This process is used when a previous version of NWiki or Moodle old wiki is installed in your system.
The objective of xmldb_wiki_upgrade function is to modify database to set it as a new installation. In other words, executing upgrade script must create the same schema defined in install.xml.
You must add conditional blocks at the end of the file such the one below these lines to alter NWiki tables:
if ($result && $oldversion < 2008011401) {
$table = new XMLDBTable('wiki');
$field = new XMLDBField('notetype');
$result = $result && drop_field($table, $field);
$field = new XMLDBField('evaluation');
$result = $result && drop_field($table, $field);
$table = new XMLDBTable('wiki_pages');
$field = new XMLDBField('evaluation');
$result = $result && drop_field($table, $field);
}
$oldversion is the current NWiki version installed in your Moodle. You must compare it with a version number. If the condition matches, modifications will be done. To alter database schema you must use XMLDB API defined in lib/ddllib.php.
4. Course backup
If the modifications in database have repercussions in course backup, mod/wiki/backuplib.php must be adapted to create backups with the correct information.
Is possible to forget about this step if, for example, you modify wiki_locks table, creating backups of this table is useless. If you add or remove field from, for example wiki_pages, backup script must be corrected.
4. Course restore
If you modified backuplib.php, you will have to modify restorelib.php too. This file contains the script that restores wiki instances from a backup.
5. Nwiki xml import/export
NWiki also provides xml import & export scripts. They only backup content into xml files, so if you have modified wiki, wiki_pages or wiki_synonyms tables you must adapt this process to your changes.
Thats all folks! As you can see, changing database schema is not trivial, it implies a lot of work, programing and testing.

Recent Comments