Rollback one specific migration in Laravel

前端 未结 19 1756
梦毁少年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:52

    It might be a little late to answer this question but here's a very good, clean and efficient way to do it I feel. I'll try to be as thorough as possible.

    Before creating your migrations create different directories like so:

        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 commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned 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 whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.

    Next if you need to rollback your specific migrations you can rollback by batch 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 techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.

提交回复
热议问题