Fix for schema upgrade issue when primary keys change; fixes upgrade direct from 0.8.x

Previously we were failing to update the primary key during ensureTable(), which could lead to failures when updating some tables (eg queue_item where we changed keys, and the addition of an autoincrement column failed because it conflicted with the old key).
Now if the key is different, we remove the old key at the start and add the new key at the end of the ALTER TABLE.

Not tested on PostgreSQL -- someone please check whether the alter table 'DROP CONSTRAINT PRIMARY KEY' bit works or if it needs to pull a special name for the key.
On MySQL, dropping uses alter table's 'DROP PRIMARY KEY' special case.
This commit is contained in:
Brion Vibber 2011-09-08 13:03:22 -07:00
부모 f63b53c495
커밋 8454b1fbad
2개의 변경된 파일31개의 추가작업 그리고 0개의 파일을 삭제

파일 보기

@ -298,6 +298,16 @@ class MysqlSchema extends Schema
return "{$tableName}_{$columnName}_idx"; return "{$tableName}_{$columnName}_idx";
} }
/**
* MySQL doesn't take 'DROP CONSTRAINT', need to treat primary keys as
* if they were indexes here, but can use 'PRIMARY KEY' special name.
*
* @param array $phrase
*/
function appendAlterDropPrimary(array &$phrase)
{
$phrase[] = 'DROP PRIMARY KEY';
}
/** /**
* MySQL doesn't take 'DROP CONSTRAINT', need to treat unique keys as * MySQL doesn't take 'DROP CONSTRAINT', need to treat unique keys as

파일 보기

@ -579,6 +579,10 @@ class Schema
$this->appendAlterDropUnique($phrase, $keyName); $this->appendAlterDropUnique($phrase, $keyName);
} }
if (isset($old['primary key']) && (!isset($def['primary key']) || $def['primary key'] != $old['primary key'])) {
$this->appendAlterDropPrimary($phrase);
}
foreach ($fields['add'] as $columnName) { foreach ($fields['add'] as $columnName) {
$this->appendAlterAddColumn($phrase, $columnName, $this->appendAlterAddColumn($phrase, $columnName,
$def['fields'][$columnName]); $def['fields'][$columnName]);
@ -594,6 +598,10 @@ class Schema
$this->appendAlterDropColumn($phrase, $columnName); $this->appendAlterDropColumn($phrase, $columnName);
} }
if (isset($def['primary key']) && (!isset($old['primary key']) || $old['primary key'] != $def['primary key'])) {
$this->appendAlterAddPrimary($phrase, $def['primary key']);
}
foreach ($uniques['mod'] + $uniques['add'] as $keyName) { foreach ($uniques['mod'] + $uniques['add'] as $keyName) {
$this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]); $this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]);
} }
@ -713,6 +721,19 @@ class Schema
$phrase[] = implode(' ', $sql); $phrase[] = implode(' ', $sql);
} }
function appendAlterAddPrimary(array &$phrase, array $def)
{
$sql = array();
$sql[] = 'ADD';
$this->appendPrimaryKeyDef($sql, $def);
$phrase[] = implode(' ', $sql);
}
function appendAlterDropPrimary(array &$phrase)
{
$phrase[] = 'DROP CONSTRAINT PRIMARY KEY';
}
function appendAlterDropUnique(array &$phrase, $keyName) function appendAlterDropUnique(array &$phrase, $keyName)
{ {
$phrase[] = 'DROP CONSTRAINT ' . $keyName; $phrase[] = 'DROP CONSTRAINT ' . $keyName;