简体   繁体   English

如何在 Laravel 中修改迁移?

[英]How can I modify a migration in Laravel?

I'm trying to modify a existing migration.我正在尝试修改现有的迁移。 Here is my current migration class:这是我当前的迁移课程:

class CreateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::create('log_for_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('table_name');
            $table->string('error_message');
            $table->unsignedTinyInteger('error_code');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('log_for_user');
    }
}

I've executed the php artisan migrate command once.我已经执行过一次php artisan migrate命令。 Now I need to add ->nullable() method to the error_message column.现在我需要将->nullable()方法添加到error_message列。 So I edited my migration, something like this:所以我编辑了我的迁移,如下所示:

.
.
    $table->string('error_message')->nullable();
.
.

But when I execute php artisan migrate again, it says:但是当我再次执行php artisan migrate时,它说:

Nothing to migrate.没什么好迁移的。

How can I apply the new version of the migration?如何应用新版本的迁移?

You should create a new migration using command:您应该使用命令创建一个新的迁移:

php artisan make:migration update_error_message_in_log_for_user_table

Then, in that created migration class, add this line, using the change method like this:然后,在创建的迁移类中,使用如下change方法添加这一行:

class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}

To make these changes and run the migration, use the command:要进行这些更改并运行迁移,请使用以下命令:

php artisan migrate

and to rollback the changes, use the command:要回滚更改,请使用以下命令:

php artisan migrate:rollback

You may rollback a limited number of migrations by providing the step option to the rollback command.您可以通过为 rollback 命令提供step选项来回滚有限数量的迁移。 For example, the following command will rollback the last five migrations:例如,以下命令将回滚最近的五次迁移:

php artisan migrate:rollback --step=5

See more about Modifying columns with Migration查看有关使用迁移修改列的更多信息

If your app is not in production and you seed your data, the best you can do is to run:如果您的应用程序不在生产中并且您为数据播种,那么您能做的最好的事情就是运行:

php artisan migrate:refresh --seed

This command will drop all tables and recreate them.此命令将删除所有表并重新创建它们。 Then it will seed the data.然后它将播种数据。

If you will create additional migrations for each change during development, you'll end up with hundreds of migrations classes.如果您在开发过程中为每个更改创建额外的迁移,您最终将拥有数百个迁移类。

You can use thechange method, it allows you to modify some existing column types to a new type or modify the column's attributes.您可以使用change方法,它允许您将一些现有的列类型修改为新类型或修改列的属性。

For example modify a column to be nullable:例如将一列修改为可以为空:

Schema::table('log_for_user', function ($table) {
    $table->string('error_message')->nullable()->change();
});

But first of all you'll need the doctrine/dbal package但首先你需要doctrine/dbal

composer require doctrine/dbal

There is one more option.还有一种选择。 Roll back the migration, edit the file, and run it again.回滚迁移,编辑文件,然后再次运行。

This is a fairly common thing to do on a local development server while you're working out the bugs in a new piece of code.在本地开发服务器上处理新代码中的错误时,这是​​很常见的事情。 Using the method from the accepted answer, you might end up with 17 migrations for creating a single table!使用已接受答案中的方法,您最终可能会进行 17 次迁移以创建单个表!

php artisan migrate
# realize you've made an error
php artisan migrate:rollback
# edit your migration file
php artisan migrate

The number of steps back to take can be specified on the command line if needed.如果需要,可以在命令行上指定后退的步骤数。

# undo the last 3 migrations
php artisan migrate:rollback --step=3

Or you can specify a particular migration that needs undoing.或者您可以指定需要撤消的特定迁移。

# undo one specific migration
php artisan migrate:rollback --path=./database/migrations/2014_10_12_100000_create_users_table.php

There are 2 ways to do this:有两种方法可以做到这一点:

  1. Run php artisan migrate:refresh .运行php artisan migrate:refresh This will rollback all your migrations and migrate all your migrations.这将回滚您的所有迁移并迁移您的所有迁移。 If you run this command, all the data inserted in your database will be lost.如果运行此命令,则插入数据库中的所有数据都将丢失。
  2. Run php artisan make:migration enter_your_migration_name_here .运行php artisan make:migration enter_your_migration_name_here Then insert this in your migration:然后将其插入到您的迁移中:

    $table->string('error_message')->nullable()->change();

    Then run php artisan migrate to make your table changes.然后运行php artisan migrate以更改您的表。 (Take note that when you do this, you have require composer require doctrine/dbal in your composer) (请注意,当您执行此操作时,您的composer require doctrine/dbal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM