1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\rbac\commands; |
4
|
|
|
|
5
|
|
|
use yii\console\controllers\BaseMigrateController; |
6
|
|
|
use yii\db\Connection; |
7
|
|
|
use yii\db\Query; |
8
|
|
|
use yii\di\Instance; |
9
|
|
|
use yii\helpers\Console; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MigrateController |
13
|
|
|
* |
14
|
|
|
* Below are some common usages of this command: |
15
|
|
|
* |
16
|
|
|
* ``` |
17
|
|
|
* # creates a new migration named 'create_rule' |
18
|
|
|
* yii rbac/migrate/create create_rule |
19
|
|
|
* |
20
|
|
|
* # applies ALL new migrations |
21
|
|
|
* yii rbac/migrate |
22
|
|
|
* |
23
|
|
|
* # reverts the last applied migration |
24
|
|
|
* yii rbac/migrate/down |
25
|
|
|
* ``` |
26
|
|
|
*/ |
27
|
|
|
class MigrateController extends BaseMigrateController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Connection The database connection |
31
|
|
|
*/ |
32
|
|
|
public $db = 'db'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public $migrationTable = '{{%auth_migration}}'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public $migrationPath = '@app/rbac/migrations'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public $templateFile = '@vendor/yii2mod/yii2-rbac/views/migration.php'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function init() |
53
|
|
|
{ |
54
|
|
|
$this->db = Instance::ensure($this->db, Connection::class); |
55
|
|
|
|
56
|
|
|
parent::init(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Connection |
61
|
|
|
*/ |
62
|
|
|
public function getDb(): Connection |
63
|
|
|
{ |
64
|
|
|
return $this->db; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
protected function getMigrationHistory($limit) |
71
|
|
|
{ |
72
|
|
|
if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) { |
73
|
|
|
$this->createMigrationHistoryTable(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$history = (new Query()) |
77
|
|
|
->select(['apply_time']) |
78
|
|
|
->from($this->migrationTable) |
79
|
|
|
->orderBy(['apply_time' => SORT_DESC, 'version' => SORT_DESC]) |
80
|
|
|
->limit($limit) |
81
|
|
|
->indexBy('version') |
82
|
|
|
->column($this->db); |
83
|
|
|
|
84
|
|
|
unset($history[self::BASE_MIGRATION]); |
85
|
|
|
|
86
|
|
|
return $history; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
protected function addMigrationHistory($version) |
93
|
|
|
{ |
94
|
|
|
$this->db->createCommand()->insert($this->migrationTable, [ |
95
|
|
|
'version' => $version, |
96
|
|
|
'apply_time' => time(), |
97
|
|
|
])->execute(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
protected function removeMigrationHistory($version) |
104
|
|
|
{ |
105
|
|
|
$this->db->createCommand()->delete($this->migrationTable, [ |
106
|
|
|
'version' => $version, |
107
|
|
|
])->execute(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Creates the migration history table. |
112
|
|
|
*/ |
113
|
|
|
protected function createMigrationHistoryTable() |
114
|
|
|
{ |
115
|
|
|
$tableName = $this->db->schema->getRawTableName($this->migrationTable); |
116
|
|
|
|
117
|
|
|
$this->stdout("Creating migration history table \"$tableName\"...", Console::FG_YELLOW); |
118
|
|
|
|
119
|
|
|
$this->db->createCommand()->createTable($this->migrationTable, [ |
120
|
|
|
'version' => 'VARCHAR(180) NOT NULL PRIMARY KEY', |
121
|
|
|
'apply_time' => 'INTEGER', |
122
|
|
|
])->execute(); |
123
|
|
|
|
124
|
|
|
$this->db->createCommand()->insert($this->migrationTable, [ |
125
|
|
|
'version' => self::BASE_MIGRATION, |
126
|
|
|
'apply_time' => time(), |
127
|
|
|
])->execute(); |
128
|
|
|
|
129
|
|
|
$this->stdout("Done.\n", Console::FG_GREEN); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|