doctrine/dbal — pull request #213 by leedavis81 (2012-10-04 16:00:44+00:00)
Title: fixed bug on schema comparator, prevent multiple rename candidates for a single original field

Starting with the following Entity
```
/**
* @Table(name="user")
* @Entity
*/
class User
{
/**
* @var integer $id
* @Column(name="id", type="integer", length=4)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var \DateTime $date_alerted_email
* @Column(name="date_alerted", type="datetime", nullable=true)
*/
private $date_alerted;
}
```
Upon making the following alterations (remove date_alerted, and add two additional columns of the same type but different names):
```
/**
* @Table(name="user")
* @Entity
*/
class User
{
/**
* @var integer $id
* @Column(name="id", type="integer", length=4)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var \DateTime $date_alerted_email
* @Column(name="date_alerted_email", type="datetime", nullable=true)
*/
private $date_alerted_email;

/**
* @var \DateTime $date_alerted_js
* @Column(name="date_alerted_js", type="datetime", nullable=true)
*/
private $date_alerted_js;
}
```
The doctrine cli schema tool used to run the update (dump sql) produces the following result:
```
ALTER TABLE user CHANGE date_alerted date_alerted_js DATETIME DEFAULT NULL
```
Expected result:
```
ALTER TABLE message ADD date_alerted_js DATETIME DEFAULT NULL, CHANGE date_alerted date_alerted_email DATETIME DEFAULT NULL
```

## What went wrong

Upon running diffTable($from, $to) in \Doctrine\DBAL\Schema\Comparator.php line 69 both new columns are added to the "addedColumns" array, and the one removed column is correctly present in the removedColumns array.

The first iteration on detectColumnRenamings (line 272) puts the two NEW columns up as rename candidates as expected, however they share the original field name.

When iterating over the rename candidates no further checks are done and these entries are added to the renamedColumns array. The last overwrites the original as they share the same array key and the original gets ignored.

My change checks the renamedColumns array for the existence of the old column name. It only becomes a rename candidate if the original field hasn't already used previously. Otherwise it remains in the addedColumns array.

In the example above these changes will produce 1 change column and 1 add column as expected.