CakeDC/migrations — pull request #148 by joshuapaling (2014-01-27 12:51:36+00:00)
Title: Get migrations from cli working and tested.

So, long story short, based on what I tested manually, none of this stuff was working all the way through.

Here's a rough overview of I've done:

- Fix two existing, failing tests. These fixes aren't related to this new feature - they are also needed in master.

- Refactor a couple of very long methods (I had to so I could read the code to get started!)

- Got generate from cli stuff working, with tests. Done some changing of the way the command line migrations work.

Now I'll elaborate on that last point.

a) In Jose's original implementation, generating a migration from the command line would also compare schema.php to the database, and kind of integrate those changes with the arguments passed via cli, to create a migration based on both, which is potentially confusing. Speaking with @lorenzo on IRC, we agreed that migrations generated from cli should be as simple as possible, and should only be based on the cli arguments, completely ignoring differences between schema.php and the database itself.

So, eg, if a user generates a migration to create 'blogs' and 'blogs' already exists in the database, it will still just let them do it.

This also means that dropping a table (or fields), is an irreversible migration - unless the user themselves manually adds the logic for the 'down' case, which would re-add the table, including all it's fields. Of course, we could look at the database or schema.php and work out the existing values to make it reversible... but I've intentionally left that feature out for now.

Doing it this way (keeping cli generation self-contained and not involving schema.php or the database at all) required some refactoring as all the logic was coupled up with the 'generate from comparison' feature.

b) Jose's original implementation didn't allow length to be parsed in via cli arguments. I haven't changed that. However, his implementation did set default lengths for fields (eg. 255 for string, 11 for integer). I've removed this, because if length is left unspecified, those default values will be used by the database anyway, so no point maintaining code for them. It'd be worth re-implementing if we allow length to be parsed in via cli.

c) If id is passed in as a field, with no index type, it will now automatically set it as a primary key index.

d) I've removed the 'alter_table' case. Since cli arguments are all we go by, and we no longer compare schema.php to the database at all, users will need to be explicit - either create_table or create_field - not alter_table.