Verify Database Schema
If you have been upgrading your Moodle site over several versions, it is possible (likely even) that some differences may have crept in between the database table definitions (the "schema") in your database and the version you would get creating a new empty site. This happens because of small errors or oversights in the upgrade scripts. Most of these differences are not harmful, but some may cause strange or unexpected errors. For example, if a default value has been added to a field and this was not reflected in an upgrade script code that assumes the presence of the default may fail to work as expected.
The solution is after doing an upgrade (or, if your problem is that the upgrade fails, before) to compare the database schema of the "production" site to that of a newly created site (where no upgrades have been performed) using exactly the same code base. There are a number of ways of doing this, but this article outlines a simple way using the Unix command line.
- Complete the upgrade
- Generate the database schema from your recently upgraded site using the following command:
mysqldump -d -u root -p myproductiondb >production.schema
- Copy the code of your production database to a new (web accessible) location (this is important, it *must* be the same version)
- Create a new, empty database and moodledata area for the new site as per the Installation instructions
- Edit the config.php file to point at the new database and locations, or delete it and use the install script
- Run the install script to generate the (if applicable) config.php file and the database (admin and site values are not important)
- Generate the database schema from your new site using the following command:
mysqldump -d -u root -p mycleandb >clean.schema
- Run the following command to detect the differences
diff -u production.schema clean.schema >db.diff
You can now look at db.diff with your favorite editor to see the differences.
NOTE: The above obviously applies to MySQL databases. Other databases are likely to have a similar function to dump only the schema. For example PostgreSQL has the pg_dump command. The remainder of the discussion still applies.
Interpreting the diff file
Firstly here's a (real) example:
-- -- Table structure for table `mdl_backup_config` @@ -129,11 +93,11 @@ DROP TABLE IF EXISTS `mdl_backup_config`; CREATE TABLE `mdl_backup_config` ( - `id` int(10) unsigned NOT NULL auto_increment, + `id` bigint(10) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL default , `value` varchar(255) NOT NULL default , PRIMARY KEY (`id`), - UNIQUE KEY `name` (`name`) + UNIQUE KEY `mdl_backconf_nam_uix` (`name`) ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='To store backup configuration variables';
The file may have lots of blocks of changes. The - lines show lines to be taken out and the + lines the ones to replace them (from the production to the clean). So here, the int has been replaced with a bigint and the key name changed. Both of these, technically, should have been changed at some point by upgrade scripts but have been missed. In this case they are unlikely to affect the operation of Moodle.
Too many changes to track manually?
You can also use database management programs to synchronize old and new Moodle databases.
For example MySQL Workbrench for MySQL databases.
This is very useful, if You have a lot of changes.
If you can work a Unix/Linux command line the Python script Schema Sync provides a quick and simple way to generate all the commands to update your database.
Should I worry?
Changes in field types (as long as they are compatible) and key name changes seem to be the common issues but should not cause problems. Things to look out for would be along the lines of missing fields and changes to default values.
If you are about to upgrade to Moodle 2.0 you should be more concerned. In order for the upgrade to proceed smoothly it is a very good idea to correct these problems.
- The missing indexes report in SiteAdmin->Misc->XMLDB editor->Check Indexes
- Moodle Databases Forum