The website has been running for a week, and Bruna has a request: show on each dish page how many portions are left today. Pippin has already added the field to the Plat (dish) entity (portions, 10 by default). All that's left is to “just” update the database.
Except this database is no longer empty: it holds the menu, in service. The preview is in the same situation. Open the stew's page: the column does not exist yet.
Your mission
Add the portions column without wrecking the cellar:
- generate the migration (
doctrine:migrations:diff), then run it (doctrine:migrations:migrate); - if it fails, read the message, fix the migration, and run it again;
- the dishes already on the menu must start with 10 portions;
- in the end,
doctrine:schema:validatemust confirm that the database matches the entities exactly; - and it must be possible to go back (
doctrine:migrations:migrate prev) without losing a dish.
“It works on my machine”
A migration that runs on your development database, empty or nearly so, can fail on the production database, which is full. This is the migration trap, and the reason why migrations are read and tested on realistic data.
A required column in two steps
Adding a required column to a table full of rows is often done in two migrations:
- add the column with a value for the existing rows;
- once the data is in place, remove what was only there for the transition.
Each step is simple, easy to review, and can be undone.