[DMIG-30] Make :migrations:diff smart and generate Schema object changes, not SQL directly Created: 16/Nov/11  Updated: 28/Jan/14

Status: Open
Project: Doctrine Migrations
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major
Reporter: Benjamin Eberlei Assignee: Benjamin Eberlei
Resolution: Unresolved Votes: 2
Labels: None

Attachments: Text File DMIG-30.patch    


Currently when using :migrations:diff the generated output will use addSql() exclusively. However there has to be a way to generate changes on the schema objects themselves by using the SchemaDiff objects and generating code from them.

Comment by Tyler Sommer [ 03/Feb/12 ]

I think this would be something that would greatly increase the usefulness of migrations.

After looking things over, I think doing something like this could work:

  • Instantiate an instance of Doctrine\ORM\Tools\Comparator, and retrieve a Doctrine\DBAL\Schema\SchemaDiff using $comparator->compare($fromSchema, $toSchema);
  • Iterate over each of the SchemaDiff's properties to retrieve new tables, dropped tables, columns, indexes, etc to generate both up and down code

It seems fairly simple, but I get kind of lost thinking about how it can be tested.

Another option, which may in the end be easier but more dirty, is to create a 'PhpPlatform' that only actually implements the getCreateTableSQL, getDropTableSQL, etc methods of Doctrine\DBAL\Platforms\AbstractPlatform.

Comment by Tyler Sommer [ 17/Feb/12 ]

I've gone and followed what I outlined in my last comment, but there is a small snag.

Comparator and SchemaDiff check for schema changes that are not possible to make using public methods. An example is a dropped index. Both Comparator and SchemaDiff support dropping indexes, but there is no Table#dropIndex to actually make it happen.

However, if you use Reflection to modify the underlying Table#$_indexes array, then the proper SQL will be generated by SchemaDiff. Terrible and dirty, I know. We should do something about it Should I create a ticket?

Anyway, I've attached a patch (based on the current master, 9e81984) of what I've come up with. I think I've covered everything. This will even generate code to drop indexes and foreign keys (using Reflection). I've tried it on a schema of around 90 entities and it works beautifully. Of course, it's definitely not done or ready, but I wanted to see what was thought about what I've done.

Finally, what would be considered an acceptable test for this kind of thing? I suppose if I refactor all of the code generating bits into individual methods, I could pretty easily test for expected output. Am I on the right track?

edit: by the way, use migrations:diff --no-platform to generate the migration

Comment by David Simon [ 28/Jan/14 ]

Note that there's also a PR for this change at https://github.com/doctrine/migrations/pull/79.

I just tried it out myself, rebasing on latest master, and it works great. Please consider merging, developers, this feature is awesome!

Generated at Sat Dec 20 01:35:46 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.