Generating Schema Diffs
Once you have Generated your first Migration you will probably do more changes to your database. To simplify the generation of new migration you can do Schema Diffs. To this, you need to follow the steps:
- Generate your first Migration (if haven't generated yet)
- Generate a schema file
- Do changes to your database
- Generate a new migration file
1. Generate your first Migration
As you know, this is the very first step when you install the migrations plugin, for this section read Generating your first Migration
2. Generate a schema file
There is impossible to generate schema diffs across migrations file. So you need to generate a CakeSchema file doing:
cake schema generate
Doing it you will save the current database state.
3. Do changes to your database
Now you can do whatever changes you want to do.
4. Generate a new migration file
Since you Generate a schema file and did some changes to your database, you will be able to compare the schema file to your new database state. By doing it, you will generate a migration file that only contains the difference between the two states.
To generate a new migration file doing this comparison you do:
cake migration generate
This will give us the following prompt:
Cake Migration Shell
---------------------------------------------------------------
Please enter the descriptive name of the migration to generate:
>
Enter in 002 some changes to the database as the name for the migration and hit return. Since there is a schema.php file present in your environment you will then get the following prompt:
Do you wanna compare the schema.php file to the database? (y/n)
[y] >
Answer y to this question as we want to compare the previous database state to the new database state. A new file on app/config/migrations/, named 002_some_changes_to_the_database.php, will be created containing only the difference between the two states.
Then the following output will be give as result:
Comparing schema.php to the database...
Generating Migration...
Mapping Migrations...
Done.