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
|
|
|
* @author Alexander Makarov <[email protected]> |
16
|
|
|
* @since 2.0.1 |
17
|
|
|
*/ |
18
|
|
|
class m141106_185632_log_init extends Migration |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var DbTarget[] Targets to create log table for |
22
|
|
|
*/ |
23
|
|
|
private $dbTargets = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws InvalidConfigException |
27
|
|
|
* @return DbTarget[] |
28
|
|
|
*/ |
29
|
|
|
protected function getDbTargets() |
30
|
|
|
{ |
31
|
|
|
if ($this->dbTargets === []) { |
32
|
|
|
$log = Yii::$app->getLog(); |
33
|
|
|
|
34
|
|
|
$usedTargets = []; |
35
|
|
|
foreach ($log->targets as $target) { |
36
|
|
|
if ($target instanceof DbTarget) { |
37
|
|
|
$currentTarget = [ |
38
|
|
|
$target->db, |
39
|
|
|
$target->logTable, |
40
|
|
|
]; |
41
|
|
|
if (!in_array($currentTarget, $usedTargets, true)) { |
42
|
|
|
// do not create same table twice |
43
|
|
|
$usedTargets[] = $currentTarget; |
44
|
|
|
$this->dbTargets[] = $target; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->dbTargets; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function up() |
54
|
|
|
{ |
55
|
|
|
foreach ($this->getDbTargets() as $target) { |
56
|
|
|
$this->db = $target->db; |
57
|
|
|
|
58
|
|
|
$tableOptions = null; |
59
|
|
|
if ($this->db->driverName === 'mysql') { |
60
|
|
|
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
61
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->createTable($target->logTable, [ |
65
|
|
|
'id' => $this->bigPrimaryKey(), |
66
|
|
|
'level' => $this->integer(), |
67
|
|
|
'category' => $this->string(), |
68
|
|
|
'log_time' => $this->double(), |
69
|
|
|
'prefix' => $this->text(), |
70
|
|
|
'message' => $this->text(), |
71
|
|
|
], $tableOptions); |
72
|
|
|
|
73
|
|
|
$this->createIndex('idx_log_level', $target->logTable, 'level'); |
74
|
|
|
$this->createIndex('idx_log_category', $target->logTable, 'category'); |
75
|
|
|
$this->createIndex('idx_log_time', $target->logTable, 'log_time'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function down() |
80
|
|
|
{ |
81
|
|
|
foreach ($this->getDbTargets() as $target) { |
82
|
|
|
$this->db = $target->db; |
83
|
|
|
|
84
|
|
|
$this->dropTable($target->logTable); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|