aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.318_step_00353_cache.php
blob: 5959945db68aa14742a113ddcd5b200ac066c5f2 (plain)
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
67
<?php

use Studip\Cache\DbCache;
use Studip\Cache\FileCache;
use Studip\Cache\MemcachedCache;
use Studip\Cache\RedisCache;

class Step00353Cache extends Migration
{
    public function description()
    {
        return 'Manage available caches and their settings.';
    }

    public function up()
    {
        // Create database table for available system cache types
        DBManager::get()->execute("CREATE TABLE IF NOT EXISTS `cache_types` (
            `cache_id` INT NOT NULL AUTO_INCREMENT,
            `class_name` VARCHAR(255) NOT NULL,
            `chdate` INT(11) DEFAULT NULL,
            `mkdate` INT(11) DEFAULT NULL,
            PRIMARY KEY (`cache_id`),
            UNIQUE KEY (`class_name`)
            ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC");

        $types = [
            DbCache::class,
            FileCache::class,
            MemcachedCache::class,
            RedisCache::class,
        ];

        // Insert pre-defined cache types in to database
        foreach ($types as $type) {
            DBManager::get()->execute(
                "INSERT IGNORE INTO `cache_types` (`class_name`, `mkdate`, `chdate`)
                VALUES (?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())", [$type]);
        }

        // Remove other set cache config entries.
        $fields = ['cache_class', 'cache_class_file', 'cache_init_args'];
        DBManager::get()->execute("DELETE FROM `config_values` WHERE `field` IN (:fields)", ['fields' => $fields]);
        DBManager::get()->execute("DELETE FROM `config` WHERE `field` IN (:fields)", ['fields' => $fields]);

        // Set StudipDbCache as (possibly new) default
        $cache = [
            'type' => 'StudipDbCache',
            'config' => []
        ];
        DBManager::get()->execute("INSERT IGNORE INTO `config` VALUES
            (:field, :value, 'array', 'global', 'global', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :desc)",
            [
                'field' => 'SYSTEMCACHE',
                'value' => json_encode($cache),
                'desc' => 'Typ und Konfiguration des zu verwendenden Systemcaches'
            ]
        );
    }

    public function down()
    {
        DBManager::get()->execute("DROP TABLE IF EXISTS `cache_types`");
        DBManager::get()->execute("DELETE FROM `config_values` WHERE `field` = :field", ['field' => 'SYSTEMCACHE']);
        DBManager::get()->execute("DELETE FROM `config` WHERE `field` = :field", ['field' => 'SYSTEMCACHE']);
    }
}