bizley / yii2-migration

Yii 2 Migration Creator And Updater
Apache License 2.0
291 stars 36 forks source link

Migration/Update drops column instead of using renameColumn command #150

Closed hkbertoson closed 3 years ago

hkbertoson commented 3 years ago

Describe the bug If I run migration/update it will generate a migration that uses the dropColumn and addColumn command.

Expected behavior This causes issues with losing data. It would be better to have it use renameColumn to change the column name instead.

Additional context

Affected versions latest version

Migration Update FIle

<?php

use yii\db\Migration;

class m211013_163356_update_table_user extends Migration
{
    public function up()
    {
        $this->dropColumn('{{%user}}', 'password_hash');

        $this->addColumn('{{%user}}', 'password', $this->string()->notNull()->defaultValue('')->after('access_token'));
    }

    public function down()
    {
        $this->dropColumn('{{%user}}', 'password');

        $this->addColumn('{{%user}}', 'password_hash', $this->string()->notNull()->defaultValue(''));
    }
}

Updated Migration File

<?php

use yii\db\Migration;

class m211013_163356_update_table_user extends Migration
{
    public function up()
    {
        $this->renameColumn('{{%user}}', 'password_hash', 'password');
    }

    public function down()
    {
        $this->renameColumn('{{%user}}', 'password', 'password_hash');
    }
}
bizley commented 3 years ago

https://github.com/bizley/yii2-migration#renaming