If you use Laravel, you’d be familiar with migrations. Migrations are commands that you run in Laravel to make changes to your database so that you don’t have to use the database manually and these commands can be shared with your team. There are variations to the migration command, including migrate:fresh
and migrate:refresh
which are similar. So, what is the difference?
migrate:fresh
drops all your tables in your database and rerun all the migrations whereas migrate:refresh
rollbacks each of your migration batches then rerun all the migrations. The difference is simply that migrate:fresh
empties your database instantly whereas migrate:refresh
did it step by step.
As you can see in the top image, migrate:fresh
dropped all tables but migrate:refresh
stated that it rolled back all migrations.
Although the methods are different, they have the same purpose. They are both used when you want to rerun all your migrations. For example, you want to empty all your data when you develop, then you can use either of those commands.
The
Laravel Documentationmigrate:fresh
command will drop all tables from the database and then execute themigrate
command
The
Laravel Documentationmigrate:refresh
command will roll back all of your migrations and then execute themigrate
command. This command effectively re-creates your entire database
What is rollback?
rollback command rolls back the last batch of migrations, which may include multiple migration files. Rolls back here means undoing your migration. The way rollback works is by running the down function in your migration when you run the command php artisan migrate:rollback
. Migration is run in batches meaning that if in one batch you run 2 migrations, then that 2 migrations are rolled back.
Hope you understand what rollback means now if you didn’t before.
Are they often used?
They are mostly used in development on your local server. Well, even rollback isn’t recommended to be used on development. You obviously don’t want to lose valuable data on production. You could lose your hard earned data if you run those commands.
But, in development, it is acceptable. There might be cases where you want to start over or you ran the wrong migrations. So, the answer is yes in development and no in production.
Which is more effective?
The fact is that most developers don’t really test their down function in their migrations. They just write it in case they are needed. If the down function is wrong, the rollback will be disturbed. So, considering that migrate:refresh
depends on the down function, there might be errors. So, it is better to use migrate:fresh
which doesn’t depend on down functions and just empty your database to fill it again.
Closing Statement
So, there you go, that’s the difference. Hope you got a satisfying answer.
Now that you understand more about Laravel, if you’re looking for where to host your Laravel App, I recommend Cloudways. It is compatible, fast, reliable and has a specific option for Laravel. There are also tons of resources about Laravel hosting on Cloudways. Go ahead and check it out!
If you’re looking to refresh your PHP skill, you can try Codecademy’s free PHP course. Go ahead and check out the syllabus to see if it fits you. If you’re interested, go and try it for free!
Feel free to check the rest of my blog if you’re interested in web development topics especially Laravel and JavaScript.