【问题标题】:phinx migration 'up' vs. 'change' functionphinx 迁移“向上”与“更改”功能
【发布时间】:2018-02-07 12:44:51
【问题描述】:

我使用了 phinx 迁移并使用了它的“向上”和“更改”功能,但我没有注意到它们之间有任何区别。以下是我的迁移文件。

    <?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();
        }
    }

}

谁能告诉我这两个函数的区别?提前致谢。

【问题讨论】:

    标签: php sql version-control migration phinx


    【解决方案1】:

    Phinx 0.2.0 添加了一项称为可逆迁移的新功能。您可以使用change 方法实现可逆迁移。如果你已经实现了change 方法,你真的不需要写downup 方法。在 change 方法中向上迁移您的逻辑时,Phinx 会自动为您计算如何向下迁移。

    在定义表结构等时,可逆迁移很有帮助。

    示例(使用更改方法的可逆迁移)

    <?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');
        }
    }
    

    阅读更多关于Reversible Migrastions Phinx的信息。

    【讨论】:

    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2016-09-13
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多