Passed
Push — master ( 6afa72...f96468 )
by M. Mikkel
04:08
created

Install::createTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 24
rs 9.7333
1
<?php
2
/**
3
 * Cache Flag plugin for Craft CMS 3.x
4
 *
5
 * Flag and clear template caches.
6
 *
7
 * @link      https://vaersaagod.no
8
 * @copyright Copyright (c) 2018 Mats Mikkel Rummelhoff
9
 */
10
11
namespace mmikkel\cacheflag\migrations;
12
13
use mmikkel\cacheflag\CacheFlag;
14
15
use Craft;
16
use craft\db\Migration;
17
18
/**
19
 * @author    Mats Mikkel Rummelhoff
20
 * @package   CacheFlag
21
 * @since     1.0.0
22
 */
23
class Install extends Migration
24
{
25
    // Public Properties
26
    // =========================================================================
27
28
    /**
29
     * @var string The database driver to use
30
     */
31
    public $driver;
32
33
    // Public Methods
34
    // =========================================================================
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function safeUp()
40
    {
41
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
42
        if ($this->createTables()) {
43
            $this->createIndexes();
44
            $this->addForeignKeys();
45
            // Refresh the db schema caches
46
            Craft::$app->db->schema->refresh();
47
            $this->insertDefaultData();
48
        }
49
50
        return true;
51
    }
52
53
   /**
54
     * @inheritdoc
55
     */
56
    public function safeDown()
57
    {
58
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
59
        $this->removeTables();
60
61
        return true;
62
    }
63
64
    // Protected Methods
65
    // =========================================================================
66
67
    /**
68
     * @return bool
69
     */
70
    protected function createTables()
71
    {
72
        $tablesCreated = false;
73
74
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%cacheflag_flags}}');
75
        if ($tableSchema === null) {
76
            $tablesCreated = true;
77
            $this->createTable(
78
                '{{%cacheflag_flags}}',
79
                [
80
                    'id' => $this->primaryKey(),
81
                    'flags' => $this->string(255)->notNull(),
82
                    'sectionId' => $this->integer()->unique(),
83
                    'categoryGroupId' => $this->integer()->unique(),
84
                    'tagGroupId' => $this->integer()->unique(),
85
                    'userGroupId' => $this->integer()->unique(),
86
                    'volumeId' => $this->integer()->unique(),
87
                    'globalSetId' => $this->integer()->unique(),
88
                    'elementType' => $this->string(255)->unique(),
89
                ]
90
            );
91
        }
92
93
        return $tablesCreated;
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    protected function createIndexes()
100
    {
101
        $this->createIndex(
102
            $this->db->getIndexName(
103
                '{{%cacheflag_flags}}',
104
                'flags',
105
                false
106
            ),
107
            '{{%cacheflag_flags}}',
108
            'flags',
109
            false
110
        );
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    protected function addForeignKeys()
117
    {
118
        $this->addForeignKey(
119
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'sectionId'),
120
            '{{%cacheflag_flags}}',
121
            'sectionId',
122
            '{{%sections}}',
123
            'id',
124
            'CASCADE',
125
            'CASCADE'
126
        );
127
128
        $this->addForeignKey(
129
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'categoryGroupId'),
130
            '{{%cacheflag_flags}}',
131
            'categoryGroupId',
132
            '{{%categorygroups}}',
133
            'id',
134
            'CASCADE',
135
            'CASCADE'
136
        );
137
138
        $this->addForeignKey(
139
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'tagGroupId'),
140
            '{{%cacheflag_flags}}',
141
            'tagGroupId',
142
            '{{%taggroups}}',
143
            'id',
144
            'CASCADE',
145
            'CASCADE'
146
        );
147
148
        $this->addForeignKey(
149
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'userGroupId'),
150
            '{{%cacheflag_flags}}',
151
            'userGroupId',
152
            '{{%usergroups}}',
153
            'id',
154
            'CASCADE',
155
            'CASCADE'
156
        );
157
158
        $this->addForeignKey(
159
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'volumeId'),
160
            '{{%cacheflag_flags}}',
161
            'volumeId',
162
            '{{%volumes}}',
163
            'id',
164
            'CASCADE',
165
            'CASCADE'
166
        );
167
168
        $this->addForeignKey(
169
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'globalSetId'),
170
            '{{%cacheflag_flags}}',
171
            'globalSetId',
172
            '{{%globalsets}}',
173
            'id',
174
            'CASCADE',
175
            'CASCADE'
176
        );
177
    }
178
179
    /**
180
     * @return void
181
     */
182
    protected function insertDefaultData()
183
    {
184
    }
185
186
    /**
187
     * @return void
188
     */
189
    protected function removeTables()
190
    {
191
        $this->dropTableIfExists('{{%cacheflag_flagged}}');
192
193
        $this->dropTableIfExists('{{%cacheflag_flags}}');
194
    }
195
}
196