Block Database Upgrade Moodle 3.5.2

Block Database Upgrade Moodle 3.5.2

by Dave Emsley -
Number of replies: 7

I've created a block which is working fine but I need to upgrade the database to add a table.

According to the documentation I can use the XMLDB creator so I did.  The code it generates doesn't work.

        // Define field id to be added to voucher_courses.
        $table = new xmldb_table('voucher_courses');
        if(!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
$field = new xmldb_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
// Conditionally launch add field id.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

This generates the error...

Table "voucher_courses" does not exist

More information about this error

Debug info:
Error code: ddltablenotexist
Stack trace:
  • line 146 of /lib/ddl/database_manager.php: ddl_table_missing_exception thrown
  • line 28 of /blocks/voucher/db/upgrade.php: call to database_manager->field_exists()
  • line 1005 of /lib/upgradelib.php: call to xmldb_block_voucher_upgrade()
  • line 514 of /lib/upgradelib.php: call to upgrade_plugins_blocks()
  • line 1857 of /lib/upgradelib.php: call to upgrade_plugins()
  • line 694 of /admin/index.php: call to upgrade_noncore()

Sorry if this is a bit of a newbie question but I've hit a brick wall with this.

Cheers

Dave

Average of ratings: -
In reply to Dave Emsley

Re: Block Database Upgrade Moodle 3.5.2

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

That code doesn't look like a straight copy and paste of the "add table" code from XMLDB. It would be a good idea to go back and do the code generation again. I'd expect the code to be something like this:


// Define table voucher_courses to be created.
$table = new xmldb_table('voucher_courses');

// Adding fields to table voucher_courses.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);

// Adding keys to table voucher_courses.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for voucher_courses.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}




Average of ratings: Useful (1)
In reply to Dave Emsley

Re: Block Database Upgrade Moodle 3.5.2

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

That is not the right PHP code to generate a new table. You need to generate the right code from XMLDB. On the page that lists all the tables in the xmldb file, select Generate PHP code there. Then you can get the code to generate the table with all its columns.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Block Database Upgrade Moodle 3.5.2

by Dave Emsley -

Hi Tim, thanks but I thought that was what I had done.

Will investigate further.


Cheers

Dave

In reply to Tim Hunt

Re: Block Database Upgrade Moodle 3.5.2

by Dave Emsley -
Hi Tim, It has been a while since I visited this problem but it still exists and the above is the code to create the table generated by the xmldb. So I really have no clue where to go from here.

Cheers
Dave
In reply to Dave Emsley

Re: Block Database Upgrade Moodle 3.5.2

by Dave Emsley -

XMLDB gives me this code -

function xmldb_block_voucher_upgrade($oldversion, $block) {
    global $CFG;
    if ($oldversion < 2019061809) {
        // Define table voucher_discount_codes to be created
        $table = new xmldb_table('voucher_discount_codes');
        // Adding fields to table voucher_discount_codes.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('discount_voucher_code', XMLDB_TYPE_CHAR, '50', null, null, null, null);
 Other fields removed 

        // Adding keys to table voucher_discount_codes.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for voucher_discount_codes.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Voucher savepoint reached.
        upgrade_block_savepoint(true, 2019061809, 'voucher');
    }
    return true;
}

Yet this doesn't seem to work.

Any ideas anyone?

Cheers

Dave

In reply to Dave Emsley

Re: Block Database Upgrade Moodle 3.5.2

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Doesn't look like $dbman is defined. You need something like this:

global $CFG, $DB;
$dbman = $DB->get_manager();
Average of ratings: Useful (1)
In reply to Michael Aherne

Re: Block Database Upgrade Moodle 3.5.2

by Dave Emsley -
Hi Michael, Thanks for that but that's not solved it either I'm afraid.

Cheers

Dave