Clean Up Migrations and Speed up Tests with the Schema Dump Command

Clean Up Migrations and Speed up Tests with the Schema Dump Command

A new schema:dump command will be released with Laravel 8 later this year. This command is useful for existing projects because it removes old migrations you no longer need and speeds up the testing and CI process. Taylor Otwell explains this feature in his pull request:

This PR adds support for a new php artisan schema:dump command, which uses mysqldump or pgdump to dump the current state of your schema to a database/schema/{connection}-schema.mysql file.

When this file exists, and php artisan migrate, or php artisan migrate:fresh is run AND no migrations have run against the database yet (migrations table is empty), this schema file will be loaded into the database first and then any outstanding migrations will be run. This means that effectively this schema file would typically only ever be used during local development or during CI testing. In production, you would typically already have migrations that have run in the past so this schema file would never be triggered.

After running schema:dump runs and the schema dump file exists in the project, you can remove old migrations that have made it to production.

This feature solves two problems:

  1. It cleans up old migrations in the schema folder, which can get rather large on old projects.
  2. Tests run much faster because of the schema file, and Laravel doesn’t need to execute all the migrations during testing.

The command would look like this in your project:

php artisan schema:dump

# Automatically clean up old files
php artisan schema:dump --prune

# Specify the connection name
php artisan schema:dump --database=pgsql

This feature has been merged into master which means it will be available in Laravel 8. To learn more about this feature, the best place to look is the 8.x Schema Dump pull request.

Filed in: News

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer
Remote
FPV Exchange
Senior Full-Stack Developer
Remote, USA Only
Givebutter
Senior PHP Developer
Cary, NC
INE
Backend PHP Developer
Hamburg
ABOUT YOU GmbH
Engineering Director
Vancouver or remote (EST/PST preferred)
Engine Digital