Doctrine Projecthttp://www.doctrine-project.org/jira
This file is an XML representation of an issueen-us6.2.3626015-04-2014[DBAL-106] Doctrine\DBAL\Schema\Comparator false positiveshttp://www.doctrine-project.org/jira/browse/DBAL-106
Doctrine DBAL<p>I am using the model class annotations for my Doctrine schema definition and a MySQL database for my target. When testing code that uses Doctrine\DBAL\Schema\Comparator, either by using <tt>$schemaTool-&gt;updateSchema($classes)</tt> which is defined in the core functionality or by running <tt>doctrine migrations:diff</tt> from the Doctrine Migrations project, a set of bugs are revealed. The symptoms of this are that immediately after calling <tt>$schemaTool-&gt;createSchema($classes)</tt> any call that uses the Comparator class to calculate the diffs between the real database and the schema definition produce a large set of false positives. Most of these false positives can be traced back to the logic in <tt>Doctrine\DBAL\Schema\Comparator::diffColumn(Column $column1, Column $column2)</tt>. There are two issues here I can see right away:</p>
<p>One is that if the model annotations rely on defaults rather than explicitly defining certain attributes, this function sees them as different. For example, if I have a column defined like this: <tt>@Column(type="decimal", scale=2, nullable=true)</tt> then the Comparator believes it is different from the real database because precision is 10 in the real data and 0 in my schema. If I change the line to this: <tt>if (($column1-&gt;getPrecision() ?: 10) != ($column2-&gt;getPrecision() ?: 10))</tt> then it works, but I am sure a real solution needs to determine what the defaults actually are. </p>
<p>The second issue is when I use the 'columnDefinition' attribute with a custom type to declare an enum or set, for instance. Since this function does not actually check what the columnDefinition field is set to at all, it has no way to know if I have actually made a change to the list of values, or anything else about that columnDefinition. I have been reading that using the comments field might be another way to do this, but I am not yet clear on what the best practice really is here. I know that <a href="http://www.doctrine-project.org/jira/browse/DBAL-42" class="external-link" rel="nofollow">http://www.doctrine-project.org/jira/browse/DBAL-42</a> touches on this with regard to storing Array type metadata in the comments field. I need more direction on how this should work in custom field types.</p>
<p>In addition, I am getting this strange set of diffs on the index for a ManyToMany association's join table:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre> $this-&gt;addSql("CREATE INDEX IDX_44B8C6D89AF7860 ON firm_firm (firm_id)");
$this-&gt;addSql("DROP INDEX primary ON firm_firm");
$this-&gt;addSql("CREATE UNIQUE INDEX primary ON firm_firm (firm_id, firm_id)");
</pre>
</div></div>
<p>I am not sure why this only happens on this one table and not others, but it seems to be another case of the Comparator getting a false positive for some reason this time in <tt>Comparator::diffIndex(Index $index1, Index $index2)</tt>. The model annotations for this association are:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre> /**
* @var Firm
* @ManyToMany(targetEntity="Firm")
*/
protected $Children = array();
</pre>
</div></div>
<p>I will add more to this as I learn more details about the issues, but hopefully this is enough to start grokking the issues in Comparator. Getting the Comparator class in the core Doctrine\DBAL library working properly for calculating diffs will go a long way toward making well defined migrations a reality for Doctrine2 projects running in production. Hopefully, someone will also be able to address this minor issue in the Doctrine Migrations (<a href="http://www.doctrine-project.org/jira/browse/DMIG-21" class="external-link" rel="nofollow">http://www.doctrine-project.org/jira/browse/DMIG-21</a>) so that it can be deployed without needing a patch.</p>
<p>Thanks to everyone for Doctrine2, and I am looking forward to being among the first to put this new library into production on a real world project in a few months. </p>Doctrine 2.0.3, PHP 5.3.5, MySQL 5.5.9DBAL-106Doctrine\DBAL\Schema\Comparator false positivesBugMajorResolvedFixedBenjamin EberleiLee FeistelTue, 29 Mar 2011 15:21:08 +0000Wed, 6 Apr 2011 17:19:17 +0000Wed, 6 Apr 2011 15:10:59 +00002.0.22.0.4Schema Managers12<p>Can you tell me how the indexes on the join table look like?</p><p>Now that I look at this schema, I think I need to apologize for my oversight because I didn't explain the problem properly. The real issue is that this self-referential ManyToMany association needs to have the id fields explicitly defined or it will not work. Doctrine2 is generating this table by apparently assuming both id fields have the same name, and so the second index that is created gets a randomly generated name assigned to it. There is no way this association is actually working properly at all, so I'll talk to the programmer that wrote this. I think we can fix this part of the problem ourselves, but perhaps some error checking here would be helpful. The first half of the problem (in diffColumn) remains. Thanks again for looking into this.</p>
<p>CREATE TABLE IF NOT EXISTS `firm_firm` (<br/>
`firm_id` int(11) NOT NULL,<br/>
PRIMARY KEY (`firm_id`),<br/>
KEY `IDX_44B8C6D89AF7860` (`firm_id`)<br/>
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</p>
<p>ALTER TABLE `firm_firm`<br/>
ADD CONSTRAINT `firm_firm_ibfk_1` FOREIGN KEY (`firm_id`) REFERENCES `firm` (`id`) ON DELETE CASCADE;</p><p>Fixed the precision issue, please open a new ticket for the many to many.</p><p>Thanks. At least the precision issue was easy, so glad to have that one out of the way. Is there any hope for a way to handle the use of columnDefiniton? I guess the alter statements being generated are just changing it to the same value, so it might not actually be broken in and of itself. Those statements could take a small amount of time to execute what is essentially a noop. Maybe we just have to manually go through and clean out the unwanted alter statements for the time being. It might be workable. </p><p>Updated version</p><p>columnDefinition always comes up in the diff. There is just no way around it. The problem is that there is no generic way to create a "columnDefinition" from the existing databse, so no good value to compare it too. If you can think of a solution for this problem i would be very happy, but we havent come up with one yet.</p>