Rollback one specific migration in Laravel

前端 未结 19 1477
梦毁少年i
梦毁少年i 2020-12-02 04:14

I want

to rollback only :

Rolled back: 2015_05_15_195423_alter_table_web_directories


I run

php artisan migrate:roll

相关标签:
19条回答
  • 2020-12-02 04:43

    Another alternative to those mentioned if you need to do this a bunch of times with the same migration(s). Personally I think this adds a lot of flexibility to your migrations.

    Add database/migrations to your autoload object in your composer.json like this:

    "autoload": {
            "psr-4": {
                "App\\": "app/"
            },
            "classmap": [
                "database/seeds",
                "database/factories",
                "database/support",
                "database/migrations" // add this line
            ]
        },
    

    Then add namespace Database\Migrations; to all of your migration files.

    Then run $ composer dump-autoload to refresh your composer.lock file.

    Then, assuming your class name for the migration is AlterTableWebDirectories, you can create a command like this:

    $ php artisan make:command DropAlterTableWebDirectories
    

    And write this logic in your handle() method:

    public function handle {
       (new AlterTableWebDirectories)->down();
       DB::raw("delete from migrations where migration like '%alter_table_web_directories%'");
    }
    

    This will do exactly what you want. If you want to decrement the migration count instead of deleting it, you can probably figure out how to change the DB:raw command.

    This command could be extended to allow you to dynamically choose which migration you're dropping it by passing an argument into the command.

    Then when you're reading to migrate that file again, you can just run php artisan migrate and it will only migrate that one.

    This process allows you to make specific changes to migrations without having to do a full refresh and seed each time.

    Personally I need to do that a lot because my seeds are rather large.

    0 讨论(0)
  • 2020-12-02 04:45

    better to used refresh migrate

    You may rollback & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will rollback & re-migrate the last two migrations:

    php artisan migrate:refresh --step=2
    

    otherwise used rollback migrate

    You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last three migrations:

    php artisan migrate:rollback --step=3
    

    for more detail about migration see

    0 讨论(0)
  • 2020-12-02 04:47
    php artisan migrate:rollback --path=/database/migrations/0000_00_00_0000_create_something_table.php
    
    0 讨论(0)
  • 2020-12-02 04:47

    It's quite easy to roll back just a specific migration.
    Since the command php artisan migrate:rollback, undo the last database migration, and the order of the migrations execution is stored in the batch field in the migrations table.

    You can edit the batch value of the migration that you want to rollback and set it as the higher. Then you can rollback that migration with a simple:

    php artisan migrate:rollback
    

    After editing the same migration you can execute it again with a simple

    php artisan migrate
    

    NOTICE: if two or more migrations have the same higher value, they will be all rolled back at the same time.

    0 讨论(0)
  • 2020-12-02 04:48

    1.) Inside the database, head to the migrations table and delete the entry of the migration related to the table you want to drop.

    Migration table image example

    2.) Next, delete the table related to the migration you just deleted from instruction 1.

    Delete table image example

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

    0 讨论(0)
  • 2020-12-02 04:49

    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.

    0 讨论(0)
提交回复
热议问题