1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\db\Migration; |
10
|
|
|
use yii\log\DbTarget; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Initializes log table. |
14
|
|
|
* |
15
|
|
|
* The indexes declared are not required. They are mainly used to improve the performance |
16
|
|
|
* of some queries about message levels and categories. Depending on your actual needs, you may |
17
|
|
|
* want to create additional indexes (e.g. index on `log_time`). |
18
|
|
|
* |
19
|
|
|
* @author Alexander Makarov <[email protected]> |
20
|
|
|
* @since 2.0.1 |
21
|
|
|
*/ |
22
|
|
|
class m141106_185632_log_init extends Migration |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DbTarget[] Targets to create log table for |
26
|
|
|
*/ |
27
|
|
|
private $dbTargets = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws InvalidConfigException |
31
|
|
|
* @return DbTarget[] |
32
|
|
|
*/ |
33
|
6 |
|
protected function getDbTargets() |
34
|
|
|
{ |
35
|
6 |
|
if ($this->dbTargets === []) { |
36
|
6 |
|
$logger = Yii::getLogger(); |
37
|
6 |
|
if (!$logger instanceof \yii\log\Logger) { |
38
|
|
|
throw new InvalidConfigException('You should configure "logger" to be instance of "\yii\log\Logger" before executing this migration.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
$usedTargets = []; |
42
|
6 |
|
foreach ($logger->targets as $target) { |
43
|
6 |
|
if ($target instanceof DbTarget) { |
44
|
|
|
$currentTarget = [ |
45
|
6 |
|
$target->db, |
46
|
6 |
|
$target->logTable, |
47
|
|
|
]; |
48
|
6 |
|
if (!in_array($currentTarget, $usedTargets, true)) { |
49
|
|
|
// do not create same table twice |
50
|
6 |
|
$usedTargets[] = $currentTarget; |
51
|
6 |
|
$this->dbTargets[] = $target; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
if ($this->dbTargets === []) { |
57
|
|
|
throw new InvalidConfigException('You should configure "log" component to use one or more database targets before executing this migration.'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
6 |
|
return $this->dbTargets; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
public function up() |
64
|
|
|
{ |
65
|
6 |
|
$targets = $this->getDbTargets(); |
66
|
6 |
|
foreach ($targets as $target) { |
67
|
6 |
|
$this->db = $target->db; |
68
|
|
|
|
69
|
6 |
|
$tableOptions = null; |
70
|
6 |
|
if ($this->db->driverName === 'mysql') { |
71
|
|
|
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
72
|
6 |
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
$this->createTable($target->logTable, [ |
76
|
6 |
|
'id' => $this->bigPrimaryKey(), |
77
|
6 |
|
'level' => $this->integer(), |
78
|
6 |
|
'category' => $this->string(), |
79
|
6 |
|
'log_time' => $this->double(), |
80
|
6 |
|
'prefix' => $this->text(), |
81
|
6 |
|
'message' => $this->text(), |
82
|
6 |
|
], $tableOptions); |
83
|
|
|
|
84
|
6 |
|
$this->createIndex('idx_log_level', $target->logTable, 'level'); |
85
|
6 |
|
$this->createIndex('idx_log_category', $target->logTable, 'category'); |
86
|
|
|
} |
87
|
6 |
|
} |
88
|
|
|
|
89
|
6 |
|
public function down() |
90
|
|
|
{ |
91
|
6 |
|
$targets = $this->getDbTargets(); |
92
|
6 |
|
foreach ($targets as $target) { |
93
|
6 |
|
$this->db = $target->db; |
94
|
|
|
|
95
|
6 |
|
$this->dropTable($target->logTable); |
96
|
|
|
} |
97
|
6 |
|
} |
98
|
|
|
} |
99
|
|
|
|