Skip to content

Commit

Permalink
Fix moodleou#123: Created new upgrade block
Browse files Browse the repository at this point in the history
Only assess the updates needed instead of moving the original upgrade
block down.
  • Loading branch information
jnlar committed Dec 21, 2022
1 parent ea0818a commit bacdd6a
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,50 @@ 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 @@ -227,47 +271,13 @@ function xmldb_report_customsql_upgrade($oldversion) {

if ($oldversion < 2022031801) {

// Define table report_customsql_categories to be created.
// Changing nullability of field name on table report_customsql_categories to null.
$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);
$field = $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);
if ($dbman->field_exists($table, $field)) {
$dbman->change_field_notnull($table, $field);
}

// Report savepoint reached.
Expand Down

0 comments on commit bacdd6a

Please sign in to comment.