laravel 8 — schema:dump/migration squashing

Paul
Nov 15, 2020

--

Over time any software project will get larger, and with laravel projects this often means that the migrations folder can get very large. In laravel 8 the schema:dump command was introduced to clean this up a bit, and instead of running possibly hundreds of migration files when migrating using migrate:fresh, the schema:dump command can run just 1 schema file containing the schema of all the migrations up until that point.

You will need the mysqldump command available as an environment variable as a prerequisite.

Here is how to do that. First run the schema:dump command:

php artisan schema:dump

You should now see in /database/schema there is a new file called mysql-schema.sql. This contains the schema for all the migrations up until this point.

You will also see that your migration files still exist. You could run the command again to delete all these unnecessary migration files now:

php artisan schema:dump --prune

Now you have a new starting point for your migrations, any new migrations will first run the mysql-schema.sql file, and then run all the migrations that came after.

I hope this was useful to someone.

--

--

Paul

I am a software developer and I like to write about interesting things I come across in my day to day.