Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #123: Fix schema issues in report_customsql_categories #1

Merged
merged 1 commit into from
Dec 22, 2022

Conversation

jnlar
Copy link

@jnlar jnlar commented Dec 21, 2022

Fixes issue moodleou#123

-------------------------------------------------------------------------------
report_customsql_categories
 * column 'name' should be NOT NULL (C)
 * column 'name' has default 'Miscellaneous', expected 'NULL' (C)
-------------------------------------------------------------------------------

At some point the name columns constraint has been changed to NULL leading to this error.

@jnlar jnlar requested a review from keevan December 21, 2022 03:39
db/upgrade.php Outdated
$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'))) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure where to put this, this just makes sure that if the column exists already make sure it has the correct constraint defined on #L235.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes the field_exists check is good to have in the new upgrade block.

db/upgrade.php Outdated
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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior, this would be run unconditionally which would cause an error since #L123 would be skipped.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean. Could you elaborate on this?

I can definitely see an issue if this was kept in the new upgrade block.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that sites that already have this plugin installed will have the report_customsql_categories record already, so if (!$DB->record_exists('report_customsql_categories', ... will be skipped, leaving $category->id undefined. This was causing $DB->execute($sql); to throw an error.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. As pointed out by the other comments, I believe only the updates needed should be in the new block, instead of trying to recreate the table and the surrounding logic, etc.

Copy link
Author

@jnlar jnlar Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, my bad there. I've left the original block alone and created a new one with what we need to resolve the failed schema check, see commit: bacdd6a

Copy link

@keevan keevan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be more changes here than expected.

I would expect this patch to address only the issue described in the linked issue (moodleou#123).

  • to ensure the 2 missing columns exist,
  • and to fix up the expected default value of name, which according to the current state of this upgrade block, and the current install.xml file, should be not set (or NULL).

The reason for this, is (probably) at some point, the Miscellaneous category created, should not be a fixed English string, but instead a lang string that could be set in a different language. Thus, it doesn't make sense for it to necessarily exist as English only.

db/upgrade.php Outdated
$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'))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes the field_exists check is good to have in the new upgrade block.

db/upgrade.php Outdated
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean. Could you elaborate on this?

I can definitely see an issue if this was kept in the new upgrade block.

Copy link

@keevan keevan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes seem good, can you confirm if the upgrade + schema checks went okay locally?

Happy to merge this once you've squashed all the commits.

@jnlar jnlar force-pushed the issue123-categories-schema branch from 4c24779 to 882afea Compare December 22, 2022 02:54
@jnlar
Copy link
Author

jnlar commented Dec 22, 2022

Changes seem good, can you confirm if the upgrade + schema checks went okay locally?

Happy to merge this once you've squashed all the commits.

Awesome 👍 commits have been squashed and can confirm upgrade + schema checks went okay locally

-->report_customsql
++ 2022031802: Success (0.15 seconds) ++
++ Success (0.24 seconds) ++
== Setting new default values ==
Command line upgrade from 4.0.5+ (Build: 20221216) (2022041905.08) to 4.0.5+ (Build: 20221216) (2022041905.08) completed successfully.
root@3bd6cb743d42:/var/www/foo# php admin/cli/check_database_schema.php 
Database structure is ok.
root@3bd6cb743d42:/var/www/foo#

@keevan keevan merged commit 7b44f4a into main-catalyst Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants