Use Change In Addition To Up And Down Methods In Yii Migration

Not a brand new idea. Of merging functionality of up and down functions in one change. Ror and https://github.com/robmorgan/phinx already have this feature.

i.e. if we have $this->createTable, addIndex etc we know that we need to delete table/column/index in down action.

The main problem here is tracking down the data that delete something that already exists. For example in up action we have dropTable dropColumn. In this case we don’t know how to recreate table or column without additional actions.

That good thing that in most cases we will create rather than remove something. And this ability will save some time for developer.

I proposed that approach to yii core team. But they refuse it https://github.com/yiisoft/yii2/issues/593#issuecomment-20397820

Perhaps they are right.

So I decided to implement it as extension

Here’s my code https://github.com/radzserg/yii-Rz/blob/master/db/ChangeMigration.php

All you need - use Rz\db\ChangeMigration as parent class for you migration. And implement you actions in change function

More details of that approach you can find here

http://radzserg.com/2013/07/10/yii-migration-use-change-in-addition-to-up-and-down/

I ask you to try and provide your feedback. What do you think about that. Maybe some ideas how we could improve that.

Thanks

Quick example




<?php

 

class m130626_132012_add_not_confirmed_users extends Rz\db\ChangeMigration

{

    public function change()

    {

        $this->addColumn('sm_user', 'confirmed', 'TINYINT(1) NOT NULL DEFAULT 0 after create_time');

        $this->addColumn('user', 'confirmed', 'TINYINT(1) NOT NULL DEFAULT 0 after create_time');

 

        $this->alterColumn('sm_user', 'user_id', 'INT', function() {

           $this->alterColumn('sm_user', 'user_id', 'INT NOT NULL');

        });

    }

 

}

Thanks for your nice suggestion. i will try it and let you know if there is any issue in it.Since in my current project i am writing lots of migration scripts.

Thanks again.