Wednesday, August 14, 2013

How to fix the book module upgrade error

When upgrading from Moodle 2.2 to 2.4 sometimes it errors out with
Debug info: SELECT id,course FROM
[array (
Error code: invalidrecord
Stack trace:
line 1335 of /lib/dml/moodle_database.php: dml_missing_record_exception thrown
line 1311 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
line 6644 of /lib/accesslib.php: call to moodle_database->get_record()
line 117 of /mod/book/db/upgradelib.php: call to context_module::instance()
line 169 of /mod/book/db/upgrade.php: call to mod_book_migrate_all_areas()
line 627 of /lib/upgradelib.php: call to xmldb_book_upgrade()
line 358 of /lib/upgradelib.php: call to upgrade_plugins_modules()
line 1524 of /lib/upgradelib.php: call to upgrade_plugins()
line 329 of /admin/index.php: call to upgrade_noncore()

To bypass this orphaned error issue, you need to edit the moodle/mod/book/db/upgradelib.php and insert on line 117

$cm = get_coursemodule_from_instance('book', $book->id);
if (empty($cm) || empty($cm->id))
{ continue; }

$context = context_module::instance($cm->id);

This would not fix the orphans, but the upgrade would at least continue properly