Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollback one specific migration in Laravel

I want

to rollback only :

Rolled back: 2015_05_15_195423_alter_table_web_directories


I run

php artisan migrate:rollback, 3 of my migration are rolling back.

Rolled back: 2015_05_15_195423_alter_table_web_directories Rolled back: 2015_05_13_135240_create_web_directories_table Rolled back: 2015_05_13_134411_create_contacts_table 

I delete

both of my web_directories and my contacts table unintentionally. I never want that to happen, and if I can rollback only that specific one, this disaster will never happen.

like image 613
code-8 Avatar asked May 17 '15 14:05

code-8


People also ask

How do I rollback a specific migration?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements.

How do I run a specific migration in laravel?

To run the specific migration in Laravel, you need to use --path option with the php artisan migrate command. Let's take a simple example, we have '2019_12_04_131405_create_payments_table. php' migration in the database/migrations directory and we would like to run this migration.


Video Answer


2 Answers

Laravel 5.3+

Rollback one step. Natively.

php artisan migrate:rollback --step=1 

And here's the manual page: docs.


Laravel 5.2 and before

No way to do without some hassle. For details, check Martin Bean's answer.

like image 132
Yauheni Prakopchyk Avatar answered Sep 16 '22 11:09

Yauheni Prakopchyk


If you look in your migrations table, then you’ll see each migration has a batch number. So when you roll back, it rolls back each migration that was part of the last batch.

If you only want to roll back the very last migration, then just increment the batch number by one. Then next time you run the rollback command, it’ll only roll back that one migration as it’s in a “batch” of its own.

Alternatively, from Laravel 5.3 onwards, you can just run:

php artisan migrate:rollback --step=1 

That will rollback the last migration, no matter what its batch number is.

like image 24
Martin Bean Avatar answered Sep 20 '22 11:09

Martin Bean