2
votes
  1. Running an XAMPP machine on Windows, I tried running the command:
    $ ./yii migrate/down
    Yii Migration Tool (based on Yii v2.0.15.1)

    Total 1 migration to be reverted:
            m180614_020037_create_table_place_lang

    Revert the above migration? (yes|no) [no]:yes
    *** reverting m180614_020037_create_table_place_lang
        > drop foreign key fk_place_lang_id_place from table place_lang ... done (time: 0.067s)
        > drop index idx_place_lang_id_place on place_lang ... done (time: 0.189s)
        > drop table place_lang ... done (time: 0.135s)
    *** failed to revert m180614_020037_create_table_place_lang (time: 0.396s)


    0 from 1 migrations were reverted.

    Migration failed. The rest of the migrations are canceled.

  1. I ran the migration below

    use yii\db\Migration;
    
    // Class m180614_020037_create_table_place_lang
    class m180614_020037_create_table_place_lang extends Migration
    {
        /**
         * {@inheritdoc}
         */
        public function up()
        {
            $this->createTable('place_lang', [
                'id' => $this->primaryKey()->unsigned(),
                'place_id' => $this->integer(11)->unsigned()->notNull(),
                'locality' => $this->string(45)->notNull(),
                'country' => $this->string(45)->notNull(),
                'lang' => $this->string(2)->notNull()
            ]);
    
    
        $this->createIndex(
            'idx_place_lang_id_place',
            'place_lang',
            'place_id'
        );
    
        $this->addForeignKey(
            'fk_place_lang_id_place',
            'place_lang',
            'place_id',
            'place',
            'id'
        );
    
    
    }
    
    /**
     * {@inheritdoc}
     */
    public function down()
    {
        $this->dropForeignKey('fk_place_lang_id_place', 'place_lang');
        $this->dropIndex('idx_place_lang_id_place', 'place_lang');
        $this->dropTable('place_lang');
    
        return false;
    }
    
    }

    The database table was dropped, but the migration failed and I could not progress down the migration stack.

There was another migration seen here below it in the stack:

<?php

use yii\db\Migration;

/**
 * Class m180614_015652_create_table_place
 */
class m180614_015652_create_table_place extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function up()
    {
        $this->createTable('place', [
            'id' => $this->primaryKey()->unsigned()->notNull(),
            'place_id' => $this->string(45)->notNull(),
            'lat' => $this->string(45)->notNull(),
            'lng' => $this->string(45)->notNull(),
            'country_code' => $this->string(2)->notNull(),
            'is_country' => $this->tinyInteger(4)->notNull()
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down()
    {
        $this->dropTable('place');

        return false;
    }

}

Is this a limitation of yii 2 on Windows and XAMPP or just a newbie error using the framework?

1
Also, when I run ./yii migrate/fresh everything works as expected - tfarmer4
you returned false in down(), so migration assumes it has failed. Remove return false. - Insane Skull

1 Answers

3
votes

Returning false in migration marks it as failed, so this is the reason. This is documented:

Not all migrations are reversible. For example, if the up() method deletes a row of a table, you may not be able to recover this row in the down() method. Sometimes, you may be just too lazy to implement the down(), because it is not very common to revert database migrations. In this case, you should return false in the down() method to indicate that the migration is not reversible.

https://www.yiiframework.com/doc/guide/2.0/en/db-migrations

You should probably not return anything if you don't want to control migrations flow.


./yii migrate/refresh works because it does not use down(). This command removes every table from database and runs migrate/up.