1

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 ?

Nisarg
  • 3,024
  • 5
  • 32
  • 54

3 Answers3

1

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
elfarqy
  • 161
  • 1
  • 4
-1

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....

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Amitesh Kumar
  • 3,051
  • 1
  • 26
  • 42
  • Check edit. Yes I checked this link. See - http://www.yiiframework.com/doc-2.0/guide-db-migrations.html#drop-table. But It is saying I have to create new class for drop table. But I want to use existing one. – Nisarg Dec 22 '15 at 06:07
  • no you don't need another class to drop the table , in my code it is working properly. – Amitesh Kumar Dec 22 '15 at 06:12
-1

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

DimaS
  • 571
  • 2
  • 5
  • 19