Commit a560519c authored by Steve Müller's avatar Steve Müller

move pre alter table alter index SQL to private method

parent 206047af
......@@ -646,30 +646,48 @@ class MySqlPlatform extends AbstractPlatform
}
}
// handle columns that are removed from changed indexes
foreach ($diff->changedIndexes as $chgKey => $chgIndex) {
// Dropping primary keys requires to unset autoincrement attribute on the particular column first
if ($chgIndex->isPrimary() && $diff->fromTable instanceof Table) {
$sql = array_merge(
$sql,
$this->getPreAlterTableAlterIndexForeignKeySQL($diff),
parent::getPreAlterTableIndexForeignKeySQL($diff)
);
return $sql;
}
// when chgIndex->isPrimary we can be sure fromTable->hasPrimaryKey is true
/**
* @param TableDiff $diff The table diff to gather the SQL for.
*
* @return array
*/
private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
{
$sql = array();
$table = $diff->name;
foreach ($diff->changedIndexes as $changedIndex) {
// Changed primary key
if ($changedIndex->isPrimary() && $diff->fromTable instanceof Table) {
foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) {
$column = $diff->fromTable->getColumn($columnName);
if ($column->getAutoincrement() === true && in_array($columnName, $chgIndex->getColumns()) === false) {
// Check if an autoincrement column was dropped from the primary key.
if ($column->getAutoincrement() && ! in_array($columnName, $changedIndex->getColumns())) {
// The autoincrement attribute needs to be removed from the dropped column
// before we can drop and recreate the primary key.
$column->setAutoincrement(false);
$sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
// original autoincrement information might be needed later on by other parts of the table alteration
// Restore the autoincrement attribute as it might be needed later on
// by other parts of the table alteration.
$column->setAutoincrement(true);
}
}
}
}
$sql = array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff));
return $sql;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment