phpsqlversion-controlmigrationphinx

phinx migration 'up' vs. 'change' function


I have used phinx migration and used it's 'up' and 'change' functions but i did'nt notice any difference between them. Below is my migration file.

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

}

Can anybody tell me the difference between these two functions ? Thanks in advance.


Solution

  • Phinx 0.2.0 added a new feature called reversible migrations. You can implement reversible migrations using change method. If you have implemented change method, you don't really need to write down or up methods. While migrating up your logic in change method will be executed and Phinx will figure out how to migrate down automatically for you.

    Reversible migrations are helpful when you are defining table structure etc.

    Example (Reversible Migrations using Change Method)

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

    Example (Same migration as above without using change method)

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

    Read more about Reversible Migrastions Phinx.