aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/6.0.3_adjust_cache_configuration.php
blob: 2432e8474ff4a438cdabb777e004440dfece76d7 (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
<?php
return new class extends Migration
{
    private const MAPPING = [
        StudipDbCache::class        => Studip\Cache\DbCache::class,
        StudipFileCache::class      => Studip\Cache\FileCache::class,
        StudipMemcachedCache::class => Studip\Cache\MemcachedCache::class,
        StudipRedisCache::class     => Studip\Cache\RedisCache::class,
    ];

    public function description()
    {
        return 'Replaces the renamed cache classes in system configuration';
    }

    protected function up()
    {
        foreach (self::MAPPING as $old => $new) {
            self::replaceCache($old, $new);
        }
    }

    protected function down()
    {
        foreach (self::MAPPING as $old => $new) {
            self::replaceCache($new, $old);
        }
    }

    private function replaceCache(string $old, string $new): void
    {
        $query = "UPDATE `config_values`
                  SET `value` = JSON_REPLACE(`value`, '$.type', ?)
                  WHERE `field` = 'SYSTEMCACHE'
                    AND JSON_CONTAINS(`value`, JSON_QUOTE(?), '$.type')";
        DBManager::get()->execute($query, [$new, $old]);
    }
};