<?php

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

/**
 * Class m240527_083421_create_table_order_status_history
 */
class m240527_083421_create_table_order_status_history extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->dropColumn('order', 'status');
        $this->addColumn('order', 'order_status_alias', Schema::TYPE_STRING);
        $this->dropTable('order_status_history');
        $this->createTable('order_status_history', [
            'id' => 'pk',
            'id_order' => Schema::TYPE_INTEGER,
            'order_status_alias' => Schema::TYPE_STRING,
            'date' => Schema::TYPE_DATETIME,
            'id_user' => Schema::TYPE_INTEGER
        ]);
        $this->createIndex('id_order', 'order_status_history', 'id_order');
        $this->createIndex('id_user', 'order_status_history', 'id_user');
        $this->addForeignKey('fk_id_order', 'order_status_history', 'id_order', 'order', 'id');
        $this->addForeignKey('fk_id_user', 'order_status_history', 'id_user', 'user', 'id');
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropColumn('order', 'order_status_alias');
        $this->addColumn('order', 'status', Schema::TYPE_STRING);

        $this->dropIndex('id_order', 'order_status_history');
        $this->dropIndex('id_user', 'order_status_history');
        $this->dropForeignKey('fk_id_order', 'order_status_history');
        $this->dropForeignKey('fk_id_user', 'order_status_history');
        $this->dropTable('order_status_history');
    }
}
