We currently maintain our schemas in MySQL Workbench which we then forward engineer to the database. However, I recently came across CakePHP’s Schema System.
Now my idea is like so:
- Create/update schema in CakePHP
Bake to create the migrations (bake could generate these migrations right?), based on what is in the database already, and what it should be (with the updated schema)
Bake to run the migrations
My questions are, is this possible, and if so, how do I do this in CakePHP 3.6?
CakePHP has two schema systems. The one (which is core and part of the ORM) is so that the ORM is aware of datatypes for fields in order to correctly marshal them (Making dates into Date objects for example).
The other one, which is used for what you are asking about is the Migrations plugin. The migrations plugin has two “modes” (For lack of a better term).
In the one mode, you write a migration script (Or use Bake to generate it for you), and then apply the migration to your database.
The other mode, you change your database (using MySQL workbench for example) and generate the migration files (also using bake) by comparing the current schema to a snapshot. This gives you a migration script that can then be run on other servers (For example production, other dev’s, etc.)
I prefer to use the first one. All your database changes are done as migration scripts and can easily be generated using Bake. The book goes through it in quite a bit of detail.
So instead of creating the complete schema in one go, I write the changes to the database in an incremental fashion?
This does seem like a lot of work at first but doesn’t sound too bad in the long run as it gives a more fine-grained control.
I’ll have a look at it