Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kiaplayer/f3534b10295381509ed8 to your computer and use it in GitHub Desktop.
Save kiaplayer/f3534b10295381509ed8 to your computer and use it in GitHub Desktop.
<?php
use yii\db\Expression;
use yii\db\Schema;
use yii\db\Migration;
class m140702_115933_create_backend_user_table extends Migration
{
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
$this->createTable('{{%backend_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',
'email' => Schema::TYPE_STRING . ' NOT NULL',
'is_active' => Schema::TYPE_BOOLEAN . ' NOT NULL',
'created_by' => Schema::TYPE_INTEGER,
'created_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL',
'updated_by' => Schema::TYPE_INTEGER,
'updated_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL',
], $tableOptions);
$this->addForeignKey('fk_backend_user_created_by', '{{%backend_user}}', 'created_by', '{{%backend_user}}', 'id');
$this->addForeignKey('fk_backend_user_updated_by', '{{%backend_user}}', 'updated_by', '{{%backend_user}}', 'id');
$this->insert('{{%backend_user}}', [
'id' => 1,
'username' => 'admin',
'auth_key' => Yii::$app->getSecurity()->generateRandomKey(32),
'password_hash' => Yii::$app->getSecurity()->generatePasswordHash('admin'),
'email' => 'admin@eshop.local',
'is_active' => true,
'created_by' => null,
'created_at' => new Expression('NOW()'),
'updated_by' => null,
'updated_at' => new Expression('NOW()'),
]);
}
public function safeDown()
{
$this->dropTable('{{%backend_user}}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment