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

m150909_153426_cache_init   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 47
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 6 1
A up() 0 17 2
A getCache() 0 8 2
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