Completed
Push — fix-unique-exists-expressions ( b0719b...8467f4 )
by Alexander
07:52
created

m141106_185632_log_init   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0
ccs 36
cts 38
cp 0.9474

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getDbTargets() 0 26 6
A down() 0 9 2
B up() 0 25 3
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 4
    protected function getDbTargets()
34
    {
35 4
        if ($this->dbTargets === []) {
36 4
            $log = Yii::$app->getLog();
37
38 4
            $usedTargets = [];
39 4
            foreach ($log->targets as $target) {
40 4
                if ($target instanceof DbTarget) {
41
                    $currentTarget = [
42 4
                        $target->db,
43 4
                        $target->logTable,
44
                    ];
45 4
                    if (!in_array($currentTarget, $usedTargets, true)) {
46
                        // do not create same table twice
47 4
                        $usedTargets[] = $currentTarget;
48 4
                        $this->dbTargets[] = $target;
49
                    }
50
                }
51
            }
52
53 4
            if ($this->dbTargets === []) {
54
                throw new InvalidConfigException('You should configure "log" component to use one or more database targets before executing this migration.');
55
            }
56
        }
57 4
        return $this->dbTargets;
58
    }
59
60 4
    public function up()
61
    {
62 4
        $targets = $this->getDbTargets();
63 4
        foreach ($targets as $target) {
64 4
            $this->db = $target->db;
65
66 4
            $tableOptions = null;
67 4
            if ($this->db->driverName === 'mysql') {
68
                // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
69
                $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
70
            }
71
72 4
            $this->createTable($target->logTable, [
73 4
                'id' => $this->bigPrimaryKey(),
74 4
                'level' => $this->integer(),
75 4
                'category' => $this->string(),
76 4
                'log_time' => $this->double(),
77 4
                'prefix' => $this->text(),
78 4
                'message' => $this->text(),
79 4
            ], $tableOptions);
80
81 4
            $this->createIndex('idx_log_level', $target->logTable, 'level');
82 4
            $this->createIndex('idx_log_category', $target->logTable, 'category');
83
        }
84 4
    }
85
86 4
    public function down()
87
    {
88 4
        $targets = $this->getDbTargets();
89 4
        foreach ($targets as $target) {
90 4
            $this->db = $target->db;
91
92 4
            $this->dropTable($target->logTable);
93
        }
94 4
    }
95
}
96