m160127_002445_create_fv_user.php
2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
use yii\db\Migration;
use thread\modules\user\User as ParentModule;
/**
* Class m160127_002445_create_fv_user
*
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class m160127_002445_create_fv_user extends Migration
{
/**
* @var string
*/
public $tableUser = '{{%user}}';
/**
* @var string
*/
public $tableUserGroup = '{{%user_group}}';
/**
* Get core DB connection
*/
public function init()
{
$this->db = ParentModule::getDb();
parent::init();
}
/**
* Implement migration
*/
public function safeUp()
{
/** Create table */
$this->createTable($this->tableUser, [
'id' => $this->primaryKey(11)->unsigned()->notNull()->comment('ID'),
'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('Group'),
'username' => $this->string(255)->unique()->notNull()->comment('Username'),
'email' => $this->string(255)->unique()->notNull()->comment('Email'),
'auth_key' => $this->string(32)->notNull()->comment('Auth key'),
'password_hash' => $this->string(255)->notNull()->comment('Password hash'),
'password_reset_token' => $this->string(255)->defaultValue(null)->comment('Password reset token'),
'published' => "enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Published'",
'deleted' => "enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Deleted'",
'created_at' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('Create time'),
'updated_at' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('Update time'),
]);
/** Create indexes */
$this->createIndex('published', $this->tableUser, 'published');
$this->createIndex('deleted', $this->tableUser, 'deleted');
$this->createIndex('group_id', $this->tableUser, 'group_id');
/** Add FKs */
$this->addForeignKey(
'fv_user_ibfk_1',
$this->tableUser,
'group_id',
$this->tableUserGroup,
'id',
'CASCADE',
'CASCADE'
);
}
/**
* Cancel migration
*/
public function safeDown()
{
/** Delete FK */
$this->dropForeignKey('fv_user_ibfk_1', $this->tableUser);
/** Drop indexes */
$this->dropIndex('deleted', $this->tableUser);
$this->dropIndex('published', $this->tableUser);
$this->dropIndex('group_id', $this->tableUser);
$this->dropIndex('username', $this->tableUser);
$this->dropIndex('email', $this->tableUser);
/** Drop table */
$this->dropTable($this->tableUser);
}
}