运行 Phinx 第二次迁移未使用新迁移更新数据库
Running Phinx migrate a second time does not update the database with the new migration
我创建了以下迁移。它在我第一次 运行 时工作,但是如果我对迁移进行更改 - 例如添加新列 - 当我 运行 phinx mingrate -c src/phinx-config.php
它不会更新数据库。
它似乎什么也没做。如果我从数据库中的 phinxlog
中删除条目,并删除 user
table,它将重新创建 table。否则,不进行任何更改。
如果我从 phinxlog
中删除条目而不删除 table,我会收到一条错误消息,指出 user
table 已经存在。我虽然这是 down()
的目的,所以它可以删除 table?
这是我的代码:
<?php
use \StudentApp\Migrations\Migration;
class AppMigration extends Migration
{
public function up()
{
$this->schema->create('users', function(Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('email');
$table->string('password');
$table->string('token');
$table->timestamp('token_expiry');
$table->timestamps();
});
}
public function down()
{
$this->schema->drop('users');
}
}
知道为什么当我重新运行 migrate
时数据库没有更新吗?
Phinx(和大多数其他迁移库)不会检查每个迁移是否有更改。 Phinx 查看文件名开头的时间戳是否列在 phinxlog table 中,如果不存在,Phinx 将 运行 迁移。
因此,当您第一次 运行 迁移时,Phinx 会在您的迁移文件夹中找到 20161123122600_app_migration.php
,它会 运行 迁移的 up
方法并应用那里的变化。然后它将 20161123122600
添加到日志中。因为下次你 运行 migrate
时,Phinx 会看到 20161123122600
已经存在,它只会传递给下一个。
所有这一切的要点是,一旦您的迁移处于版本控制中,您就不应该更改迁移(其他人可能已经使用过它,并且如果他们签出新的,将会遇到与您遇到的相同的问题已应用的迁移版本)。最好的办法是创建一个新的迁移来修改现有的 table(并包括恢复更改的回滚),尤其是在迁移已被推送或合并的情况下。
如果您尚未推送更改,则可以回滚迁移(这将调用 down
方法),然后添加新列并重新运行 迁移。然后你应该压缩提交,这样人们就不能签出未修改的迁移。总而言之,为这个变化创建一个新的迁移会更容易。
我创建了以下迁移。它在我第一次 运行 时工作,但是如果我对迁移进行更改 - 例如添加新列 - 当我 运行 phinx mingrate -c src/phinx-config.php
它不会更新数据库。
它似乎什么也没做。如果我从数据库中的 phinxlog
中删除条目,并删除 user
table,它将重新创建 table。否则,不进行任何更改。
如果我从 phinxlog
中删除条目而不删除 table,我会收到一条错误消息,指出 user
table 已经存在。我虽然这是 down()
的目的,所以它可以删除 table?
这是我的代码:
<?php
use \StudentApp\Migrations\Migration;
class AppMigration extends Migration
{
public function up()
{
$this->schema->create('users', function(Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('email');
$table->string('password');
$table->string('token');
$table->timestamp('token_expiry');
$table->timestamps();
});
}
public function down()
{
$this->schema->drop('users');
}
}
知道为什么当我重新运行 migrate
时数据库没有更新吗?
Phinx(和大多数其他迁移库)不会检查每个迁移是否有更改。 Phinx 查看文件名开头的时间戳是否列在 phinxlog table 中,如果不存在,Phinx 将 运行 迁移。
因此,当您第一次 运行 迁移时,Phinx 会在您的迁移文件夹中找到 20161123122600_app_migration.php
,它会 运行 迁移的 up
方法并应用那里的变化。然后它将 20161123122600
添加到日志中。因为下次你 运行 migrate
时,Phinx 会看到 20161123122600
已经存在,它只会传递给下一个。
所有这一切的要点是,一旦您的迁移处于版本控制中,您就不应该更改迁移(其他人可能已经使用过它,并且如果他们签出新的,将会遇到与您遇到的相同的问题已应用的迁移版本)。最好的办法是创建一个新的迁移来修改现有的 table(并包括恢复更改的回滚),尤其是在迁移已被推送或合并的情况下。
如果您尚未推送更改,则可以回滚迁移(这将调用 down
方法),然后添加新列并重新运行 迁移。然后你应该压缩提交,这样人们就不能签出未修改的迁移。总而言之,为这个变化创建一个新的迁移会更容易。