MDL-19057 detect missing unique primary key on id columns

This commit is contained in:
Petr Skoda 2010-09-14 16:13:06 +00:00
parent fd31089c76
commit 41a50aa254
2 changed files with 18 additions and 0 deletions

View File

@ -186,6 +186,7 @@ $string['ddldependencyerror'] = '{$a->targettype} "{$a->targetname}" cannot be m
$string['ddlexecuteerror'] = 'DDL sql execution error';
$string['ddlfieldalreadyexists'] = 'Field "{$a}" already exists';
$string['ddlfieldnotexist'] = 'Field "{$a->fieldname}" does not exist in table "{$a->tablename}"';
$string['ddsequenceerror'] = 'Incorrect table "{$a}" definition; there can be only one auto column and it must be defined as a key.';
$string['ddltablealreadyexists'] = 'Table "{$a}" already exists';
$string['ddltablenotexist'] = 'Table "{$a}" does not exist';
$string['ddlunknownerror'] = 'Unknown DDL library error';

View File

@ -233,8 +233,13 @@ abstract class sql_generator {
return $results;
}
$sequencefield = null;
/// Add the fields, separated by commas
foreach ($xmldb_fields as $xmldb_field) {
if ($xmldb_field->getSequence()) {
$sequencefield = $xmldb_field->getName();
}
$table .= "\n " . $this->getFieldSQL($xmldb_field);
$table .= ',';
}
@ -252,8 +257,20 @@ abstract class sql_generator {
$table .= "\nCONSTRAINT " . $keytext . ',';
}
}
/// make sure sequence field is unique
if ($sequencefield and $xmldb_key->getType() == XMLDB_KEY_PRIMARY) {
$field = reset($xmldb_key->getFields());
if ($sequencefield === $field) {
$sequencefield = null;
}
}
}
}
/// throw error if sequence field does not have unique key defined
if ($sequencefield) {
throw new ddl_exception('ddsequenceerror', $xmldb_table->getName());
}
/// Table footer, trim the latest comma
$table = trim($table,',');
$table .= "\n)";