m150909_153426_cache_init.php 1.6 KB

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