Database issue related to context table. Need troubleshooting help

Database issue related to context table. Need troubleshooting help

by John Doe -
Number of replies: 4
Hey, hope you're having a great day.

I wanted to inquire about an issue that we faced today on our production site out of the blue.
All of our courses suddenly disappeared (the content, not the entries). I wanted to attach a screenshot but all of a sudden I no longer have access to the entire site (see screenshot attached). I have no idea what the issue could be.

An error that says: Can't find data record in database table context.

Things to note:

- We did not perform any updates/upgrades
- We did not perform any migration
- We did not install/uninstall any plugins.

In fact, we weren't working on the server at all today, when all of a sudden a student reported this issue. Your help is highly appreciated.

Version 4.2.1+ (Build: 20230629)
Average of ratings: -
In reply to John Doe

Re: Database issue related to context table. Need troubleshooting help

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

It looks like you're missing the CONTEXT_SYSTEM record from mdl_context:

SELECT * FROM mdl_context WHERE contextlevel = 10;
+----+--------------+------------+------+-------+--------+
| id | contextlevel | instanceid | path | depth | locked |
+----+--------------+------------+------+-------+--------+
|  1 |           10 |          0 | /1   |     1 |      0 |
+----+--------------+------------+------+-------+--------+

You can possibly reinsert this but you'll likely need to rebuild the context paths because the system record's id will have changed. If you have a nightly backup maybe it would be better to:

  1. Back up mdl_context.
  2. Delete (drop) mdl_context.
  3. Restore mdl_context from the nightly backup.
  4. Add any records that had been added since the nightly backup, for example, modify the INSERT statement from the backup in step 1 to insert records after the highest id.

I'm assuming you're using MySQL/MariaDB and that $CFG->prefix = 'mdl_'.

Also, it's highly unlikely Moodle removed this record. So unless your DBMS corrupted mdl_context you may wish to investigate the cause further.

Average of ratings:Useful (4)
In reply to Leon Stringer

Re: Database issue related to context table. Need troubleshooting help

by John Doe -
Yes, that's exactly the issue. We manually inserted the first record and then things started working again. We had around 60K records in that table, and all the records from id = 1 to 55K magically disappeared. We had not worked on this server for the past few days and the cronjobs were running fine, so it literally just happened like that.

In any case, we restored a snapshot that we had and everything is fine now, but we're just very confused as to why it happened in the first place. I'll post a couple more questions here in case you have any more insights:

1) Do you have any idea whether it is possible to restore a single table in an SQL database? (Assuming we are NOT using a tool like phpMyAdmin).
2) Do you have any idea what may be the cause of this? Is there a chance that things like this happen out of the blue ?
In reply to John Doe

Re: Database issue related to context table. Need troubleshooting help

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

You don't tell us which database software you're using. I'm fairly sure I've had data loss like this when there's no disk space available to MySQL.

You'll need to trawl through the Moodle site logs, web server access logs, and PHP error logs to narrow down the time window when this occurred, hopefully down to the minute or second. Then check the database and server logs for that time, and any server monitoring you may have. Could there be some backup running during this time that used all the disk space? Moodle has optional automated course backups which temporarily increases the disk space, a potential problem if the database server and web server are the same server or file system.

I'd be almost certain that Moodle didn't do this. That doesn't rule out some major bug if the core code has been modified for you site, or in some additional plugin you have installed.

Average of ratings:Useful (1)