diff --git a/lib/mysqlschema.php b/lib/mysqlschema.php index c3d3501c74..435ba4e3a9 100644 --- a/lib/mysqlschema.php +++ b/lib/mysqlschema.php @@ -298,6 +298,16 @@ class MysqlSchema extends Schema 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 diff --git a/lib/schema.php b/lib/schema.php index 2e27955881..aad705a533 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -579,6 +579,10 @@ class Schema $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) { $this->appendAlterAddColumn($phrase, $columnName, $def['fields'][$columnName]); @@ -594,6 +598,10 @@ class Schema $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) { $this->appendAlterAddUnique($phrase, $keyName, $def['unique keys'][$keyName]); } @@ -713,6 +721,19 @@ class Schema $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) { $phrase[] = 'DROP CONSTRAINT ' . $keyName;