Moodle ER Diagram HowTo

Creating an ER Diagram by generating a log from the install process. This was done with an install on a MyISAM database. It did not seem to work
when I tried it with the InnoDB database engine, probably because it created some contradictory relationships.
Note that some of the table relationships in Moodle are not strictly relational, i.e. some tables may have
multiple value fields and there may be missing primary/foreign key relationships. This will result in a diagram where a table has
no link to any other table.  However this process may still result in a diagram that is useful to understand the database design.

There may be a far easier way of doing this from the install files and if you know of one please let me know by emailing
marcusavgreen at gmail dot com.

Find the file database_manager.php in the folder lib/ddl

Find the file database_manager.php

Make a backup so you can easily revert to the standard version afterwards then edit the code for the function execute_sql_arr to something like the following

protected function execute_sql_arr(array $sqlarr) {
    $fh = fopen($myFile, 'a') or die("can't open file");
    foreach ($sqlarr as $sql) {
        $sql=str_replace("CREATE TABLE",";CREATE TABLE",$sql);
        $sql=preg_replace("/ALTER TABLE(.*)COMMENT/"," COMMENT",$sql);
        $sql=preg_replace("/CREATE UNIQUE INDEX(.*)\)/","",$sql);
        $sql=preg_replace("/CREATE INDEX(.*)\)/","",$sql);

        fwrite($fh, $sql);


Find the file


Change Line
var $foreign_keys=false;
to read
var $foreign_keys=true;

Perform an install and you should find the install.sql file in the admin directory.

This was done on a Windows/Xampp machine. The reverse engineering was done with  MySQL Workbench 5.2.29