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

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

by Dave Emsley -
Number of replies: 3

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: -
In reply to Dave Emsley

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

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin 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)
In reply to Davo Smith

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

by Dave Emsley -

It does Davo, thanks.

In reply to Dave Emsley

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

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

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)