MDL-59323 XMLDB: Check defaults work incorrectly with char fields

This commit is contained in:
sam marshall 2017-07-13 11:17:05 +01:00
parent 94904cc323
commit 12f7d77cc6

View file

@ -66,6 +66,15 @@ class check_defaults extends XMLDBCheckAction {
// Get the default value for the field. // Get the default value for the field.
$xmldbdefault = $xmldbfield->getDefault(); $xmldbdefault = $xmldbfield->getDefault();
// Char fields with not null currently have default '' when actually installed.
if ($xmldbdefault === null && $xmldbfield->getType() === XMLDB_TYPE_CHAR &&
$xmldbfield->getNotNull()) {
$xmldbdefault = '';
}
if ($xmldbdefault !== null) {
$xmldbdefault = (string)$xmldbdefault;
}
// If the metadata for that column doesn't exist or 'id' field found, skip. // If the metadata for that column doesn't exist or 'id' field found, skip.
if (!isset($metacolumns[$xmldbfield->getName()]) or $xmldbfield->getName() == 'id') { if (!isset($metacolumns[$xmldbfield->getName()]) or $xmldbfield->getName() == 'id') {
continue; continue;
@ -81,13 +90,14 @@ class check_defaults extends XMLDBCheckAction {
if ($metacolumn->has_default == 1) { if ($metacolumn->has_default == 1) {
$physicaldefault = $metacolumn->default_value; $physicaldefault = $metacolumn->default_value;
} else { } else {
$physicaldefault = ''; $physicaldefault = null;
} }
// There *is* a default and it's wrong. // There *is* a default and it's wrong.
if ($physicaldefault != $xmldbdefault) { if ($physicaldefault !== $xmldbdefault) {
$info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual']. $xmldbtext = self::display_default($xmldbdefault);
" '$physicaldefault')"; $physicaltext = self::display_default($physicaldefault);
$info = "({$this->str['expected']} {$xmldbtext}, {$this->str['actual']} {$physicaltext})";
$o .= '<font color="red">' . $this->str['wrong'] . " $info</font>"; $o .= '<font color="red">' . $this->str['wrong'] . " $info</font>";
// Add the wrong field to the list. // Add the wrong field to the list.
$obj = new stdClass(); $obj = new stdClass();
@ -107,6 +117,20 @@ class check_defaults extends XMLDBCheckAction {
return array($o, $wrongfields); return array($o, $wrongfields);
} }
/**
* Converts a default value suitable for display.
*
* @param string|null $defaultvalue Default value
* @return string Displayed version
*/
protected static function display_default($defaultvalue) {
if ($defaultvalue === null) {
return '-';
} else {
return "'" . s($defaultvalue) . "'";
}
}
protected function display_results(array $wrongfields) { protected function display_results(array $wrongfields) {
global $DB; global $DB;
$dbman = $DB->get_manager(); $dbman = $DB->get_manager();
@ -126,8 +150,8 @@ class check_defaults extends XMLDBCheckAction {
foreach ($wrongfields as $obj) { foreach ($wrongfields as $obj) {
$xmldbtable = $obj->table; $xmldbtable = $obj->table;
$xmldbfield = $obj->field; $xmldbfield = $obj->field;
$physicaldefault = $obj->physicaldefault; $physicaltext = self::display_default($obj->physicaldefault);
$xmldbdefault = $obj->xmldbdefault; $xmldbtext = self::display_default($obj->xmldbdefault);
// Get the alter table command. // Get the alter table command.
$sqlarr = $dbman->generator->getAlterFieldSQL($xmldbtable, $xmldbfield); $sqlarr = $dbman->generator->getAlterFieldSQL($xmldbtable, $xmldbfield);