Passed
Push — master ( f96468...e8dc9f )
by M. Mikkel
03:43
created

m200722_000315_projectconfig::safeDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
4
namespace mmikkel\cacheflag\migrations;
5
6
use Craft;
7
use craft\db\Migration;
8
use craft\db\Query;
9
use craft\db\Table;
10
use craft\helpers\Db;
11
12
/**
13
 * Class m200722_000315_projectconfig
14
 * @package mmikkel\cacheflag\migrations
15
 * @since 1.2.0
16
 */
17
class m200722_000315_projectconfig extends Migration
18
{
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function safeUp()
24
    {
25
        // Don't make the same config changes twice
26
        $schemaVersion = Craft::$app->projectConfig
27
            ->get('plugins.cache-flag.schemaVersion', true);
28
29
        if (\version_compare($schemaVersion, '1.0.1', '>=')) {
30
            return;
31
        }
32
33
        $rows = (new Query())
34
            ->select(['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'volumeId', 'globalSetId', 'elementType', 'uid'])
35
            ->from('{{%cacheflag_flags}}')
36
            ->all();
37
38
        foreach ($rows as $row) {
39
40
            $source = null;
41
42
            if ($row['sectionId']) {
43
                if ($uid = Db::uidById(Table::SECTIONS, $row['sectionId'])) {
44
                    $source = "section:$uid";
45
                }
46
            } else if ($row['categoryGroupId']) {
47
                if ($uid = Db::uidById(Table::CATEGORYGROUPS, $row['categoryGroupId'])) {
48
                    $source = "categoryGroup:$uid";
49
                }
50
            } else if ($row['tagGroupId']) {
51
                if ($uid = Db::uidById(Table::TAGGROUPS, $row['tagGroupId'])) {
52
                    $source = "tagGroup:$uid";
53
                }
54
            } else if ($row['userGroupId']) {
55
                if ($uid = Db::uidById(Table::USERGROUPS, $row['userGroupId'])) {
56
                    $source = "userGroup:$uid";
57
                }
58
            } else if ($row['volumeId']) {
59
                if ($uid = Db::uidById(Table::VOLUMES, $row['volumeId'])) {
60
                    $source = "volume:$uid";
61
                }
62
            } else if ($row['globalSetId']) {
63
                if ($uid = Db::uidById(Table::GLOBALSETS, $row['globalSetId'])) {
64
                    $source = "globalSet:$uid";
65
                }
66
            } else if ($row['elementType']) {
67
                $source = 'elementType:' . $row['elementType'];
68
            }
69
70
            if (!$source) {
71
                continue;
72
            }
73
74
            $path = "cacheFlags.{$row['uid']}";
75
76
            Craft::$app->projectConfig->set($path, [
77
                'source' => $source,
78
                'flags' => $row['flags'],
79
            ]);
80
        }
81
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function safeDown()
88
    {
89
        echo "m200722_000315_projectconfig cannot be reverted.\n";
90
        return false;
91
    }
92
93
}
94