MINOR FIX: ID column protected against index and column alterations

This commit is contained in:
Geoff Munn 2009-07-28 21:15:27 +00:00
parent 82dcbb89d2
commit 396384e709

View File

@ -96,13 +96,13 @@ class MSSQLDatabase extends Database {
public function __destruct() { public function __destruct() {
if(is_resource($this->dbConn)) { if(is_resource($this->dbConn)) {
if($this->mssql) { if($this->mssql) {
mssql_close($this->dbConn); mssql_close($this->dbConn);
} else { } else {
sqlsrv_close($this->dbConn); sqlsrv_close($this->dbConn);
}
} }
} }
}
/** /**
* Throw a database error * Throw a database error
@ -461,8 +461,11 @@ class MSSQLDatabase extends Database {
//drop the index if it exists: //drop the index if it exists:
$alterCol=''; $alterCol='';
if(isset($indexList[$colName])){ if(isset($indexList[$colName]) && $colName!='ID'){
$alterCol = "\nDROP INDEX \"$tableName\".ix_{$tableName}_{$colName};"; //$alterCol = "\nDROP INDEX \"$tableName\".ix_{$tableName}_{$colName};";
//The indexname value should hold the name of the index, so we don't have to construct it outselves.
//This also means we can use internal indexes if they happen to appear.
$alterCol = "\nDROP INDEX \"$tableName\"." . $indexList[$colName]['indexname'] . ';';
} }
$prefix="ALTER TABLE \"" . $tableName . "\" "; $prefix="ALTER TABLE \"" . $tableName . "\" ";
@ -473,24 +476,27 @@ class MSSQLDatabase extends Database {
} }
if(isset($matches[1])) { if(isset($matches[1])) {
$alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1]"; //We will prevent any changes being made to the ID column. Primary key indexes will have a fit if we do anything here.
if($colName!='ID'){
$alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1]";
// SET null / not null // SET null / not null
if(!empty($matches[2])) $alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1] $matches[2]"; if(!empty($matches[2])) $alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1] $matches[2]";
// Add a default back // Add a default back
if(!empty($matches[3])) $alterCol .= ";\n$prefix ADD $matches[3] FOR \"$colName\""; if(!empty($matches[3])) $alterCol .= ";\n$prefix ADD $matches[3] FOR \"$colName\"";
// SET check constraint (The constraint HAS to be dropped) // SET check constraint (The constraint HAS to be dropped)
if(!empty($matches[4])) { if(!empty($matches[4])) {
$constraint=$this->ColumnConstraints($tableName, $colName); $constraint=$this->ColumnConstraints($tableName, $colName);
if($constraint) if($constraint)
$alterCol .= ";\n$prefix DROP CONSTRAINT {$constraint['CONSTRAINT_NAME']}"; $alterCol .= ";\n$prefix DROP CONSTRAINT {$constraint['CONSTRAINT_NAME']}";
//NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables. //NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables.
$alterCol .= ";\n$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]"; $alterCol .= ";\n$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]";
}
} }
} }
return isset($alterCol) ? $alterCol : ''; return isset($alterCol) ? $alterCol : '';
@ -571,7 +577,7 @@ class MSSQLDatabase extends Database {
case 'float': case 'float':
case 'bit': case 'bit':
if($field['data_type'] != 'bigint' && $sizeSuffix = $field['numeric_precision']) { if($field['data_type'] != 'bigint' && $sizeSuffix = $field['numeric_precision']) {
$field['data_type'] .= "($sizeSuffix)"; $field['data_type'] .= "($sizeSuffix)";
} }
if($field['is_nullable'] == 'YES') { if($field['is_nullable'] == 'YES') {
@ -726,7 +732,7 @@ class MSSQLDatabase extends Database {
$indexType = "index"; $indexType = "index";
} }
$this->query("DROP INDEX $indexName ON $tableName;"); $this->query("DROP INDEX $indexName ON $tableName;");
$this->query("ALTER TABLE \"$tableName\" ADD $indexType \"$indexName\" $indexFields"); $this->query("ALTER TABLE \"$tableName\" ADD $indexType \"$indexName\" $indexFields");
} }