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