Issues (7)

src/migrations/Install.php (2 issues)

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
        // Remove the old "cacheflag_flagged" table if it exists
51
        $this->dropTableIfExists('{{%cacheflag_flagged}}');
52
53
        return true;
54
    }
55
56
   /**
57
     * @inheritdoc
58
     */
59
    public function safeDown()
60
    {
61
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
62
        $this->removeTables();
63
64
        return true;
65
    }
66
67
    // Protected Methods
68
    // =========================================================================
69
70
    /**
71
     * @return bool
72
     */
73
    protected function createTables()
74
    {
75
        $tablesCreated = false;
76
77
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%cacheflag_flags}}');
78
        if ($tableSchema === null) {
79
            $tablesCreated = true;
80
            $this->createTable(
81
                '{{%cacheflag_flags}}',
82
                [
83
                    'id' => $this->primaryKey(),
84
                    'flags' => $this->string(255)->notNull(),
85
                    'sectionId' => $this->integer()->unique(),
86
                    'categoryGroupId' => $this->integer()->unique(),
87
                    'tagGroupId' => $this->integer()->unique(),
88
                    'userGroupId' => $this->integer()->unique(),
89
                    'volumeId' => $this->integer()->unique(),
90
                    'globalSetId' => $this->integer()->unique(),
91
                    'elementType' => $this->string(255)->unique(),
92
                    'dateCreated' => $this->dateTime()->notNull(),
93
                    'dateUpdated' => $this->dateTime()->notNull(),
94
                    'uid' => $this->uid(),
95
                ]
96
            );
97
        }
98
99
        return $tablesCreated;
100
    }
101
102
    /**
103
     * @return void
104
     */
105
    protected function createIndexes()
106
    {
107
        $this->createIndex(
108
            $this->db->getIndexName(
109
                '{{%cacheflag_flags}}',
0 ignored issues
show
The call to craft\db\Connection::getIndexName() has too many arguments starting with '{{%cacheflag_flags}}'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            $this->db->/** @scrutinizer ignore-call */ 
110
                       getIndexName(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110
                'flags',
111
                false
112
            ),
113
            '{{%cacheflag_flags}}',
114
            'flags',
115
            false
116
        );
117
    }
118
119
    /**
120
     * @return void
121
     */
122
    protected function addForeignKeys()
123
    {
124
        $this->addForeignKey(
125
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'sectionId'),
0 ignored issues
show
The call to craft\db\Connection::getForeignKeyName() has too many arguments starting with '{{%cacheflag_flags}}'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            $this->db->/** @scrutinizer ignore-call */ 
126
                       getForeignKeyName('{{%cacheflag_flags}}', 'sectionId'),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
126
            '{{%cacheflag_flags}}',
127
            'sectionId',
128
            '{{%sections}}',
129
            'id',
130
            'CASCADE',
131
            'CASCADE'
132
        );
133
134
        $this->addForeignKey(
135
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'categoryGroupId'),
136
            '{{%cacheflag_flags}}',
137
            'categoryGroupId',
138
            '{{%categorygroups}}',
139
            'id',
140
            'CASCADE',
141
            'CASCADE'
142
        );
143
144
        $this->addForeignKey(
145
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'tagGroupId'),
146
            '{{%cacheflag_flags}}',
147
            'tagGroupId',
148
            '{{%taggroups}}',
149
            'id',
150
            'CASCADE',
151
            'CASCADE'
152
        );
153
154
        $this->addForeignKey(
155
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'userGroupId'),
156
            '{{%cacheflag_flags}}',
157
            'userGroupId',
158
            '{{%usergroups}}',
159
            'id',
160
            'CASCADE',
161
            'CASCADE'
162
        );
163
164
        $this->addForeignKey(
165
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'volumeId'),
166
            '{{%cacheflag_flags}}',
167
            'volumeId',
168
            '{{%volumes}}',
169
            'id',
170
            'CASCADE',
171
            'CASCADE'
172
        );
173
174
        $this->addForeignKey(
175
            $this->db->getForeignKeyName('{{%cacheflag_flags}}', 'globalSetId'),
176
            '{{%cacheflag_flags}}',
177
            'globalSetId',
178
            '{{%globalsets}}',
179
            'id',
180
            'CASCADE',
181
            'CASCADE'
182
        );
183
    }
184
185
    /**
186
     * @return void
187
     */
188
    protected function insertDefaultData()
189
    {
190
    }
191
192
    /**
193
     * @return void
194
     */
195
    protected function removeTables()
196
    {
197
        $this->dropTableIfExists('{{%cacheflag_flagged}}');
198
        $this->dropTableIfExists('{{%cacheflag_flags}}');
199
    }
200
}
201