Migrations advanced

Generate diffs

After entity edits, generate a migration from the diff. The tool compares models with the base and writes the SQL for you.

One command prints the new file name. Nothing runs yet, so this step is always safe.

php bin/console doctrine:migrations:diff

New migration created

Read before running

Open the file before it runs. Check table names, dropped columns, and data loss risks line by line.

The method below adds one create table statement. Small files like this are easy to trust.

final class Version20240101 extends AbstractMigration { public function up(Schema $schema): void { $this->addSql("CREATE TABLE product (id INT NOT NULL)"); } }

Migrate down

Each migration can also go back down. Move to the previous version to undo the last change on a dev copy.

Test the way down as often as the way up. A deploy plan needs both directions.

php bin/console doctrine:migrations:migrate prev

Migrated to previous version

Prod discipline

Production runs need extra calm: back up first, run during a quiet window, and watch the logs after.

Never edit an old migration that already ran in prod. Write a new one so every base moves forward the same way.

Next: Security advanced

Article author: Arthur Isaev