Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

DB layer 2.0 migration docs

From MoodleDocs

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


This article defines all the changes that need to be performed in Moodle 1.9 code in order to make it run properly under new Moodle 2.0 DB layer. Changes below are grouped into 3 main blocks (xmldb, ddl and dml) to have them organised.

Although the order of changes showed in this page isn't mandatory at all, it can be interesting to follow it in the migration progress to be able to understand and learn a bit more about all them. That way, you'll end knowing not only what to change but how and why those changes are required.

Also, it's recommended to read the whole XMLDB, DDL and DML documentation and APIs before start with the migration. That will allow you to have some initial knowledge about the new architecture.

Each change will be as simple as possible, representing one easy rule to follow to adapt the code. When anything become too much complex to be explained as one simple rule, it will contain one link to the examples page.

For any problem in the migration of code, it's recommended to use the Databases forum at moodle.org. Also if you find any bug in the process, pleas report it in the Moodle Tracker, that way developers will be able to fix it ASAP.

And finally, feel free to complete/fix the list below with the changes you find in the progress of migration, for sure that will help a lot of developers. Thanks!

XMLDB changes

Some comments

  • When changing DB structures it's highly recommended to use the XMLDB Editor (Admin->Misc->XMLDB Editor). It uses to be a safe way to edit install.xml files and to get correct PHP code to be used in upgrade.php scripts.
  • If you have some doubts about the list of changes below, it's highly recommended to take a look to some code in core modules, blocks... whatever you need. They are a good reference.

The changes

  • No changes required in install.xml files at all (that's good news!).
  • All upgrade.php scripts, within the main xxxx_upgrade function must have $DB (uppercase) in the globals declaration (along with others if needed). Example:
   function xmldb_glossary_upgrade($oldversion=0) {
   
       global $CFG, $THEME, $DB;
  • All upgrade.php scripts, must NOT have $db (lowercase) in the globals declaration. Delete it if present.
  • After the global declaration in the points above, this line must be present (we'll need it later):
   $dbman = $DB->get_manager(); /// loads ddl manager and xmldb classes

DDL changes

DML changes

See also