[DBAL-361] [GH-213] fixed bug on schema comparator, prevent multiple rename candidates for a single original field Created: 04/Oct/12  Updated: 01/May/13  Resolved: 01/May/13

Status: Resolved
Project: Doctrine DBAL
Component/s: None
Affects Version/s: None
Fix Version/s: 2.3.4
Security Level: All

Type: Bug Priority: Major
Reporter: Benjamin Eberlei Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None


This issue is created automatically through a Github pull request on behalf of leedavis81:

Url: https://github.com/doctrine/dbal/pull/213


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

    1. 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.

Comment by Lee Davis [ 18/Apr/13 ]

Is there anything else I need to do to get this to be merged? It's quite a frustrating issue that causes me to run an update twice to ensure changes to the DB have been correctly applied.

This bug also affects the migrations tool.

Pull request has been created https://github.com/doctrine/dbal/pull/213

Comment by Doctrine Bot [ 01/May/13 ]

A related Github Pull-Request [GH-213] was closed:

Generated at Fri Jul 31 13:24:34 UTC 2015 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.