Rollback one specific migration in Laravel

I want to

only for rollback:

Rolled back: 2015_05_15_195423_alter_table_web_directories




I'm runing

php artisan migrate:rollback , 3 of my migrations are rolled 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'm deleting

both my web_directories and my contacts table are inadvertently. I never want this to happen, and if I can only roll back this particular case, this disaster will never happen.

+192
laravel laravel-5 database-migration laravel-4
May 17 '15 at
source share
14 answers

If you look in the migrations table, you will see that each migration has a batch number. Therefore, when you roll back, it rolls back every migration that was part of the last batch.

If you only want to undo the last migration, just increase the batch number by one. Then the next time you run the rollback command, it will roll back only one migration, like its own, to its "batch".

+119
May 17 '15 at 14:44
source share

Laravel 5.3 +

Rollback one step. Congenital

 php artisan migrate:rollback --step=1 

And here is the manual page: docs .




Laravel 5.2 and up

It is impossible to do without any hassle. For more information, see Martin Bean.

+204
Jun 13 '16 at 16:48
source share

Each time you roll back, you get the latest migration package. use command

 php artisan migrate:rollback --step=1 
+20
Apr 02 '18 at 22:13
source share

If you can't do what @Martin Bean said, then you can try another trick.

Create a new migration and in this file, in the up () method, insert what in the down () method of the migration you want to cancel, and in the down () method, insert what is in the up () method.

For example, if your initial migration is like this

 public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id')->unsigned(); $table->string('name'); }); } public function down() { Schema::drop('users'); } 

then in the new migration file do this

 public function up() { Schema::drop('users'); } public function down() { Schema::create('users', function(Blueprint $table) { $table->increments('id')->unsigned(); $table->string('name'); }); } 

and then start the migration, it will delete the table. and if you again want to just roll back.

+17
May 18 '15 at 1:39
source share

It may be a little late to answer this question, but here is a very good, clean and effective way to do this, I feel. I will try to be as thorough as possible.

Before creating your migrations, create different directories:

  database | migrations | batch_1 batch_2 batch_3 

Then, when creating your migrations, run the following command (using your tables as an example):

  php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1 

or

  php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2 

or

  php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3 

The above commands will make the migration file within this directory. Then you can simply run the following command to transfer files through the designated directories.

  php artisan migrate alter_table_web_directories --path=database/migrations/batch_1 

* Note. You can change batch_1 to batch_2 or batch_3 or to any other name of the folder in which the migration files are stored. While it remains in the database / migration directory or in some specific directory.

Further, if you need to cancel your specific migrations, you can roll back the package as shown below:

  php artisan migrate:rollback --step=1 or try php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1 

or

  php artisan migrate:rollback --step=2 or try php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2 

or

  php artisan migrate:rollback --step=3 or try php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3 

Using these methods will allow you more flexibility and control over your database and any changes made to your schema.

+8
Jul 02 '16 at 1:31 on
source share

better use update migration

You can roll back & re-migrate a limited number of migrations by providing a step option for the upgrade command. For example, the following command will roll back & re-migrate the last two migrations:

 php artisan migrate:refresh --step=2 

otherwise, migration rollback is used

You can rollback a limited number of migrations by providing a step option for the rollback command. For example, the following command rolls back the last three migrations:

 php artisan migrate:rollback --step=3 

more about migration see

+8
Apr 16 '19 at 6:52
source share

Rollback one step. Congenital

 php artisan migrate:rollback --step=1 

Rollback in two steps. Congenital

 php artisan migrate:rollback --step=2 
+4
Dec 25 '17 at 16:38 on
source share

Use the command "php artisan migrate: rollback --step = 1" to roll back the migration 1 step back.

For more information check the link: - https://laravel.com/docs/master/migrations#running-migrations

+4
Jul 31 '18 at 14:02
source share

Move the tables one by one.

Change the batch number of the migration you want to roll back to the highest.

Start Migration: Rollback.

It may not be the most convenient way to solve large projects.

+3
Aug 27 '16 at 21:34
source share

If you want to modify the original migration file and migrate it again, you can use this package for migration. (Applicable to Laravel 5.4 or later)

First install the package in your Laravel project:

 composer require caloskao/migrate-specific 

Register the command in app/Console/Kernel.php :

 protected $commands = [ \CalosKao\MigrateSpecific::class ]; 

Now run this command to transfer the file

 php artisan migrate:specific database/migrations/table.php 
+2
Oct 25 '18 at 6:49
source share
 INSERT INTO homestead.bb_migrations (`migration`, `batch`) VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2') 

something like that

+1
Jan 24 '16 at 6:01
source share

1.) Inside the database, navigate to the migration table and delete the migration entry related to the table you want to delete.

Example image of a migration table

2.) Then delete the migration-related table that you just deleted from instruction 1.

Delete sample table image

3.) Finally, make the necessary changes to the migration file of the table that you deleted from instruction no. 2 then run php artisan migrate to reconfigure the table again.

+1
Feb 24 '19 at 3:37
source share

As specified in the Laravel guide , you can roll back a specific number of migrations using the --step

 php artisan migrate:rollback --step=5 
0
Aug 25 '16 at 16:15
source share

The best way is to create a new migration and make the necessary changes to it.

In the worst case ( if you have access to the database and everything is fine with RESET of this table data ):

  1. Go to DB and delete / rename migration record for your-specific-migration
  2. Delete the table created by your-specific-migration
  3. Run php artisan migrate --path=/database/migrations/your-specific-migration.php

This will force laravel to perform this particular migration, as there is no record of this in the Laravel migration history

0
Jul 08 '19 at 8:27
source share



All Articles