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

m150909_153426_cache_init::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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\caching\DbCache;
10
use yii\db\Migration;
11
12
/**
13
 * Initializes Cache tables.
14
 *
15
 * @author Misbahul D Munir <[email protected]>
16
 * @since 2.0.7
17
 */
18
class m150909_153426_cache_init extends Migration
19
{
20
    /**
21
     * @throws yii\base\InvalidConfigException
22
     * @return DbCache
23
     */
24
    protected function getCache()
25
    {
26
        $cache = Yii::$app->getCache();
27
        if (!$cache instanceof DbCache) {
28
            throw new InvalidConfigException('You should configure "cache" component to use database before executing this migration.');
29
        }
30
31
        return $cache;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function up()
38
    {
39
        $cache = $this->getCache();
40
        $this->db = $cache->db;
41
42
        $tableOptions = null;
43
        if ($this->db->driverName === 'mysql') {
44
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
45
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
46
        }
47
48
        $this->createTable($cache->cacheTable, [
49
            'id' => $this->string(128)->notNull(),
50
            'expire' => $this->integer(),
51
            'data' => $this->binary(),
52
            'PRIMARY KEY ([[id]])',
53
            ], $tableOptions);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function down()
60
    {
61
        $cache = $this->getCache();
62
        $this->db = $cache->db;
63
64
        $this->dropTable($cache->cacheTable);
65
    }
66
}
67