From 01aee79df01edadab5dd4047ecc3500cb5988cc0 Mon Sep 17 00:00:00 2001 From: Leon Stringer Date: Thu, 12 Nov 2020 14:44:11 +0000 Subject: [PATCH] MDL-70181 database: Quote database object names Table names and database name now enclosed in backticks. admin/cli/mysql_collation.php failed if $CFG->prefix was blank with MySQL 8.0 because table 'groups' conflicted with a new reserved word. Note that this quotes both mysql_collation.php and mysql_compressed_rows.php but only the former was mal-functioning. The case of mysql_compressed_rows.php is a little bit special because not all tables are processed, only those having big rows. And the groups table is not one of them. In any case, better add the quotes there for any future case. Also, when testing this patch https://tracker.moodle.org/browse/MDL-71512 was discovered and will need to be fixed to make core 100% compliant with MySQL 8.0 and MariaDB 10.6 and up. --- admin/cli/mysql_collation.php | 16 ++++++++-------- admin/cli/mysql_compressed_rows.php | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/admin/cli/mysql_collation.php b/admin/cli/mysql_collation.php index ba8a49ef7cc..591f81e9a5d 100644 --- a/admin/cli/mysql_collation.php +++ b/admin/cli/mysql_collation.php @@ -119,7 +119,7 @@ if (!empty($options['collation'])) { if ($dbcollation->value !== $collation || $dbcharset->value !== $charset) { // Try to convert the DB. echo "Converting database to '$collation' for $CFG->wwwroot:\n"; - $sql = "ALTER DATABASE $CFG->dbname DEFAULT CHARACTER SET $charset DEFAULT COLLATE = $collation"; + $sql = "ALTER DATABASE `$CFG->dbname` DEFAULT CHARACTER SET $charset DEFAULT COLLATE = $collation"; try { $DB->change_database_structure($sql); } catch (exception $e) { @@ -146,7 +146,7 @@ if (!empty($options['collation'])) { } else { try { - $DB->change_database_structure("ALTER TABLE $table->name CONVERT TO CHARACTER SET $charset COLLATE $collation"); + $DB->change_database_structure("ALTER TABLE `$table->name` CONVERT TO CHARACTER SET $charset COLLATE $collation"); echo "CONVERTED\n"; $converted++; } catch (ddl_exception $e) { @@ -163,7 +163,7 @@ if (!empty($options['collation'])) { } } - $sql = "SHOW FULL COLUMNS FROM $table->name WHERE collation IS NOT NULL"; + $sql = "SHOW FULL COLUMNS FROM `$table->name` WHERE collation IS NOT NULL"; $rs2 = $DB->get_recordset_sql($sql); foreach ($rs2 as $column) { $column = (object)array_change_key_case((array)$column, CASE_LOWER); @@ -181,7 +181,7 @@ if (!empty($options['collation'])) { $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; // primary, unique and inc are not supported for texts - $sql = "ALTER TABLE $table->name + $sql = "ALTER TABLE `$table->name` MODIFY COLUMN $column->field $column->type CHARACTER SET $charset COLLATE $collation $notnull $default"; @@ -192,11 +192,11 @@ if (!empty($options['collation'])) { $default = !is_null($column->default) ? "DEFAULT '$column->default'" : ''; if ($rowformat != '') { - $sql = "ALTER TABLE $table->name $rowformat"; + $sql = "ALTER TABLE `$table->name` $rowformat"; $DB->change_database_structure($sql); } - $sql = "ALTER TABLE $table->name + $sql = "ALTER TABLE `$table->name` MODIFY COLUMN $column->field $column->type CHARACTER SET $charset COLLATE $collation $notnull $default"; @@ -315,8 +315,8 @@ function mysql_set_row_format($tablename, $charset, $collation, $engine) { if ($rs->row_format == 'Compact' || $rs->row_format == 'Redundant') { $rowformat = $DB->get_row_format_sql($engine, $collation); // Try to convert to compressed format and then try updating the collation again. - $DB->change_database_structure("ALTER TABLE $tablename $rowformat"); - $DB->change_database_structure("ALTER TABLE $tablename CONVERT TO CHARACTER SET $charset COLLATE $collation"); + $DB->change_database_structure("ALTER TABLE `$tablename` $rowformat"); + $DB->change_database_structure("ALTER TABLE `$tablename` CONVERT TO CHARACTER SET $charset COLLATE $collation"); } else { // Row format may not be the problem. Can not diagnose problem. Send fail reply. return false; diff --git a/admin/cli/mysql_compressed_rows.php b/admin/cli/mysql_compressed_rows.php index e90060cdd8d..d9d421105cc 100644 --- a/admin/cli/mysql_compressed_rows.php +++ b/admin/cli/mysql_compressed_rows.php @@ -158,7 +158,7 @@ if (!empty($options['info'])) { } foreach ($fixtables as $table) { - $DB->change_database_structure("ALTER TABLE {$prefix}$table ROW_FORMAT=Compressed"); + $DB->change_database_structure("ALTER TABLE `{$prefix}$table` ROW_FORMAT=Compressed"); echo str_pad($prefix . $table, 32, ' ', STR_PAD_RIGHT) . " ... Compressed\n"; } @@ -189,7 +189,7 @@ if (!empty($options['info'])) { echo "SET GLOBAL innodb_file_per_table=1;\n"; echo "SET GLOBAL innodb_file_format=Barracuda;\n"; foreach ($fixtables as $table) { - echo "ALTER TABLE {$prefix}$table ROW_FORMAT=Compressed;\n"; + echo "ALTER TABLE `{$prefix}$table` ROW_FORMAT=Compressed;\n"; } echo "\n"; exit(0);