Commit f884b305 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-237' into 2.2

parents 54f82879 04b425e9
......@@ -519,6 +519,64 @@ class MySqlPlatform extends AbstractPlatform
return array_merge($sql, $tableSql, $columnSql);
}
/**
* Fix for DROP/CREATE index after foreign key change from OneToOne to ManyToOne
*
* @param TableDiff $diff
* @return array
*/
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$sql = array();
$table = $diff->name;
foreach ($diff->removedIndexes AS $remKey => $remIndex) {
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) {
$columns = $addIndex->getColumns();
$type = '';
if ($addIndex->isUnique()) {
$type = 'UNIQUE ';
}
$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
$query .= 'ADD ' . $type . 'INDEX ' . $addIndex->getName();
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
$sql[] = $query;
unset($diff->removedIndexes[$remKey]);
unset($diff->addedIndexes[$addKey]);
break;
}
}
}
$sql = array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff));
return $sql;
}
/**
* @override
*/
protected function getCreateIndexSQLFlags(Index $index)
{
$type = '';
if ($index->isUnique()) {
$type .= 'UNIQUE ';
} else if ($index->hasFlag('fulltext')) {
$type .= 'FULLTEXT ';
}
return $type;
}
/**
* Obtain DBMS specific SQL code portion needed to declare an integer type
* field to be used in statements like CREATE TABLE.
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -29,7 +27,6 @@ namespace Doctrine\DBAL\Schema;
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class TableDiff
......
......@@ -5,9 +5,9 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Schema;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Index;
class MySqlPlatformTest extends AbstractPlatformTestCase
{
......@@ -209,4 +209,21 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
{
return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) ENGINE = InnoDB");
}
/**
* @group DBAL-237
*/
public function testChangeIndexWithForeignKeys()
{
$index = new Index("idx", array("col"), false);
$unique = new Index("uniq", array("col"), true);
$diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
$sql = $this->_platform->getAlterTableSQL($diff);
$this->assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);
$diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
$sql = $this->_platform->getAlterTableSQL($diff);
$this->assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $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