1
votes

I want to remove table using migration class method.

This is my migration class :

use yii\db\Schema;
use yii\db\Migration;

class m130524_201442_init extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%user}}', [
            'id' => Schema::TYPE_PK,
            'username' => Schema::TYPE_STRING . ' NOT NULL',
            'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL',
            'password_hash' => Schema::TYPE_STRING . ' NOT NULL',
            'password_reset_token' => Schema::TYPE_STRING,
            'email' => Schema::TYPE_STRING . ' NOT NULL',
            'firstname' => Schema::TYPE_STRING,
            'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',
            'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
            'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
        ], $tableOptions);
    }

    public function down()
    {
        $this->dropTable('{{%user}}');
    }
}

I migrated this class using yii migrate/to 130524_201442. After that I had done many other migrations and now I want to drop user table so I tried below commands,

yii migrate safeDown console\migrations\m130524_201442_init.php

yii migrate down console\migrations\m130524_201442_init.php

OUTPUT :

D:\Projects\wamp\www\yiiadvanced>yii migrate down console\migrations\m130524_201442_init.php
Yii Migration Tool (based on Yii v2.0.5)

No new migration found. Your system is up-to-date.

Also tried this one :

yii migrate/down 130524_201442

OUTPUT

D:\Projects\wamp\www\yiiadvanced>yii migrate/down 130524_201442
Yii Migration Tool (based on Yii v2.0.5)

Total 2 migrations to be reverted:
        m151222_063506_roles
        m130524_201442_init

Revert the above migrations? (yes|no) [no]:

If I enter "yes" then it will revert both one but i want to remove only m130524_201442_init.

So, which command should I use ?

3
Please, show the error message. - DimaS
Don't run migrate/down 130524_201442. Run something like migrate/down 1 to revert one last migration, migrate/down 2 to revert last two, etc. - Beowulfenator

3 Answers

1
votes

May be this is hard way, but works on me.

  • Delete the table manually, using phpmyadmin or other tools.
  • Update your migration file as you need.
  • By default, in yii database there is migration table, This table stores a history of all migrations are you doing in development mode. Delete value in version columns which is have same name with migrations file
-1
votes

Try this:

public function up() 
{ 
   $this->createTable('user', [
      'id' => Schema::TYPE_PK,
      'username' => Schema::TYPE_STRING . ' NOT NULL',
      'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL',
      'password_hash' => Schema::TYPE_STRING . ' NOT NULL',
      'password_reset_token' => Schema::TYPE_STRING,
      'email' => Schema::TYPE_STRING . ' NOT NULL',
      'firstname' => Schema::TYPE_STRING,
      'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',
      'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
      updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
      ], $tableOptions);
}

public function Down()
{
   $this->dropTable('user');
}

for more info click here....

-1
votes

It seems, You run wrong command. Please show the error message you receive.

In the console go to the directory where yii file located and try: php yii migrate/to m130524