Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-361

[GH-213] fixed bug on schema comparator, prevent multiple rename candidates for a single original field


    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.3.4
    • Component/s: None
    • Security Level: All
    • Labels:


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



      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.



          • Assignee:
            Benjamin Eberlei
            Benjamin Eberlei
          • Votes:
            0 Vote for this issue
            3 Start watching this issue


            • Created: