Issues (83)

src/migrations/Install.php (4 issues)

1
<?php
2
/**
3
 * Spoon plugin for Craft CMS 3.x
4
 *
5
 * Enhance Matrix
6
 *
7
 * @link      https://angell.io
8
 * @copyright Copyright (c) 2018 Angell & Co
9
 */
10
11
namespace angellco\spoon\migrations;
12
13
use angellco\spoon\Spoon;
14
15
use Craft;
16
use craft\config\DbConfig;
17
use craft\db\Migration;
18
use craft\db\Query;
19
20
/**
21
 * Spoon Install Migration
22
 *
23
 * If your plugin needs to create any custom database tables when it gets installed,
24
 * create a migrations/ folder within your plugin folder, and save an Install.php file
25
 * within it using the following template:
26
 *
27
 * If you need to perform any additional actions on install/uninstall, override the
28
 * safeUp() and safeDown() methods.
29
 *
30
 * @author    Angell & Co
31
 * @package   Spoon
32
 * @since     3.0.0
33
 */
34
class Install extends Migration
35
{
36
    // Public Properties
37
    // =========================================================================
38
39
    /**
40
     * @var string The database driver to use
41
     */
42
    public $driver;
43
44
    // Public Methods
45
    // =========================================================================
46
47
    /**
48
     * This method contains the logic to be executed when applying this migration.
49
     * This method differs from [[up()]] in that the DB logic implemented here will
50
     * be enclosed within a DB transaction.
51
     * Child classes may implement this method instead of [[up()]] if the DB logic
52
     * needs to be within a transaction.
53
     *
54
     * @return boolean return a false value to indicate the migration fails
55
     * and should not proceed further. All other return values mean the migration succeeds.
56
     */
57
    public function safeUp()
58
    {
59
60
        if ($this->_upgradeFromCraft2()) {
61
            return;
62
        }
63
64
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
0 ignored issues
show
Deprecated Code introduced by
The property craft\config\DbConfig::$driver has been deprecated: in 3.4.0. [[dsn]] should be set directly instead. ( Ignorable by Annotation )

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

64
        $this->driver = /** @scrutinizer ignore-deprecated */ Craft::$app->getConfig()->getDb()->driver;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
65
        if ($this->createTables()) {
66
            $this->createIndexes();
67
            $this->addForeignKeys();
68
            // Refresh the db schema caches
69
            Craft::$app->db->schema->refresh();
70
            $this->insertDefaultData();
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * This method contains the logic to be executed when removing this migration.
78
     * This method differs from [[down()]] in that the DB logic implemented here will
79
     * be enclosed within a DB transaction.
80
     * Child classes may implement this method instead of [[down()]] if the DB logic
81
     * needs to be within a transaction.
82
     *
83
     * @return boolean return a false value to indicate the migration fails
84
     * and should not proceed further. All other return values mean the migration succeeds.
85
     */
86
    public function safeDown()
87
    {
88
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
0 ignored issues
show
Deprecated Code introduced by
The property craft\config\DbConfig::$driver has been deprecated: in 3.4.0. [[dsn]] should be set directly instead. ( Ignorable by Annotation )

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

88
        $this->driver = /** @scrutinizer ignore-deprecated */ Craft::$app->getConfig()->getDb()->driver;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
89
        $this->removeTables();
90
91
        return true;
92
    }
93
94
    // Protected Methods
95
    // =========================================================================
96
97
    /**
98
     * Creates the tables needed for the Records used by the plugin
99
     *
100
     * @return bool
101
     */
102
    protected function createTables()
103
    {
104
        $tablesCreated = false;
105
106
        // spoon_blocktypes table
107
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%spoon_blocktypes}}');
108
        if ($tableSchema === null) {
109
            $tablesCreated = true;
110
            $this->createTable(
111
                '{{%spoon_blocktypes}}',
112
                [
113
                    'id' => $this->primaryKey(),
114
                    'fieldId' => $this->integer()->notNull(),
115
                    'matrixBlockTypeId' => $this->integer()->notNull(),
116
                    'fieldLayoutId' => $this->integer(),
117
                    'groupName' => $this->string(255)->notNull()->defaultValue(''),
118
                    'context' => $this->string(255)->notNull()->defaultValue(''),
119
                    'groupSortOrder' => $this->smallInteger()->unsigned(),
120
                    'sortOrder' => $this->smallInteger()->unsigned(),
121
                    'dateCreated' => $this->dateTime()->notNull(),
122
                    'dateUpdated' => $this->dateTime()->notNull(),
123
                    'uid' => $this->uid(),
124
                ]
125
            );
126
        }
127
128
        return $tablesCreated;
129
    }
130
131
    /**
132
     * Creates the indexes needed for the Records used by the plugin
133
     *
134
     * @return void
135
     */
136
    protected function createIndexes()
137
    {
138
        // spoon_blocktypes table
139
        $this->createIndex(
140
            $this->db->getIndexName(
141
                '{{%spoon_blocktypes}}',
142
                'fieldId',
143
                false
144
            ),
145
            '{{%spoon_blocktypes}}',
146
            'fieldId',
147
            false
148
        );
149
        $this->createIndex(
150
            $this->db->getIndexName(
151
                '{{%spoon_blocktypes}}',
152
                'matrixBlockTypeId',
153
                false
154
            ),
155
            '{{%spoon_blocktypes}}',
156
            'matrixBlockTypeId',
157
            false
158
        );
159
        $this->createIndex(
160
            $this->db->getIndexName(
161
                '{{%spoon_blocktypes}}',
162
                'fieldLayoutId',
163
                false
164
            ),
165
            '{{%spoon_blocktypes}}',
166
            'fieldLayoutId',
167
            false
168
        );
169
170
        // Additional commands depending on the db driver
171
        switch ($this->driver) {
172
            case DbConfig::DRIVER_MYSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_MYSQL has been deprecated: in 3.4.0 ( Ignorable by Annotation )

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

172
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_MYSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
173
                break;
174
            case DbConfig::DRIVER_PGSQL:
0 ignored issues
show
Deprecated Code introduced by
The constant craft\config\DbConfig::DRIVER_PGSQL has been deprecated: in 3.4.0 ( Ignorable by Annotation )

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

174
            case /** @scrutinizer ignore-deprecated */ DbConfig::DRIVER_PGSQL:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
175
                break;
176
        }
177
    }
178
179
    /**
180
     * Creates the foreign keys needed for the Records used by the plugin
181
     *
182
     * @return void
183
     */
184
    protected function addForeignKeys()
185
    {
186
        // spoon_blocktypes table
187
        $this->addForeignKey(
188
            $this->db->getForeignKeyName('{{%spoon_blocktypes}}', 'fieldId'),
189
            '{{%spoon_blocktypes}}',
190
            'fieldId',
191
            '{{%fields}}',
192
            'id',
193
            'CASCADE',
194
            'CASCADE'
195
        );
196
        $this->addForeignKey(
197
            $this->db->getForeignKeyName('{{%spoon_blocktypes}}', 'matrixBlockTypeId'),
198
            '{{%spoon_blocktypes}}',
199
            'matrixBlockTypeId',
200
            '{{%matrixblocktypes}}',
201
            'id',
202
            'CASCADE',
203
            'CASCADE'
204
        );
205
        $this->addForeignKey(
206
            $this->db->getForeignKeyName('{{%spoon_blocktypes}}', 'fieldLayoutId'),
207
            '{{%spoon_blocktypes}}',
208
            'fieldLayoutId',
209
            '{{%fieldlayouts}}',
210
            'id',
211
            'CASCADE',
212
            'CASCADE'
213
        );
214
    }
215
216
    /**
217
     * Populates the DB with the default data.
218
     *
219
     * @return void
220
     */
221
    protected function insertDefaultData()
222
    {
223
    }
224
225
    /**
226
     * Removes the tables needed for the Records used by the plugin
227
     *
228
     * @return void
229
     */
230
    protected function removeTables()
231
    {
232
        // spoon_blocktypes table
233
        $this->dropTableIfExists('{{%spoon_blocktypes}}');
234
    }
235
236
237
    // Private Methods
238
    // =========================================================================
239
240
    /**
241
     * Upgrade from Craft 2
242
     *
243
     * @return bool
244
     */
245
    private function _upgradeFromCraft2()
246
    {
247
248
        // If this install is 3.1 then the settings column won’t be there,
249
        // so we need to bail. However, if upgrading from 2.x then we should
250
        // hopefully be on 3.0.x first.
251
        if (substr( Craft::$app->getVersion(), 0, 3 ) !== "3.0") {
252
            return false;
253
        }
254
255
        // Fetch the old plugin row, if it was installed
256
        $row = (new Query())
257
            ->select(['id', 'settings'])
258
            ->from(['{{%plugins}}'])
259
            ->where(['in', 'handle', ['pimp-my-matrix', 'pimpmymatrix']])
260
            ->one();
261
262
        if (!$row) {
263
            return false;
264
        }
265
266
        // Delete the old row
267
        $this->delete('{{%plugins}}', ['id' => $row['id']]);
268
269
        // Rename the old table, the schema is identical
270
        $this->renameTable('{{%pimpmymatrix_blocktypes}}', '{{%spoon_blocktypes}}');
271
272
        return true;
273
    }
274
275
}
276