You are here
Home > Tech >

Laravel’s new migrate:fresh command

One of the features in Laravel, because of which I fell in love with the framework initially, is migrations. I don’t remember how most other frameworks handle migrations, but Laravel’s migration engine is super awesome. If you work with Laravel regularly, you’d have no doubt used a few migration commands, maybe for creating a migration file, running migrations, rolling back migrations, etc.

During development, it’s not uncommon to run into situations where you break your DB schema and wanting to start from scratch, you know, a blank DB. Laravel makes this super easy with the migrate:refresh command. When you run this command against your database, the tool runs the down() function in all your migration files, thereby going back in time to an empty DB, and then running the up() function in all the migration files. So your DB is “refreshed.”

But, for sure, you’d have worked with people who just don’t bother about the down() function in migration files. I’m not sure why, but some people don’t like writing a couple of lines of code in the down() function to make rolling back migrations easy.

Well, thanks to one such person, we have a new command coming in the next release of Laravel which helps us refresh our DB irrespective of whether proper instructions are given in the down() function or not.

Laravel 5.5 has the new “migrate:fresh” command, which, unlike the “refresh” command, just drops all the tables in the DB and runs the up() function in all your migration files. So even if one of your teammate “forgot” to write the down() function, you’ll still have a fresh DB whenever you want. This feature is coming into Laravel 5.5 as a PR from a user. If you’re app is written in Laravel 5.4 and already want this feature, there’s a package for that too (I’m not sure what it’s called).

Laravel 5.5 migrate:fresh
Image by Taylor Otwell, Laravel 5.5’s migrate:fresh command

Oh, and yeah, you do have the –seed option with the migrate:fresh command.

Sunny Srinidhi
Coding, reading, sleeping, listening, watching, potato. INDIAN. "If you don't have time to do it right, when will you have time to do it over?" - John Wooden
https://blog.contactsunny.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top