Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

m141106_185632_log_init   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 73
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

3 Methods

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