From 9d2d735415326e57c47cbcd56c5655e3b663c8fc Mon Sep 17 00:00:00 2001 From: jnlar Date: Wed, 21 Dec 2022 14:30:17 +1100 Subject: [PATCH] Fix #123: Fix schema issues in report_customsql_categories --- db/upgrade.php | 95 ++++++++++++++++++++++++++------------------------ version.php | 2 +- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index 2dc9304..c441b15 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -87,50 +87,6 @@ function xmldb_report_customsql_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2013062300, 'report', 'customsql'); } - if ($oldversion < 2013102400) { - - // Define table report_customsql_categories to be created. - $table = new xmldb_table('report_customsql_categories'); - - // Adding fields to table report_customsql_categories. - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null); - - // Adding key to table report_customsql_categories. - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - // Conditionally launch create table for report_customsql_categories. - if (!$dbman->table_exists($table)) { - $dbman->create_table($table); - } - - // Define field categoryid to be added to report_customsql_queries. - $table = new xmldb_table('report_customsql_queries'); - $field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'emailwhat'); - - // Conditionally launch add field categoryid. - if (! $dbman->field_exists($table, $field)) { - $dbman->add_field($table, $field); - } - - // Add key (for the new field just added). - $key = new xmldb_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'report_customsql_categories', array('id')); - $dbman->add_key($table, $key); - - // Create the default 'Miscellaneous' category. - $category = new stdClass(); - $category->name = get_string('defaultcategory', 'report_customsql'); - if (!$DB->record_exists('report_customsql_categories', array('name' => $category->name))) { - $category->id = $DB->insert_record('report_customsql_categories', $category); - } - // Update the existing query category ids, to move them into this category. - $sql = 'UPDATE {report_customsql_queries} SET categoryid =' . $category->id; - $DB->execute($sql); - - // Report savepoint reached. - upgrade_plugin_savepoint(true, 2013102400, 'report', 'customsql'); - } - // Repeat upgrade step that might have got missed on some branches. if ($oldversion < 2014020300) { require_once($CFG->dirroot . '/report/customsql/locallib.php'); @@ -269,5 +225,54 @@ function xmldb_report_customsql_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2021111600, 'report', 'customsql'); } - return true; + if ($oldversion < 2022031801) { + + // Define table report_customsql_categories to be created. + $table = new xmldb_table('report_customsql_categories'); + + // Adding fields to table report_customsql_categories. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null); + + // If the 'name' field already exists, ensure its constraint is set to NOTNULL + if ($dbman->field_exists($table, $table->getField('name'))) { + $dbman->change_field_notnull($table, $table->getField('name')); + } + + // Adding key to table report_customsql_categories. + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + // Conditionally launch create table for report_customsql_categories. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Define field categoryid to be added to report_customsql_queries. + $table = new xmldb_table('report_customsql_queries'); + $field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'emailwhat'); + + // Conditionally launch add field categoryid. + if (! $dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Add key (for the new field just added). + $key = new xmldb_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'report_customsql_categories', array('id')); + $dbman->add_key($table, $key); + + // Create the default 'Miscellaneous' category. + $category = new stdClass(); + $category->name = get_string('defaultcategory', 'report_customsql'); + if (!$DB->record_exists('report_customsql_categories', array('name' => $category->name))) { + $category->id = $DB->insert_record('report_customsql_categories', $category); + // Update the existing query category ids, to move them into this category. + $sql = 'UPDATE {report_customsql_queries} SET categoryid =' . $category->id; + $DB->execute($sql); + } + + // Report savepoint reached. + upgrade_plugin_savepoint(true, 2022031801, 'report', 'customsql'); + } + + return true; } diff --git a/version.php b/version.php index fd29261..6e60617 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022031800; +$plugin->version = 2022031801; $plugin->requires = 2020061500; $plugin->component = 'report_customsql'; $plugin->maturity = MATURITY_STABLE;