Skip to content

Commit

Permalink
MDL-73095 core_ddl: Fix binary type field check
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Nov 18, 2021
1 parent eab63d2 commit cb4e37c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
96 changes: 52 additions & 44 deletions lib/ddl/database_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,52 +1039,60 @@ public function check_database_schema(xmldb_structure $schema, array $options =
$errors[$tablename][] = "column '$fieldname' should allow NULL ($dbfield->meta_type)";
}
}
if ($dbtype == XMLDB_TYPE_TEXT) {
// No length check necessary - there is one size only now.

} else if ($dbtype == XMLDB_TYPE_NUMBER) {
if ($field->getType() == XMLDB_TYPE_FLOAT) {
switch ($dbtype) {
case XMLDB_TYPE_TEXT:
case XMLDB_TYPE_BINARY:
// No length check necessary - there is one size only now.
break;

case XMLDB_TYPE_NUMBER:
$lengthmismatch = $field->getLength() != $dbfield->max_length;
$decimalmismatch = $field->getDecimals() != $dbfield->scale;
// Do not use floats in any new code, they are deprecated in XMLDB editor!
if ($field->getType() != XMLDB_TYPE_FLOAT && ($lengthmismatch || $decimalmismatch)) {
$size = "({$field->getLength()},{$field->getDecimals()})";
$dbsize = "($dbfield->max_length,$dbfield->scale)";
$errors[$tablename][] = "column '$fieldname' size is $dbsize,".
" expected $size ($dbfield->meta_type)";
}
break;

case XMLDB_TYPE_CHAR:
// This is not critical, but they should ideally match.
if ($field->getLength() != $dbfield->max_length) {
$errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length,".
" expected {$field->getLength()} ($dbfield->meta_type)";
}
break;

case XMLDB_TYPE_INTEGER:
// Integers may be bigger in some DBs.
$length = $field->getLength();
if ($length > 18) {
// Integers are not supposed to be bigger than 18.
$length = 18;
}
if ($length > $dbfield->max_length) {
$errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length,".
" expected at least {$field->getLength()} ($dbfield->meta_type)";
}
break;

case XMLDB_TYPE_TIMESTAMP:
$errors[$tablename][] = "column '$fieldname' is a timestamp,".
" this type is not supported ($dbfield->meta_type)";
continue 2;

case XMLDB_TYPE_DATETIME:
$errors[$tablename][] = "column '$fieldname' is a datetime,".
"this type is not supported ($dbfield->meta_type)";
continue 2;

default:
// Report all other unsupported types as problems.
$errors[$tablename][] = "column '$fieldname' has unknown type ($dbfield->meta_type)";
continue 2;

} else if ($field->getLength() != $dbfield->max_length or $field->getDecimals() != $dbfield->scale) {
$size = "({$field->getLength()},{$field->getDecimals()})";
$dbsize = "($dbfield->max_length,$dbfield->scale)";
$errors[$tablename][] = "column '$fieldname' size is $dbsize, expected $size ($dbfield->meta_type)";
}

} else if ($dbtype == XMLDB_TYPE_CHAR) {
// This is not critical, but they should ideally match.
if ($field->getLength() != $dbfield->max_length) {
$errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length, expected {$field->getLength()} ($dbfield->meta_type)";
}

} else if ($dbtype == XMLDB_TYPE_INTEGER) {
// Integers may be bigger in some DBs.
$length = $field->getLength();
if ($length > 18) {
// Integers are not supposed to be bigger than 18.
$length = 18;
}
if ($length > $dbfield->max_length) {
$errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length, expected at least {$field->getLength()} ($dbfield->meta_type)";
}

} else if ($dbtype == XMLDB_TYPE_BINARY) {
// Ignore binary types.
continue;

} else if ($dbtype == XMLDB_TYPE_TIMESTAMP) {
$errors[$tablename][] = "column '$fieldname' is a timestamp, this type is not supported ($dbfield->meta_type)";
continue;

} else if ($dbtype == XMLDB_TYPE_DATETIME) {
$errors[$tablename][] = "column '$fieldname' is a datetime, this type is not supported ($dbfield->meta_type)";
continue;

} else {
// Report all other unsupported types as problems.
$errors[$tablename][] = "column '$fieldname' has unknown type ($dbfield->meta_type)";
continue;
}

// Note: The empty string defaults are a bit messy...
Expand Down
1 change: 1 addition & 0 deletions lib/ddl/tests/ddl_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,7 @@ public function test_check_database_schema() {
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('extracolumn', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('binaryfield', XMLDB_TYPE_BINARY, null, null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('extraindex', XMLDB_KEY_UNIQUE, array('extracolumn'));
$table->setComment("This is a test table, you can drop it safely.");
Expand Down

0 comments on commit cb4e37c

Please sign in to comment.