|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\Yii2\Setting\Migration; |
|
3
|
|
|
|
|
4
|
|
|
use RazonYang\Yii2\Setting\DbManager; |
|
5
|
|
|
use yii\base\InvalidConfigException; |
|
6
|
|
|
use yii\db\Migration; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class M190725110154Setting |
|
11
|
|
|
*/ |
|
12
|
|
|
class M190725110154Setting extends Migration |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var DbManager[] Targets to create log table for |
|
16
|
|
|
*/ |
|
17
|
|
|
private $managers = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @throws InvalidConfigException |
|
21
|
|
|
* @return DbManager[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function getManagers() |
|
24
|
|
|
{ |
|
25
|
|
|
if ($this->managers === []) { |
|
26
|
|
|
foreach (Yii::$app->getComponents() as $component) { |
|
27
|
|
|
try { |
|
28
|
|
|
$component = Yii::createObject($component); |
|
29
|
|
|
if ($component instanceof DbManager) { |
|
30
|
|
|
$this->managers[] = $component; |
|
31
|
|
|
} |
|
32
|
|
|
} catch (\Throwable $e) { |
|
33
|
|
|
Yii::error($e, __METHOD__); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($this->managers === []) { |
|
38
|
|
|
throw new InvalidConfigException('You should configure setting manager component before executing this migration.'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->managers; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function safeUp() |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($this->getManagers() as $manager) { |
|
48
|
|
|
$tableOptions = null; |
|
49
|
|
|
if ($manager->db->driverName === 'mysql') { |
|
50
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->db = $manager->db; |
|
54
|
|
|
$this->createTable($manager->settingTable, [ |
|
55
|
|
|
'id' => $this->string(32)->notNull()->comment('ID'), |
|
56
|
|
|
'description' => $this->string()->notNull()->comment('description'), |
|
57
|
|
|
'value' => $this->text()->notNull()->comment('Value'), |
|
58
|
|
|
'rules' => $this->text()->notNull()->comment('Validate Rules'), |
|
59
|
|
|
'is_secret' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('Is Secret'), |
|
60
|
|
|
'create_time' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('Create Time'), |
|
61
|
|
|
'update_time' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('Update Time'), |
|
62
|
|
|
], $tableOptions); |
|
63
|
|
|
|
|
64
|
|
|
$this->addPrimaryKey('setting_pk', $manager->settingTable, ['id']); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function safeDown() |
|
69
|
|
|
{ |
|
70
|
|
|
foreach ($this->getManagers() as $manager) { |
|
71
|
|
|
$this->db = $manager->db; |
|
72
|
|
|
$this->dropTable($manager->settingTable); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|