How to Use doctrine:migrations:rollup Command

During the development process, a large number of migrations are created, but rollbacks are not often used or not used at all. Instead of creating another migration, it's better to create one single migration that will include all past database schema changes. That's what the doctrine:migrations:rollup command is for.

The doctrine:migrations:rollup command does the following:

  • Check that there is only one migration in the migrations directory
  • Delete executed migrations from the doctrine_migration_versions table
  • Will add a single migration to the doctrine_migration_versions table, but will not execute it

A single migration will not be executed because it is expected that all previous migrations were executed earlier, and the migration itself contains the up-to-date database schema.

Steps to run doctrine:migrations:rollup command:

DEV environment

  • Dump database data using pg_dumpall -a -U USERNAME > dump.sql command
  • Drop all tables and sequences:
    • drop table TABLENAME cascade
    • drop sequence SEQUENCENAME cascade
  • Remove all migrations from migrations directory
  • Create new migration using symfony console make:migration command
  • Execute new migration using symfony console doctrine:migrations:migrate command
  • Restore database data using psql -U USERNAME -f dump.sql command
  • Run symfony console doctrine:migrations:rollup command

PROD environment

  • Run symfony console doctrine:migrations:rollup command

As a result, there will be only one migration.
After creating a large number of new migrations, the doctrine:migrations:rollup command can be repeated.