Skip to content

Commit

Permalink
Fix moodleou#123: Fix schema issues in report_customsql_categories
Browse files Browse the repository at this point in the history
  • Loading branch information
jnlar committed Dec 21, 2022
1 parent 55b1bcd commit 9d2d735
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
95 changes: 50 additions & 45 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9d2d735

Please sign in to comment.