Big news: the tavern's website is going live for real. It will run on a server, with a database that keeps the reservations, the reviews and the staff accounts.
Until now, every exercise prepared the database with doctrine:schema:update --force: Doctrine compared the entities with the database and changed the tables on the spot. Handy in the preview, whose database starts from scratch on every load. Dangerous in production: no record of what changed, no way back, and a renamed column can vanish along with its data.
In production, every schema change is written down in a migration: a PHP file versioned with the code, reviewed, and run once on each server.
Your mission
The preview is broken: this time, nothing creates the tables any more. In the Console tab:
- generate the first migration:
doctrine:migrations:diff; - read the file that opens in the editor, and describe it in
getDescription(); - run it:
doctrine:migrations:migrate; - reload the menu:
app:charger-carte(load the menu), then the preview.
What a migration contains
up(): what to run to move forward (create the tables);down(): what to run to go back (drop them);- a dated name (
Versionfollowed by the date and time): migrations run in order.
Doctrine remembers the migrations already run in the doctrine_migration_versions table: running migrate again replays nothing.
Good habit
The generated file says it itself: “please modify it to your needs”. Doctrine guesses, it does not know: a migration is always read before it is run. You will soon see why.