<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%merchant}}`.
*/
class m221219_123909_create_merchant_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->truncateTable('{{%webhook_log}}');
$this->db->createCommand('DELETE FROM `payment_request`')->execute();
$this->createTable('{{%merchant}}', [
'merchant_uuid' => $this->string(36)->unique()->notNull(),
'user_id' => $this->integer()->notNull(),
'name' => $this->string(255)->notNull(),
'url' => $this->string(255)->notNull(),
'image' => $this->string(255)->null(),
'commission' => $this->tinyInteger(1)->notNull()->defaultValue(1),
'commission_percent' => $this->tinyInteger(3)->notNull()->defaultValue(0),
'trc_wallet' => $this->string(128)->notNull(),
'webhook_url' => $this->string(255)->notNull(),
'status' => $this->integer()->notNull()->defaultValue(10),
'public_key' => $this->string(64)->notNull()->unique(),
'private_key' => $this->string(128)->notNull(),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->null(),
], $tableOptions);
$this->addPrimaryKey('merchant_PK', '{{%merchant}}', 'merchant_uuid');
$this->createIndex('user_id', '{{%merchant}}', 'user_id');
$this->addForeignKey('FK_merchant__user_id__user__id', '{{%merchant}}', 'user_id', '{{%user}}', 'id', 'CASCADE');
if (Yii::$app->db->schema->getTableSchema('{{%user_api_key}}')) {
$this->dropTable('{{%user_api_key}}');
}
$this->dropForeignKey('FK_payment_request__user_id', '{{%payment_request}}');
$this->dropColumn('{{%payment_request}}', 'user_id');
$this->addColumn('{{%payment_request}}', 'merchant_uuid', $this->string(36)->notNull()->after('id'));
$this->createIndex('payment_request__merchant_uuid', '{{%payment_request}}', 'merchant_uuid');
$this->addForeignKey('FK_payment_request__merchant_uuid', '{{%payment_request}}', 'merchant_uuid', '{{%merchant}}', 'merchant_uuid');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->truncateTable('{{%webhook_log}}');
$this->db->createCommand('DELETE FROM `payment_request`')->execute();
$this->dropForeignKey('FK_payment_request__merchant_uuid', '{{%payment_request}}');
$this->dropIndex('payment_request__merchant_uuid', '{{%payment_request}}');
$this->dropColumn('{{%payment_request}}', 'merchant_uuid');
$this->addColumn('{{%payment_request}}', 'user_id', $this->integer()->null()->after('id'));
$this->createIndex('user_id', '{{%payment_request}}', 'user_id');
$this->addForeignKey('FK_payment_request__user_id', '{{%payment_request}}', 'user_id', '{{%user}}', 'id', 'CASCADE');
$this->createTable('{{%user_api_key}}', [
'id' => $this->primaryKey(),
'user_id' => $this->integer()->notNull(),
'public_key' => $this->string(64)->notNull(),
'private_key' => $this->string(128)->notNull(),
'created_at' => $this->integer(),
], $tableOptions);
$this->createIndex('user_id', '{{%user_api_key}}', 'user_id');
$this->addForeignKey('user_id__user__id', '{{%user_api_key}}', 'user_id', '{{%user}}', 'id', 'CASCADE');
$this->dropForeignKey('FK_merchant__user_id__user__id', '{{%merchant}}');
$this->dropIndex('user_id', '{{%merchant}}');
$this->dropPrimaryKey('merchant_PK', '{{%merchant}}');
$this->dropTable('{{%merchant}}');
}
}