phinx 迁移 'up' 与 'change' 函数
phinx migration 'up' vs. 'change' function
我使用过 phinx 迁移并使用了它的 'up' 和 'change' 函数,但我没有注意到它们之间的任何区别。下面是我的迁移文件。
<?php
use Phinx\Migration\AbstractMigration;
class MediaManager extends AbstractMigration
{
public function up()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}
public function change()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}
}
谁能告诉我这两个函数的区别?提前致谢。
Phinx 0.2.0 添加了一个称为可逆迁移的新功能。您可以使用 change
方法实现可逆迁移。如果你已经实现了 change
方法,你真的不需要写 down
或 up
方法。在 change
方法中向上迁移你的逻辑时,Phinx 会计算出如何为你自动向下迁移。
当您定义 table 结构等时,可逆迁移很有用
示例(使用更改方法的可逆迁移)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
}
示例(与上面相同的迁移,不使用更改方法)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Down.
*/
public function down()
{
$this->dropTable('user_logins');
}
}
我使用过 phinx 迁移并使用了它的 'up' 和 'change' 函数,但我没有注意到它们之间的任何区别。下面是我的迁移文件。
<?php
use Phinx\Migration\AbstractMigration;
class MediaManager extends AbstractMigration
{
public function up()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}
public function change()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}
}
谁能告诉我这两个函数的区别?提前致谢。
Phinx 0.2.0 添加了一个称为可逆迁移的新功能。您可以使用 change
方法实现可逆迁移。如果你已经实现了 change
方法,你真的不需要写 down
或 up
方法。在 change
方法中向上迁移你的逻辑时,Phinx 会计算出如何为你自动向下迁移。
当您定义 table 结构等时,可逆迁移很有用
示例(使用更改方法的可逆迁移)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
}
示例(与上面相同的迁移,不使用更改方法)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUserLoginsTable extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Down.
*/
public function down()
{
$this->dropTable('user_logins');
}
}