Migrations advanced

Contents
Generate diffs
Read before running
Migrate down
Prod discipline

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

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: