Migrations
Make a migration
A migration is a versioned recipe that moves the schema forward or back. After changing an entity, generate one with the maker command:
Generate a migration from your entities
php bin/console make:migration
created: migrations/Version20260101000000.php
One file appears per change set. Generate as often as you change entities: small migrations stay easy to read.
Read it
Open the new file before running it. The up method holds the SQL for applying the change, and down holds the SQL for undoing it:
use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; final class Version20260101000000 extends AbstractMigration { public function up(Schema $schema): void { $this->addSql("CREATE TABLE post (id INT NOT NULL, title VARCHAR(255) NOT NULL)"); } public function down(Schema $schema): void { $this->addSql("DROP TABLE post"); } }
Reading first is a habit worth keeping. SQL you understand is SQL you can trust.
Run it
Apply every pending migration with one command. Doctrine tracks which files already ran, so running it twice changes nothing the second time:
Apply pending migrations
php bin/console doctrine:migrations:migrate
[OK] All migrations were applied
Roll back
Mistakes happen, so the latest migration can go back. The prev keyword rolls the schema to the state before the newest file:
Roll back the last migration
php bin/console doctrine:migrations:migrate prev
[OK] Rolled back to the previous version
Next: Repositories
Article author: Arthur Isaev