Doctrine migrations in Symfony: generate, review and run
Doctrine migrations, in Symfony, write each schema change into a versioned PHP file, reviewed, then run exactly once per database.
final class Version20260419090000 extends AbstractMigration { public function getDescription(): string { return 'Registrations: the date of the day-before reminder'; } public function up(Schema $schema): void { $this->addSql('ALTER TABLE inscription ADD COLUMN rappel_envoye_le DATETIME DEFAULT NULL'); } public function down(Schema $schema): void { // The way back: rebuild the table without the column, keeping its rows. } }
doctrine:schema:update --force changes the tables on the spot, with no trace and no way back. doctrine:migrations:diff instead compares the entities with the schema and generates a class: up() to move forward, down() to go back. doctrine:migrations:migrate runs the missing ones, in the order of their dated names, and records each one in doctrine_migration_versions; migrate prev undoes the last one. The trap: Doctrine guesses, it doesn't know, and a migration is reviewed before it is run. A down() that drops a table and then recreates it, a tempting shortcut under SQLite, which rebuilds tables to remove a column, runs without error: the command answers "OK", and the rows are gone.
The exercises have you generate and run the first migration of the tavern's website, fix an allergen migration whose rollback empties the pantry, then write the migrations for the storytelling evenings and the reminder date of their registrations.
4 exercises work on this concept, by writing code in the browser.
In the tracks
- The first migration Symfony for PHP developers · chapter 8: Back to the cellar
- 🐉 Boss: the collapsed storeroom Symfony for PHP developers · chapter 8: Back to the cellar
- The specifications Symfony for PHP developers · chapter 12: The big night
- The night before Symfony for PHP developers · chapter 12: The big night
Related concepts
DebuggingManyToManyN+1 queriesPaginationValidationConsole commandMailer