我想要的

仅回退:

回滚:2015_05_15_195423_alter_table_web_directories


我跑

PHP artisan migrate:rollback,我的3个迁移都在回滚。

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

我删除

我的web_directory和我的联系人表都是无意的。我不希望发生这种情况,如果我只能回滚特定的一个,这种灾难就永远不会发生。


当前回答

现在回答这个问题可能有点晚了,但我觉得这里有一个非常好的、干净和有效的方法。我会尽量详细的。

在创建迁移之前,创建不同的目录,如下所示:

    database
       | 
       migrations
            |
            batch_1
            batch_2
            batch_3

然后,在创建迁移时运行以下命令(以表为例):

     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

上面的命令将迁移文件置于给定的目录路径中。然后,您可以简单地运行以下命令通过指定的目录迁移文件。

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

*注意:您可以将batch_1更改为batch_2或batch_3或任何您存储迁移文件的文件夹名称。只要它仍然在database/migrations目录或某个指定的目录中。

接下来,如果你需要回滚特定的迁移,你可以批量回滚,如下所示:

    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

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

使用这些技术将使您能够更灵活地控制数据库以及对模式的任何修改。

其他回答

试试这个:

php artisan migrate:rollback --step=1

这是从Laravel 5.3 +工作

如果您查看您的迁移表,那么您将看到每个迁移都有一个批编号。因此,当您回滚时,它将回滚上一批中的每个迁移。

如果您只想回滚最后一次迁移,那么只需将批批号增加1。然后,下次运行rollback命令时,它将只回滚一次迁移,因为它是在自己的“批处理”中。

或者,从Laravel 5.3开始,你可以运行:

php artisan migrate:rollback --step=1

这将回滚最后一次迁移,不管它的批号是什么。

如果你不能做到@Martin Bean告诉你的,那么你可以尝试另一个技巧。

创建一个新的迁移,在该文件上,在up()方法中插入你想要回滚的迁移的down()方法中的内容,在down()方法中插入up()方法中的内容。

例如,如果你最初的迁移是这样的

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

然后在新的迁移文件中这样做

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

然后运行migrate,它会删除这个表。 如果你想要回滚它。

回退一步。本机。

php artisan migrate:rollback --step=1

回滚两步。本机。

php artisan migrate:rollback --step=2

如果您可以访问数据库,则有一个更简单的解决方案。您可以从迁移表中删除记录,然后删除表。SQL客户端。

或者可以使用

php artisan migrate:rollback --path=... 

就像很多答案一样。记住路径是Location。您甚至可以像这样删除模块迁移。(从任何地方迁移)

php artisan migrate:rollback --path=Modules/YourModule/database/migrations/2020_05_15_xxxxxx_create_your_table.php

记住,如果你使用的是linux服务器,小心区分大小写。你必须在开头加上/Database/Migrations。

/Database/Migrations/2020_05_15_xxxxxx_create_your_table.php