General developer forum

Creating a table through block/db/upgrade. Moodle 3.4+

 
Picture of Dave Emsley
Creating a table through block/db/upgrade. Moodle 3.4+
Particularly helpful Moodlers

Hi there,

I'm upgrading a block I wrote a while back and need to add a new database table.  Looking at  https://docs.moodle.org/dev/Upgrade_API#Upgrade_API_Cheatsheet the code should be:

function xmldb_block_myblockname_upgrade($oldversion) {
    global $DB, $CFG;

    $dbman = $DB->get_manager();
   
    if ($oldversion < 2018061303) {
        $table = new xmldb_table('my_table_name');
        if(!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
    upgrade_block_savepoint(true, 2018061303, 'myblockname');
    }
}


but this is not working and throwing an error

Unknown DDL library error

More information about this error

Debug info: table create sql not generated
Error code: ddlunknownerror

Any clues greatfully received.

Cheers

Dave




 
Average of ratings: -
Davo
Re: Creating a table through block/db/upgrade. Moodle 3.4+
Core developersParticularly helpful MoodlersPlugin developers

The immediate problem that jumps out to me is that you are creating a table, without defining any fields.

If you use Moodle's built-in XMLDB editor, then it should generate the correct content for both the install.xml file and the upgrade.php statements you need.


 
Average of ratings: Useful (1)
Picture of Dave Emsley
Re: Creating a table through block/db/upgrade. Moodle 3.4+
Particularly helpful Moodlers

It does Davo, thanks.

 
Average of ratings: -
Picture of Marcus Green
Re: Creating a table through block/db/upgrade. Moodle 3.4+
Core developersParticularly helpful MoodlersPlugin developersTesters

To add to Davo's comment, the XMLDB editor not only allows you to create a table, but it spits out the PHP that you can copy and paste into an upgrade.php file. I missed that feature when I first started working on it. 


 
Average of ratings: Useful (1)