Understanding Migrations help


It is my understanding that Migrations are used as a safety precaution in case things do go bad but…

What I cannot work out is how to create in the migration file the data that goes in it.

For example, lets say I am in the root of the app and type in GIT BASH yii migrate/create my_first-migration

A file is created:


But in that file, everything is empty:


use yii\db\Schema;

use yii\db\Migration;

class m150415_202300_my_first-migration extends Migration


    public function up()



    public function down()


        echo "m150415_202300_add_book_table cannot be reverted.\n";

        return false;




    // Use safeUp/safeDown to run migration code within a transaction

    public function safeUp()




    public function safeDown()





Is there a way to get the migration synthax fast rather than typing everything by hand? As I am not understanding the point of creating this safety precaution if we can still make a synthax mistake and cause some problems to the database later on on migrate up.

Thank you in advance for your explanations.


No. Currently you should implement up and down methods yourself.

The purpose of migrations is to put database schema into source control.

I see, so it can be quite a bit of a job, if I understand the migration is what you create first by hand on your local machine, then migrate up to mysql (still on the local).

I first thought you added the database changes with the phpmyadmin gui first and then had a tool to extract the changes to the php migrating up file. Which for me would have made more sense:-)

Thank you,


I’m not aware of any way to generate SQL schema diff reliably enough. Mistake cost is very high in this case.

Well, it is somewhat possible todo this between databases and entire servers with tools like mysqldiff. Syncing schemes via MySQL Workbench is another way. The last method is also a very nice way to check if you haven’t forgotten anything in your migrations. But a fully automated solution would have to do a lot of guesswork and will be inherently prone to errors.

I understand now, thank you for the reply:-)