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

Published on by

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

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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Visit Paragraph
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Rule::array() and whereJsonOverlaps() for MySQL in Laravel 11.7 image

Rule::array() and whereJsonOverlaps() for MySQL in Laravel 11.7

Read article
Optimize Your Eloquent Queries with AI image

Optimize Your Eloquent Queries with AI

Read article
Build Your SaaS In Days With SaaSykit image

Build Your SaaS In Days With SaaSykit

Read article
Moonshine is an Open-source Admin Panel for Laravel image

Moonshine is an Open-source Admin Panel for Laravel

Read article
NativePHP Windows Builds are Here image

NativePHP Windows Builds are Here

Read article
Replicate Laravel PHP Client image

Replicate Laravel PHP Client

Read article