The first migration

Exercise 1 of 6 in chapter 8 · 250 XP · 5 objectives checked by tests

Topics covered: Migrationsdoctrine:migrations:diffdoctrine:migrations:migrateup() and down()

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:

  1. generate the first migration: doctrine:migrations:diff;
  2. read the file that opens in the editor, and describe it in getDescription();
  3. run it: doctrine:migrations:migrate;
  4. reload the menu: app:charger-carte (load the menu), then the preview.

What a migration contains

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.