Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 3 comments

2 participants

@doctrinebot

Jira issue originally created by user @beberlei:

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

Url: #213

Message:

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.

@doctrinebot

Comment created by leedavis81:

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 #213

@doctrinebot

Comment created by @doctrinebot:

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

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added the Bug label
@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.3.4 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.